Data Biding

Request parameters can be bound with method parameters or a Form bean object in a Controller.

Controller uses

  1. annotation @RequestParam to bind request parameters with method arguments.

  2. annotation @ModelAttribute to bind request parameters to a bean object and store bean into Model object.

Figure: request parameter binding

Method Parameter Binding

Annotation @RequestParam to bind request parameters with method arguments.

Here is example code that binds a "message" request parameters to method parameter "message".

This is equivalent to String message = request.getParameter("message");

@RequestMapping(value = "/param", method = RequestMethod.GET)

public String requestParam(@RequestParam String message) {

System.out.println("Request Parameter : " + message);

return "UrlMappingView";

}

See

Above code binds request parameter named "message" to the method parameter named "message". Request parameter's name can be passed to annotation as parameter. Above method signature can be rewritten as

public String requestParam(@RequestParam("message") String message) {..}

Different Names

If request parameter name and method parameter name is different then you can write annotation as

public String requestParam(@RequestParam("msg") String message) {..}

Here request parameter named "msg" will be bound with method parameter named "message".

Type Conversion

If type of method parameter is other than String then type conversion will be automatically done by Spring MVC.

public String requestParam(@RequestParam("id") long id) {..}

Here "id" request parameter will be converted into long and stored in "id" method parameter.

Optional Parameter

By default request parameter is required. If it is not sent in request then you will get an exception. To make it optional you can set "required" attribute of @RequestParam to false.

@RequestParam(value="id", required=false)).

Form bean Binding

Request parameters can be populated into a Model attribute form-bean using @ModelAttribute annotation. This concept is called Data Binding in Spring MVC.

Here is the example LoginSimpleForm bean that is populated from request parameters in the controller FormBindingCtl. Form bean is further bind to HTML form of LoginBindingView.jsp.

Form bean

It is simple POJO that has attributes and its getter and setter methods.

public class LoginSimpleForm {

private String login = null;

private String password = null;

private String message = null;

private String operation = null;

public String getLogin() {

return login;

}

public void setLogin(String login) {

this.login = login;

}

//Other accessor

}

Controller

Populates Request parameters into LoginSimpleForm bean and make it Model attribute named "loginForm".

public class FormBindingCtl {

@RequestMapping(value = "/login", method = RequestMethod.GET)

public String loginDisplay(@ModelAttribute("loginForm") LoginSimpleForm form, Model model) {

return "LoginBindingView";

}

@RequestMapping(value = "/login", method = RequestMethod.POST)

public String loginSubmit(@ModelAttribute("loginForm") LoginSimpleForm form, Model model) {

String message = "You entered Login: " + form.getLogin()

+ " and Password: " + form.getPassword();

form.setMessage(message);

return "LoginBindingView";

}

...

HTML form biding in View

Spring tag f:form and commandName="loginForm" is used to bind model attribute "loginForm" to HTML form elements.

<BODY>

<H1>Login Form</H1>

<H3 style="color: green">${loginForm.message }</H3>

<f:form action="../FormBinding/login" method="post" commandName="loginForm">

<table>

<tr>

<td>Login</td>

<td><f:input path="login" /></td>

</tr>

<tr>

<td>Password</td>

<td><f:password path="password" /></td>

</tr>

<tr>

<td colspan="2"><input type="submit" value="SignIn"

name="operation"> | <input type="submit" value="SignUp"

name="operation"></td>

</tr>

</table>

</f:form>

</BODY>