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.

0

4 Answers 4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I found the solution and the code is posted on github. A working code exists at github.com/hth/StatusInvoke.git
ok. I thought you want to bind the values of the list to the jsp controls. So I thought this would be more easy. :)
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.

9 Comments

userManager is not returning user empty. If I remove the binding and just print out the user it prints.
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
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
I have tried as you have mentioned and it still does not work. For reference I have created a sample code to test at github.com/hth/StatusInvoke.git
@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".
|
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

Comments

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.

1 Comment

Can you give more information as I am not able to understand what you are suggesting as alternative

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.