Mary
Mary

Reputation: 1595

spring mvc-adding a object to the request

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

Upvotes: 1

Views: 1002

Answers (1)

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279920

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.

Upvotes: 1

Related Questions