Search This Blog

Tuesday, 27 November 2012

Processing Parameters in Spring

Any web application will need the ability to process request parameters. Spring provide support for direct access to request parameters. But how do we get to the Request object ? Or can we manage without it ?
I took the previous example involving annotations and modified it slightly. I changed the test URL to include a parameter:
<body>
    Redirect application flow from <a href="/SpringMVC-Annotated/anything.do?shva=1">here</a>
</body>
The parameter name, I copied from my gmail account. The next step is to read the parameters form the request. One way to achieve it would be
int shva = Integer.parseInt(request.getParameter("shva"));
But in Spring, we can get this value directly:
@RequestMapping(value = { "/anything.do" })
public String handleRequest(final HttpServletRequest request,
        final HttpServletResponse response, Map<String, Object> model, int shva)
        throws Exception {
    
    System.out.println("name received is " +shva);
    System.out.println("Request object received for method "
            + request.getMethod());
    User user = new User();
    user.setAge(61);
    user.setName("Test user");
    model.put("user", user);
    System.out.println("response content type is not set : "
            + response.getContentType());
    return "data";
}
Spring reads the parameter from the request, cast it to an int and assigns it to the method parameter "shva".
If the name of the parameter is different, then we need to use an annotation to ensure assignment of parameters.
public String handleRequest(final HttpServletRequest request,
        final HttpServletResponse response, Map<String, Object> model, @RequestParam("shva")int val)
        throws Exception {
Can Spring create complete objects for us based on request parameters ?
Consider the below HTML code:
<html>
<body>
    <form action="/SpringMVC-Annotated/login.do" method="post">
        <p>
            User name: <input type="text" name="user" ><br/>
            Password: <input type="password" name="pwd" >
        </p>
        <div>
            <input type="submit" value="Send">
        </div>
    </form>
</body>
</html>
The code is for a login page. Different ways to process the request parameters would be:
  1. Pass the request object as a parameter. Spring will pass it to your controller method and you can read the values from it directly
  2. Use multiple @RequestParam annotations with the method parameters
  3. Use the @ModelAttribute annotation
The third technique is used to bind the parameters with a Java object directly. I created a java class for the same:
public class Login {
    private String user,pwd;
        //setter, getters
}
The code for the controller is as below:
@RequestMapping(value = { "/login.do" }, method = { RequestMethod.POST })
public String handlePostRequest(@ModelAttribute("login")Login login, Map<String, Object> model)
        throws Exception {
    System.out.println("user name is " + login.getUser() + " and pwd is "
            + login.getPwd());
    User user = new User();
    user.setAge(61);
    user.setName(login.getUser());
    model.put("user", user);
    return "data";
}
When the request is made the Spring container binds the method parameter  to the model attribute.For this binding to work it is necessary that Login class have fields with the same name as the form fields. Thus the form has input fields with name user and pwd, as does the Login class.

No comments:

Post a Comment