Different Ways of Form Binding in Spring

1. RequestBody: In this approach incoming http request is binded to the value objects.How this works is when we submit the form spring internally uses the message converters to convert incoming http request into domain objects.Spring provides many message converters. Spring uses jackson mapping message converters by default and this needs to be defined in classpath if we are going with this approach.To implement this need to define method parameter annotated with @RequestBody as like below.
Class LoginController{
@RequestMapping(value = "/loginUser", method = RequestMethod.POST)
 public String showLoginForm(@RequestBody LoginVO user, UriComponentsBuilder ucBuilder) {
2. Model Attribute:In this way in our controller we need to define the VO object when GET request is coming to controller and form is handled and processed when post request arrived to the same controller. Biding result is used to find out is there any  errors in the form submission. Please find the below html code and controller code for the sample. Controller:
Class LoginController{

@RequestMapping(value = "/loginUser", method = RequestMethod.GET)
 public String showLoginForm(Model model) {
  LoginVO user = new LoginVO();
  model.addAttribute("loginForm", user);
@RequestMapping(value = "/loginUser", method = RequestMethod.POST)
public String login(@ModelAttribute("loginForm") LoginVO login,
  BindingResult result, Model model) {
 // Data is available in LoginVO
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

 <form:form method="post" modelAttribute="loginForm" action="/loginUser">
  <form:input path="userName" type="text" /> 
  <form:input path="password" type="text" /> 
  <form:input type="submit" /> 
3. SimpleForm Controller:
In this approach to handle the forms our class should extend the simple form controller,on submit method will be invoked in the controller when we submit the form to url pattern. Lets take the simple example of Login.
First we will create the value object LoginVO as like below.
class LoginVO{
private String userName;
private String password;
//setter and getter is required
Now Lets create the controller and this controller needs to be initialized in spring context.We are not describing all steps how to configure in spring.We completely concentrating on only how to form binding values.
class LoginController extends SimpleFormController{
 protected ModelAndView onSubmit(Object command) throws Exception {
  LoginVO user = (LoginVO ) command;
  return new ModelAndView("userSuccess","user",user);

xml configuration: In this commandClass is the class which is going to bind with this form and commandName is used to map to the html form.
<bean name="login" class="com.LoginController">
            <property name="formView" value="login" />
            <property name="successView" value="success" />
            <property name="commandName" value="loginVO"/>
            <property name="commandClass" value="com.LoginVO"/>
Html :
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<title>Login Page</title>

<form:form method="POST" commandName="LoginForm">
        <td>User Name :</td>
        <td><form:input path="userName" /></td>
        <td>Password :</td>
        <td><form:password path="password" /></td>
        <td colspan="2"><input type="submit"></td>

Now when we submit this form we get form values are binding into user object in LoginController.Since spring 3.0 SimpleFormControllers are deprecated.


Post a Comment

Powered by Blogger.