1

I am new to spring-mvc and have a basic question. I have a controller that reads the parameters that are sent in from a jsp and adds an object called userInfo to the ModelAndView and passes on to another jsp. The second jsp displays the vaious properites of the userInfo. How do I send back the userInfo object to the controller?

 <td><input type="hidden" name="userInfo" value="${requestScope.userInfo}"/></td>

I try to read the userInfo in the controller as follows:

request.getAttribute("userInfo")

However, this is null. What is the best way for me to do this? Thanks

3
  • Can you share the applicable code for your controller? Commented Nov 19, 2013 at 14:40
  • In the jsp, I am passing it as a hidden field as follows: <td><input type ="hidden" name ="userInfo" value = "${requestScope.userInfo}"/></td> In the controller this is how I am reading it UserInfo userInfo = (UserInfo) request.getAttribute("userInfo"); Commented Nov 19, 2013 at 15:02
  • I'd look into the @RequestParam annotation as suggested in the answer below, that's why I was curious about your controller code. I don't use request.getAttribute() in any of my Spring controllers. Commented Nov 19, 2013 at 15:05

1 Answer 1

1

HTML <form> <input> elements are sent as url-encoded parameters. You need to access them with HttpServletRequest#getParameter(String)

request.getParameter("userInfo");

Or use the @RequestParam annotation on a handler method parameter

@RequestMapping(...)
public String myHandler(@RequestParam("userInfo") String userInfo) {
   ...
}

Note however, that this won't send back the object, it will send back a String which is the toString() value of the object because that is what is given with ${requestScope.userInfo}.

Consider using session or flash attributes to have access to the same object in future requests.

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

Comments

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.