1
votes

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

1
Can you share the applicable code for your controller?Pedantic
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");Mary
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.Pedantic

1 Answers

1
votes

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.