Loading
 
What's Next: View All Tutorial →

Spring Form Handling using @ModelAttribute bean and CommandName attribute

In this article, you will learn how to handle the form data using @ModelAttribute and commandName attribute @ModelAttribute is used to bind the Bean class(POJO Object) to the form. After binding the form object aware of the bean object value a...

Published Date: 13/12/2014  Last Modified Date: 13/12/2014    New Post





In this article, you will learn how to handle the form data using @ModelAttribute and commandName attribute

@ModelAttribute is used to bind the Bean class(POJO Object) to the form. After binding the form object aware of the bean object value and bind to the field element

On submitting the data, the data from the "form" object will be bounded to the "Bean" object.

commandName is used to bind the "Bean" object to the form

Refer Spring MVC Hello World for base project setup.
 

1. Create Bean class User.java with three fields


User.java (In the package com.tutorial.domain)
 
package com.tutorial.domain;


public class User {

    private String userName;
    private String gender;
    private String ageGroup;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getGender() {
        return gender;
    }

    public void setGender(String gender) {
        this.gender = gender;
    }

    public String getAgeGroup() {
        return ageGroup;
    }

    public void setAgeGroup(String ageGroup) {
        this.ageGroup = ageGroup;
    }

}
 

2. Create a new applicationForm.jsp with some form input fields


ApplicationForm jsp has three form element, to get input from the user

File Path: /Web Pages/WEB-INF/pages/applicationFrom.jsp
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Application Form</h1>
        <form:form name="appFrm" commandName="user" action="${pageContext.servletContext.contextPath}/saveAndViewApplicationData" method="post">
            <table>
                <tr>
                    <td>Name:</td>
                    <td><form:input path="userName"/><td>
                </tr>
                <tr>
                    <td>Gender: </td>
                    <td>
                        <form:select path="gender">
                            <form:option value="M">Male</form:option>
                            <form:option value="F">Female</form:option>                
                        </form:select>
                    <td>
                </tr>
                <tr>
                    <td>Age Group? : </td>
                    <td><form:checkbox path="ageGroup"  value="Less than 30"/> Less than 30
                        <form:checkbox path="ageGroup"  value="Above 30"/> Above 30
                    </td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" value="Submit"/></td>
                </tr>
            </table>

        </form:form>
    </body>
</html>
 

3. Create a new applicationFormData.jsp with some form input fields

ApplicationFormData is to display the data entered by the user

File Path: /Web Pages/WEB-INF/pages/applicationFromData.jsp
 
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h1>Application Form Data</h1>

        <h3>Access ModelAttribute data</h3>
        <!--Note: ModelAttribute bean object can be accessed only inside the  form:form  with the commandName field mapped to it-->

        <form:form name="appFrm" commandName="user" action="${pageContext.servletContext.contextPath}/saveAndViewApplicationData" method="post">
            Name: ${user.userName}<br/>
            Gender: ${user.gender}<br/>
            Age Group : ${user.ageGroup}<br/>
        </form:form>
        <!-- Here user is the reference (defined in commandName) of the User class -->



        <h3>Alternate way of access using $ {param} attribute. $ {param} is similar to request.getParamter()</h3>
        Name: ${param.userName}<br/>
        Gender: ${param.gender}<br/>
        Age Group : ${param.ageGroup}<br/>

    </body>
</html>
 

4. Create a new FormController( java class) 


FormBeanController.java is used to create two requestMapping url

1. "/showApplicationForm" to display the application form to the use to get the data entered by the user
2. "/saveAndViewApplicationData" to reterive the value entered by the user and display in the output page
 
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tutorial.web;

import com.tutorial.domain.User;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class FormBeanController {

    @RequestMapping("/showApplicationForm")
    public String showApplicationForm(@ModelAttribute("user") User user, ModelMap map, HttpServletRequest request) {

        return "applicationForm";
    }

    @RequestMapping("/saveAndViewApplicationData")
    public String saveApplicationForm(ModelMap map, @ModelAttribute("user") User user, HttpServletRequest request) {

        //Using request.getParameter()
        System.out.println("User Name :: " + user.getUserName());
        System.out.println("Gender :: " + user.getGender());
        System.out.println("Age Group :: " + user.getAgeGroup());

        return "applicationFormData";
    }

}
 

5. Output


Hit url http://localhost:8084/SpringMVC/showApplicationForm and input the data and submit


HTML Output:

http://localhost:8084/SpringMVC/saveAndViewApplicationData

Application Form Data

Access ModelAttribute data

Name: John
Gender: M
Age Group : Less than 30

Access using $ {param} attribute

Name: John
Gender: M
Age Group : Less than 30
 

Console Log:

 
User Name :: John
Gender :: M
Age Group :: Less than 30
 

6. Download Link


https://drive.google.com/file/d/0B_9jt9kIlxciSTVIOVhCUlE4d1U/view?usp=sharing
Awaiting for Administrator approval





Tags: Forms Handling Spring MVC

← Back to list


Related Post




×