Spring Framework FORM Tags

Spring FORM Tag Library

Spring MVC provides a JSP tag library (Spring Form) for making it easier to bind form elements to Model data. Spring Framework also provides you with some tags for evaluating errors, setting themes and outputting internationalized messages.

also read:

Syntax to use Spring Form tag library

	<%@taglib  uri="http://www.springframework.org/tags/form" prefix="form">

Form tags used in this example

  • Renders an HTML ‘form’ tag and exposes a binding path to inner tags for binding.
  • Renders an HTML ‘input’ tag with type ‘text’ using the bound value.
  • Renders field errors in an HTML ‘span’ tag.
  • Renders an HTML ‘input’ tag with type ‘password’ using the bound value.
  • Renders an HTML ‘input’ tag with type ‘radio’.
  • Renders an HTML ‘select’ element. Supports databinding to the selected option.
  • Renders a single HTML ‘option’. Sets ‘selected’ as appropriate based on bound value.
  • Renders an HTML ‘textarea’.
  • Renders an HTML ‘input’ tag with type ‘checkbox’.

Simple Registration Form Example

1.Modify the web.xml to configure the Dispatcher Servlet.

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

2.Create an dispatcher-servlet.xml file which contains all the configuration beans to handle the user requests.It handles the user request and dispatches to respective controllers.


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
       http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

    <bean class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping"/>

    <bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
      <property name="urlMap">
<entry  key="/index.html">
<ref bean="registrationController"/>


<bean id="registrationValidator" class="registrationValidator"/>
<bean id="registrationController" class="RegistrationFormController">

<property name="sessionForm"><value>false</value></property>

<property name="commandName" value="registration"></property>

<property name="commandClass" value="Registration"></property>

<property name="validator"><ref bean="registrationValidator"/></property>
<property name="formView"><value>index</value></property>

<property name="successView"><value>success</value></property>


<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
        <property name="basename" value="messages"/>

     <bean id="viewResolver"
          p:suffix=".jsp" />

3.Create a Jsp file for taking input from the user index.jsp which contains all the form fields with Spring Form tags.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib  uri="http://www.springframework.org/tags/form" prefix="form" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Welcome to Spring Web MVC project</title>

    <body bgcolor="DDDDDD">
        <h1>Spring Form tags Example</h1>
        <form:form commandName="registration" method="POST">

           User Name: <form:input path="username"/><font color="red">
               <form:errors path="username"/></font><br />
           Password: <form:password path="password"/><font color="red">
           <form:errors path="password"/></font><br />
           First Name: <form:input path="fname"/><br />
           Last Name: <form:input path="lname"/><br />
           Gender: <form:radiobutton path="gender" value="male"/>Male
               <form:radiobutton path="gender" value="female"/>Female<br />
                   Country :<form:select path="country" >
                       <form:option value="india">India</form:option>
                       <form:option value="india">USA</form:option>
                       <form:option value="india">Australia</form:option>
                   </form:select><br />
                   Address: <form:textarea path="addr"/><br />
                       Select any :<form:checkbox path="cb" value="checkbox1"/>
                       Check Box1
                       <form:checkbox path="cb" value="checkbox2"/>
                       Check Box2<br />

            <input type="submit" value="submit"/>


4.Create another Jsp file success.jsp which is a View for Spring to display the output. In this file we use Expression Language to display the details.


<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@page import="java.util.Enumeration"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"

        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>Spring Form Tags Example</title>
    <body bgcolor="DDDDDD">
        <h1>Spring Form tags examples</h1>
         User Name: ${uname}<br />
         First Name: ${fname}<br />
         Last Name: ${lname}<br />
         Gender: ${gender}<br />
         Country: ${country}<br />
         Address: ${addr}<br />
         Selected Check box: ${cb}


5.Create a Java class file Registration.java which contains the business logic for registration application. Here this file contains 8 private variables with their respective getter and setter methods to store the details for registration.


public class Registration {

    private String username;
    private String password;
    private String fname;
    private String lname;
    private String gender;
    private String country;
    private String addr;
    private String cb;

    public String getAddr() {
        return addr;

    public void setAddr(String addr) {
        this.addr = addr;

    public String getCb() {
        return cb;

    public void setCb(String cb) {
        this.cb = cb;

    public String getCountry() {
        return country;

    public void setCountry(String country) {
        this.country = country;

    public String getGender() {
        return gender;

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

    public Registration() {

    public String getFname() {
        return fname;

    public void setFname(String fname) {
        this.fname = fname;

    public String getLname() {
        return lname;

    public void setLname(String lname) {
        this.lname = lname;

    public String getPassword() {
        return password;

    public void setPassword(String password) {
        this.password = password;

    public String getUsername() {
        return username;

    public void setUsername(String username) {
        this.username = username;


6.Create a RegistrationFormController.java file which extends SimpleFormController to control the user request and return respective ModelAndView object.

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

public class RegistrationFormController extends SimpleFormController {

    protected ModelAndView onSubmit(Object command) throws Exception {

        Registration reg=(Registration)command;

        String uname=reg.getUsername();
        String fname=reg.getFname();
        String lname=reg.getLname();

        String gender=reg.getGender();
        String country=reg.getCountry();
        String cb=reg.getCb();
        String addr=reg.getAddr();

         ModelAndView mv = new ModelAndView(getSuccessView());


        return mv;


7.Create a registrationValidator.java file to validate the form fields like username and password should not be empty.DispatcherServlet is responsible to give property to add Validator to the user request and perform validation.


import org.springframework.validation.Errors;
import org.springframework.validation.Validator;

public class registrationValidator implements Validator

    public boolean supports(Class cl) {
        return Registration.class.isAssignableFrom(cl);


    public void validate(Object ob, Errors errors) {
       Registration reg=(Registration)ob;
       if (reg.getUsername() == null || reg.getUsername().length() == 0) {
            errors.rejectValue("username", "error.empty.username");

       else if (reg.getPassword() == null || reg.getPassword().length() == 0) {
            errors.rejectValue("password", "error.empty.password");



8.Create or Modify messages.properties file which contains the messages for their respective keys.In this file we write messages for two keys empty username and empty password.

error.empty.username=Please Enter User name
error.empty.password=Please Enter Password

9.Building and running the application


Access page:http://localhost:8080/Registration/index.html
When password is emtpy respective error message is displayed using spring errors tag
We can submit the form only after the correct validation of username and password.
Output is displayed with all the values submitted for registration

also read:

Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!