0

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. Getting this exception HTTP Status 500 - javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'users[0]' available as request attribute

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

Where am I going wrong and what do I need to do? Thanks

Not working code below. [I have to invoke formatting on field @NumberFormat so have to try it using status variable]

<spring:bind path="user.name">
   <c:out value="${status.value}" />
</spring:bind>

Gets this error --> javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute

So added a bean binding and then I get empty table :(. I believe thats because the instance is empty. So this does not seems like a right approach.

@ModelAttribute("user")
public UserEntity userEntityBinding() {
    return UserEntity.newInstance();
}

A working code exists at https://github.com/hth/StatusInvoke.git

Let me know if you face any problem deploying it.

This question has been solved. Thanks for looking at it.

java_dude
  • 4,038
  • 9
  • 36
  • 61

4 Answers4

1

You can try using LazyList instead of simple list. If you want to take a look at the example then you can refer one of my question. In the question statement I have mentioned how to use the LazyList.

Hope that helps you. Cheers.

Community
  • 1
  • 1
Japan Trivedi
  • 4,445
  • 2
  • 23
  • 44
  • I found the solution and the code is posted on github. A working code exists at https://github.com/hth/StatusInvoke.git – java_dude Jan 09 '13 at 07:06
  • ok. I thought you want to bind the values of the list to the jsp controls. So I thought this would be more easy. :) – Japan Trivedi Jan 09 '13 at 07:10
0

this, if the modelandview are returned, is the correct way to populate the list

ModelAndView modelAndView = new ModelAndView("go_some_JSP_page");
List<UserEntity> users = userManager.getAllObjects();
modelAndView.addObject("users", users);

And this is the correct way to reference the list

<c:forEach items="${users}" var="user" varStatus="status">
    <spring:bind path="user.name">
      <c:out value="${status.value}" />
   </spring:bind>
</c:forEach>

Your problem must be elsewhere is the name field definitely populated, is the correct jsp being called... the above code is correct and should work.

NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
  • userManager is not returning `user` empty. If I remove the binding and just print out the `user` it prints. – java_dude Dec 31 '12 at 22:22
  • After changing the binding I see this error `javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute` – java_dude Dec 31 '12 at 23:11
  • I assume you are asking what is the result of the code posted above :) Its actually blank. I see empty table. I have mentioned that in my posting. And the empty result is because of the empty object `UserEntity` – java_dude Jan 01 '13 at 19:01
  • I have tried as you have mentioned and it still does not work. For reference I have created a sample code to test at https://github.com/hth/StatusInvoke.git – java_dude Jan 02 '13 at 06:03
  • @java_dude you are redirecting from your index, to landing without actually calling the controller. Be sure to call your code using the url "/landing". – NimChimpsky Jan 02 '13 at 15:50
  • If the controller is not being called then how is the page being populated with data. Have you tried putting a break point in controller at ModelAndView? – java_dude Jan 02 '13 at 17:35
  • @java_dude its not being populated with data, according to yr question – NimChimpsky Jan 02 '13 at 17:36
  • The data is getting populated but its not using ` – java_dude Jan 02 '13 at 17:47
  • I understand what you are saying. The reason why I have to get the uncommented section working is because the formatting code gets called through `var=status`. The UserEntity has @NumberFormat annotation and that would be called through `status` alone. If I print balance without status, then annotation would not be invoked and no formatting would show up. Hope this clears it. – java_dude Jan 02 '13 at 18:38
0

The correct answer to invoke @NumberFormat annotation is by using spring:eval expression tag

<spring:eval expression="user.balance" />

This invokes the annotation and performs formatting as mentioned in the annotation

java_dude
  • 4,038
  • 9
  • 36
  • 61
0

I don't think you can use spring:bind in that case, AFAIK it tries to get the variable from the ModelMap, it's not able to get it from the "for" var.

beder
  • 1,086
  • 5
  • 10