Reputation: 4088
I have a list containing users. I am trying to print it in JSP but some how I am not able to get it to print it using spring:bind
. I get an exception. The reason I am trying to use spring:bind
is to invole @Formatting annotation.
Code in JSP
<c:forEach items="${users}" var="user" varStatus="status">
<spring:bind path="users[${status.index}].name">
<c:out value="${status.value}" />
</spring:bind>
</c:forEach>
Controller
ModelAndView modelAndView = new ModelAndView("go_some_JSP_page");
List<UserEntity> users = userManager.getAllObjects();
modelAndView.addObject("users", users);
BTW, UserEntity has name field. If I remove the binding and try to print the user.name
using <c:out value="user.name" />
it prints the value
A test sample code exists at https://github.com/hth/StatusInvoke.git
Please take a look at landing.jsp line 35
Let me know if you face any problem deploying it.
Upvotes: 0
Views: 483
Reputation: 4088
The correct answer to invoke @NumberFormat annotation is by using spring:eval expression
tag
<spring:eval expression="user.balance" />
Upvotes: 0
Reputation: 4246
The only way I could get this to work is if the bean path that you are binding to is within the base command bean. In my case I am using MyForm which has a getter and setter for users. The spring bind tag does not seem to work outside of the base form command bean.
So your controller method needs to change to .
@RequestMapping(method = RequestMethod.GET)
public ModelAndView loadForm() {
ModelAndView modelAndView = new ModelAndView("landing");
MyForm myForm = new MyForm();
myForm.setUsers(populate());
modelAndView.addObject("myForm", myForm);
return modelAndView;
}
In your jsp have the following.
<form:form commandName="myForm">
<c:forEach var="user" items="${myForm.users}" varStatus="status">
<tr>
<td><spring:bind path="users[${status.index}].loginName">
<c:out value="${status.value}" />
</spring:bind></td>
<td><spring:bind path="users[${status.index}].balance">
<c:out value="${status.value}" />
</spring:bind></td>
</tr>
</c:forEach>
</form:form>
Upvotes: 1