Spring MVC form handling example

In Spring MVC, form controller is used to handle the form submission task (binding values to/from Spring’s form tags). Often times, you may just need to extend the SimpleFormController to inherit the form handling ability.

1. Binding Values

In SimpleFormController, you need to tell Spring about how form controller will binds the value to/from the HTML form’s components. See code snippet :

public class YourController extends SimpleFormController{
	//...
	public CustomerController(){
		setCommandClass(Customer.class);
		setCommandName("customerForm");
	}
  1. setCommandClass(Customer.class) – Form’s values will store into this Customer object.
  2. setCommandName(“customerForm”) – If HTML form action value with named “customerForm” is submitted, Spring will forward request to this form controller.

2. Form submission

When HTML form is submitted, form controller’s onSubmit() method will be executed, to handle the submitted form’s value and return a ModelAndView if success. So, onSubmit() method is the ideal place to perform form submission business logic.

public class YourController extends SimpleFormController{
	//...
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
		HttpServletResponse response, Object command, BindException errors)
		throws Exception {
 
		Customer customer = (Customer)command;
		System.out.println(customer);
		return new ModelAndView("CustomerSuccess","customer",customer);
	}

3. Initialization

In some cases, you may need to initialize the form component values before render the page, like default value for HTML form component. To do this, just override the formBackingObject() method.

public class YourController extends SimpleFormController{
	//...
	@Override
	protected Object formBackingObject(HttpServletRequest request)
		throws Exception {
 
		Customer cust = new Customer();
		cust.setFavFramework(new String []{"Spring MVC"});
		return cust;
	}
Note
This technique is useful to set a default selected value for HTML components, like checkbox , radio button or select options.

4. Data Referencing

Provide a list of data for HTML components, like checkbox , radio button or select options. To do this, just override the referenceData() method.

public class YourController extends SimpleFormController{
	//...
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map referenceData = new HashMap();
		List<String> webFrameworkList = new ArrayList<String>();
		webFrameworkList.add("Spring MVC");
		webFrameworkList.add("Struts 1");
		webFrameworkList.add("Struts 2");
		referenceData.put("webFrameworkList", webFrameworkList);
		return referenceData;
	}

5. SimpleFormController bean

Finally, declares the SimpleFormController bean and override the “formView” and “successView” property value.

  1. formView – Indicate the form page that will shows to the user. If validation failed, the controller will return this page to the user as well.
  2. successView – If the form is submitted successful, return this page to the user.

With below InternalResourceViewResolver declared :

	<bean class="com.mkyong.customer.controller.CustomerController">
		<property name="formView" value="CustomerForm" />
		<property name="successView" value="CustomerSuccess" />
	</bean>
 
	<bean id="viewResolver"
             class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
             <property name="prefix">
                 <value>/WEB-INF/pages/</value>
             </property>
             <property name="suffix">
                 <value>.jsp</value>
             </property>
       </bean>
  1. “formView” will redirect to “/WEB-INF/pages/CustomerForm.jsp”.
  2. “successView” will redirect to “/WEB-INF/pages/CustomerSuccess.jsp”.

Spring MVC form handling example

In this example, it will use the Spring’s form tags to render the textbox, password, textarea, checkbox, radio button, select option and hidden value field. And all the submitted form values will be handle by the SimpleFormController. Additionally, attach a simple validator to do the empty checking on the HTML form components, if the validation failed, use the form’s error tag to output the error message associated with the field. By studying this example, you should have a very good understanding of the form handling in Spring MVC.

1. Model

A Customer model object for the demonstration.

File : Customer.java

package com.mkyong.customer.model;
 
public class Customer{
 
	//textbox
	String userName;
 
	//textarea
	String address;
 
	//password
	String password;
	String confirmPassword;
 
	//checkbox
	boolean receiveNewsletter;
	String [] favFramework;
 
	//radio button
	String favNumber;
	String sex;
 
	//dropdown box
	String country;
	String javaSkills;
 
	//hidden value
	String secretValue;
 
	//getter and setter methods
}

2. Form Controller

A SimpleFormController class to handle the form submission.

File : CustomerController.java

package com.mkyong.customer.controller;
 
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.mkyong.customer.model.Customer;
 
public class CustomerController extends SimpleFormController{
 
	public CustomerController(){
		setCommandClass(Customer.class);
		setCommandName("customerForm");
	}
 
	@Override
	protected ModelAndView onSubmit(HttpServletRequest request,
		HttpServletResponse response, Object command, BindException errors)
		throws Exception {
 
		Customer customer = (Customer)command;
		System.out.println(customer);
		return new ModelAndView("CustomerSuccess","customer",customer);
 
	}
 
	@Override
	protected Object formBackingObject(HttpServletRequest request)
		throws Exception {
 
		Customer cust = new Customer();
		//Make "Spring MVC" as default checked value
		cust.setFavFramework(new String []{"Spring MVC"});
 
		//Make "Make" as default radio button selected value
		cust.setSex("M");
 
		//make "Hibernate" as the default java skills selection
		cust.setJavaSkills("Hibernate");
 
		//initilize a hidden value
		cust.setSecretValue("I'm hidden value");
 
		return cust;
	}
 
	@Override
	protected Map referenceData(HttpServletRequest request) throws Exception {
 
		Map referenceData = new HashMap();
 
		//Data referencing for web framework checkboxes
		List<String> webFrameworkList = new ArrayList<String>();
		webFrameworkList.add("Spring MVC");
		webFrameworkList.add("Struts 1");
		webFrameworkList.add("Struts 2");
		webFrameworkList.add("JSF");
		webFrameworkList.add("Apache Wicket");
		referenceData.put("webFrameworkList", webFrameworkList);
 
		//Data referencing for number radiobuttons
		List<String> numberList = new ArrayList<String>();
		numberList.add("Number 1");
		numberList.add("Number 2");
		numberList.add("Number 3");
		numberList.add("Number 4");
		numberList.add("Number 5");
		referenceData.put("numberList", numberList);
 
		//Data referencing for country dropdown box
		Map<String,String> country = new LinkedHashMap<String,String>();
		country.put("US", "United Stated");
		country.put("CHINA", "China");
		country.put("SG", "Singapore");
		country.put("MY", "Malaysia");
		referenceData.put("countryList", country);
 
		//Data referencing for java skills list box
		Map<String,String> javaSkill = new LinkedHashMap<String,String>();
		javaSkill.put("Hibernate", "Hibernate");
		javaSkill.put("Spring", "Spring");
		javaSkill.put("Apache Wicket", "Apache Wicket");
		javaSkill.put("Struts", "Struts");
		referenceData.put("javaSkillsList", javaSkill);
 
		return referenceData;
	}
}

3. Form Validator

A form validator class to do the empty checking on the HTML components. If validation failed, get the error message from resource bundle, and associate it with the corresponds error field.

Note
In form views, you can use the <form:errors /> to renders and format the error message that is associated with the field.
package com.mkyong.customer.validator;
 
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;
import com.mkyong.customer.model.Customer;
 
public class CustomerValidator implements Validator{
 
	@Override
	public boolean supports(Class clazz) {
		//just validate the Customer instances
		return Customer.class.isAssignableFrom(clazz);
	}
 
	@Override
	public void validate(Object target, Errors errors) {
 
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "userName",
			"required.userName", "Field name is required.");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "address",
			"required.address", "Field name is required.");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "password",
			"required.password", "Field name is required.");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "confirmPassword",
			"required.confirmPassword", "Field name is required.");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "sex", 
			"required.sex", "Field name is required.");
		ValidationUtils.rejectIfEmptyOrWhitespace(errors, "favNumber", 
			"required.favNumber", "Field name is required.");
		ValidationUtils.rejectIfEmptyOrWhitespace(
			errors, "javaSkills", "required.javaSkills","Field name is required.");
 
		Customer cust = (Customer)target;
 
		if(!(cust.getPassword().equals(cust.getConfirmPassword()))){
			errors.rejectValue("password", "notmatch.password");
		}
		if(cust.getFavFramework().length==0){
			errors.rejectValue("favFramework", "required.favFrameworks");
		}
		if("NONE".equals(cust.getCountry())){
			errors.rejectValue("country", "required.country");
		}
	}
}

A properties file to store all the error messages.

File : Customer.properties

required.userName = Username is required!
required.address = Address is required!
required.password = Password is required!
required.confirmPassword = Confirm password is required!
required.favFrameworks = Please select at least a web frameworks!
required.sex = Please select a sex!
required.favNumber = Please select a number!
notmatch.password = Password and Conform password is not match!

4. View

This is the “formView“, render the HTML components with Spring’s form tags, display and highlight the error message if any.

File : CustomerForm.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<style>
.error {
	color: #ff0000;
}
 
.errorblock {
	color: #000;
	background-color: #ffEEEE;
	border: 3px solid #ff0000;
	padding: 8px;
	margin: 16px;
}
</style>
</head>
<body>
	<h2>Spring's form tags example</h2>
 
	<form:form method="POST" commandName="customerForm">
		<form:errors path="*" cssClass="errorblock" element="div" />
		<table>
			<tr>
				<td>UserName :</td>
				<td><form:input path="userName" />
				</td>
				<td><form:errors path="userName" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td>Address :</td>
				<td><form:textarea path="address" />
				</td>
				<td><form:errors path="address" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td>Password :</td>
				<td><form:password path="password" />
				</td>
				<td><form:errors path="password" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td>Confirm Password :</td>
				<td><form:password path="confirmPassword" />
				</td>
				<td><form:errors path="confirmPassword" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td>Subscribe to newsletter? :</td>
				<td><form:checkbox path="receiveNewsletter" />
				</td>
				<td><form:errors path="receiveNewsletter" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td>Favourite Web Frameworks :</td>
				<td><form:checkboxes items="${webFrameworkList}"
					path="favFramework" /></td>
				<td><form:errors path="favFramework" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td>Sex :</td>
				<td><form:radiobutton path="sex" value="M" />Male 
                                       <form:radiobutton path="sex" value="F" />Female</td>
				<td><form:errors path="sex" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td>Choose a number :</td>
				<td><form:radiobuttons path="favNumber" items="${numberList}" />
				</td>
				<td><form:errors path="favNumber" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td>Country :</td>
				<td><form:select path="country">
					   <form:option value="NONE" label="--- Select ---" />
					   <form:options items="${countryList}" />
					</form:select></td>
				<td><form:errors path="country" cssClass="error" />
				</td>
			</tr>
			<tr>
				<td>Java Skills :</td>
				<td><form:select path="javaSkills" items="${javaSkillsList}"
					multiple="true" /></td>
				<td><form:errors path="javaSkills" cssClass="error" />
				</td>
			</tr>
			<form:hidden path="secretValue" />
			<tr>
				<td colspan="3"><input type="submit" /></td>
			</tr>
		</table>
	</form:form>
 
</body>
</html>

This is the “successView“, renders all the submitted values.

Fie : CustomerSuccess.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
 
<html>
<body>
	<h2>Spring's form tags example</h2>
 
	<table>
		<tr>
			<td>UserName :</td>
			<td>${customer.userName}</td>
		</tr>
		<tr>
			<td>Address :</td>
			<td>${customer.address}</td>
		</tr>
		<tr>
			<td>Password :</td>
			<td>${customer.password}</td>
		</tr>
		<tr>
			<td>Confirm Password :</td>
			<td>${customer.confirmPassword}</td>
		</tr>
		<tr>
			<td>Receive Newsletter :</td>
			<td>${customer.receiveNewsletter}</td>
		</tr>
		<tr>
			<td>Favourite Web Frameworks :</td>
			<td><c:forEach items="${customer.favFramework}" var="current">
				   [<c:out value="${current}" />]
				</c:forEach>
			</td>
		</tr>
		<tr>
			<td>Sex :</td>
			<td>${customer.sex}</td>
		</tr>
		<tr>
			<td>Favourite Number :</td>
			<td>${customer.favNumber}</td>
		</tr>
		<td>Java Skills :</td>
		<td>${customer.javaSkills}</td>
		</tr>
		<tr>
			<td>Hidden Value :</td>
			<td>${customer.secretValue}</td>
		</tr>
		<tr>
			<td>Hidden Value :</td>
			<td>${customer.secretValue}</td>
		</tr>
	</table>
 
</body>
</html>

5. Spring Bean Configuration

Finally, link the properties file, validator and form controller together.

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
 
  <bean 
  class="org.springframework.web.servlet.mvc.support.ControllerClassNameHandlerMapping" />
 
  <bean class="com.mkyong.customer.controller.CustomerController">
	<property name="formView" value="CustomerForm" />
	<property name="successView" value="CustomerSuccess" />
 
	<!-- Map a validator -->
	<property name="validator">
		<bean class="com.mkyong.customer.validator.CustomerValidator" />
	</property>
  </bean>
 
  <!-- Register the Customer.properties -->
  <bean id="messageSource"
	class="org.springframework.context.support.ResourceBundleMessageSource">
	<property name="basename" value="com/mkyong/customer/properties/Customer" />
  </bean>
 
  <bean id="viewResolver"
       class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
       <property name="prefix">
             <value>/WEB-INF/pages/</value>
        </property>
        <property name="suffix">
             <value>.jsp</value>
        </property>
  </bean>
</beans>

6. Demo

Access the page – http://localhost:8080/SpringMVC/customer.htm

SpringMVC-Form-Handle-Example-1

If the user failed the validation, display and highlight the error message.

SpringMVC-Form-Handle-Example-2

If the form is submitted successfully, just display all the submitted values.

SpringMVC-Form-Handle-Example-3

Download Source Code

References

  1. http://static.springsource.org/spring/docs/2.5.x/reference/mvc.html
  2. http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/web/servlet/mvc/SimpleFormController.html
  3. http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/validation/Validator.html
  4. http://static.springsource.org/spring/docs/2.5.x/api/org/springframework/validation/ValidationUtils.html
  5. http://static.springsource.org/spring/docs/2.5.x/reference/validation.html
Tags :

About the Author

mkyong
Founder of Mkyong.com and HostingCompass.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

  • http://www.viwawa.com/profile/relish75candle Aileen

    Excellent site you have got here.. It’s difficult to find high quality writing like yours nowadays. I honestly appreciate individuals like you! Take care!!

  • Saran

    Hi..

    i have executed the above code.it was executed..
    but i got a small problem. when i haven’t select any country it not showing any error message.gives an exception.
    ie: SEVERE: No message found under code ‘required.country’ for locale ‘en_US’.
    org.springframework.context.NoSuchMessageException: No message found under code ‘required.country’ for locale ‘en_US’.

    please resolve it.

    • Tom

      Hi,

      Adding the following line to Customer.properties solves this problem:-

      required.country = Please select a country!

  • http://barge31print.wordpress.com/2013/05/25/talk-nasty-watching-cute-models-through-free-online-live-sex-cams/ free naked chat

    I love it whenever people get together and share opinions.
    Great website, continue the good work!

  • Jehan Jaleel

    Hi,

    I really benefitted from this tutorial. Just one question, when you access the form with customer.htm in your url how does Spring know to link this url with your JSP?

  • http://elmomah.com/social/blogs/801/1082/decent-couples-live-sex-cams-bas Jacquelyn

    Heya i’m for the primary time here. I came across this board and I in finding It truly helpful & it helped me out much. I am hoping to present something back and aid others such as you aided me.

  • http://alegakicupebridges.blogbaker.com/2013/03/16/free-online-cams-will-be-rapidly-experienced-within-this-internet-site alegakicupebridges.blogbaker.com

    Hello would you mind stating which blog platform you’re working with? I’m
    planning to start my own blog in the near future but I’m having a difficult time choosing between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for
    something unique. P.S My apologies for getting off-topic but I had to
    ask!

  • Volodia

    Hello!

    I met the following problem when deploying to tomcat
    java.lang.NoSuchMethodError: org.springframework.core.GenericTypeResolver.resolveTypeArguments(Ljava/lang/Class;Ljava/lang/Class;)[Ljava/lang/Class;

    I resolved it by removing this dependency

                &lt;groupId&gt;org.springframework&lt;/groupId&gt;
                &lt;artifactId&gt;spring&lt;/artifactId&gt;

    Instead I put this:

                org.springframework
                spring-core
    
  • Haris

    hi Mkyong,

    Thanks for the tutorial.

    can you please explain me the difference between the formbackingobject() vs referencedate()

  • Javix

    I have some html controls in my page, they have the same ID, how can I generate something similar to to each control?

    <input type="text" id="mycontrol” />

    Any idea?

    thanks!

  • Vikas

    thanks for nice explanation.

  • Mahender

    Nice explanation sir

  • pradeep

    Sir i am new one for spring. Some xml files are not supported in Google App Engine so what i do.

  • Kamel HADDAD

    the class SimpleFormController was deprecated in the new Spring version, kindly did you know which class that we can use in the place of the deprecated one.

  • Suresh

    Is there Any example for MultiAction COntroller Form Validation

  • Chandu

    Thank you very much yong. It’s really nice.

  • Khaing

    Thank you for your step by step example. For the edit, how to set up bean configuration? I found all the examples are for the add and I have not found for edit yet. Could you guide me? I tried with as below but not working and it is showing error.

  • czarny

    Hi,

    Why do you use package access for properties in Customer.java instead of private ones?

    Regards

  • http://[email protected] [email protected]

    Good example , unfortunately its not working for me, no meter what i do , the customer form jsp just not loaded :”Oops! This link appears to be broken.”

  • Arthur

    Example is good! It was usefully =)
    I`m not sure, but if you didn`t added this string in pom.xml

    	&lt;build&gt;
    		&lt;finalName&gt;SpringMVC&lt;/finalName&gt;
    		&lt;plugins&gt;
    			&lt;plugin&gt;
    				&lt;artifactId&gt;maven-compiler-plugin&lt;/artifactId&gt;
    				&lt;configuration&gt;
    					&lt;source&gt;1.5&lt;/source&gt;
    					&lt;target&gt;1.5&lt;/target&gt;
    				&lt;/configuration&gt;
    			&lt;/plugin&gt;
    		&lt;/plugins&gt;
    	&lt;/build&gt;

    You will get: SpringMVCForm/src/main/java/com/mkyong/customer/controller/CustomerController.java:[24,2] error: annotations are not supported in -source 1.3
    and
    /SpringMVCForm/src/main/java/com/mkyong/customer/controller/CustomerController.java:[61,6] error: generics are not supported in -source 1.3

    Somebody can explain this?

    • http://www.mkyong.com mkyong

      Maven uses JDK 1.3 to compile, which is not support annotation yet. So, your configuration is right, thanks for your input.

  • Manasi

    A thorough A to Z steps explaining a SimpleFormController
    Real BIG help
    Thanks Buddy

  • Srinivas.G

    Thanks for nice explanation….

  • Manohar

    Hi Mkyong,
    Im newbie for Springs.,
    I implemented same form example, but formBackingObject method gets called twice.
    Dnt know y.
    Please can u guide me ?
    Thanks in advance.

  • Rady

    Hi,

    this example need to modify 2 files to make it work.

    – file at SpringMVCForm\.classpath

    	&lt;classpathentry kind=&quot;var&quot; path=&quot;M2_REPO/javax/servlet/javax.servlet-api/3.0.1/javax.servlet-api-3.0.1.jar&quot; sourcepath=&quot;M2_REPO/javax/servlet/javax.servlet-api/3.0.1/javax.servlet-api-3.0.1.jar&quot;&gt;
    		&lt;attributes&gt;
    			&lt;attribute name=&quot;org.eclipse.jst.component.dependency&quot; value=&quot;/WEB-INF/lib&quot;/&gt;
    		&lt;/attributes&gt;
    	&lt;/classpathentry&gt;

    – file at SpringMVCForm\.settings\org.eclipse.wst.common.project.facet.core.xml

    	&lt;installed facet=&quot;jst.java&quot; version=&quot;1.6&quot;/&gt;
    • Rady

      add more:

          &lt;dependency&gt;
              &lt;groupId&gt;javax.servlet&lt;/groupId&gt;
              &lt;artifactId&gt;javax.servlet-api&lt;/artifactId&gt;
              &lt;version&gt;3.0.1&lt;/version&gt;
          &lt;/dependency&gt;
  • Bona

    what a clear explanation!!!!!!

  • olsi

    i download and run on my maven eclipse but i get a http error 404. The webpage cannot be found. i emphasize its a helpful article.

  • Jenni

    Good one. Thanks for this tutorial.
    Please keep writing more

  • Levan

    Once again thanks a lot for the nice and clear tutorial!

  • Sudhakar

    Dear friend,
    I really impressed by this website.
    Good work buddy.

  • Kai

    Mkyong –
    Thank you for such a wonderful, lightweight and easy to follow set of tutorials. I am new to Spring and I find your tutorials much easier to work with the the Spring reference manual.
    I do have one question – is there an easy way of populating the ‘referencedata’ map dynamically via a properties file or XML? I’m just thinking it would be good to be able to change these values without re-building and re-deploying the app. I appreciate I could write some java code to do it via a Properties object but surely something already exists ‘out of the box’?
    Thanks again,
    Kai

  • Vaibhav

    One of the best examples i have ever seen on Spring MVC. Great Work. Keep it up!

  • sriram

    Hi,
    I downloaded this code and when i try to access customer.htm, it gives me

    org.springframework.web.servlet.DispatcherServlet noHandlerFound

    WARNING: No mapping found for HTTP request with URI [/SpringMkyong/customer.htm] in DispatcherServlet with name ‘dispatcher’

    Why i am getting like this? i googled it but if of no use.

    • http://www.mkyong.com mkyong

      Did you build with Maven to get all project dependencies?

  • csguy

    hi mkyong,

    I would like to ask how to retain the list value in referenceData() in the submission pass the validate checking and throw error in onSubmit method?

    if there is error in onSubmit method i try to forward to back the same and show errors but the reference data is missing e.g. list options in selection box becomes empty.

  • Nishant

    Thanks dude you have given best description for spring
    Keep up the good work

    • Simple

      hi mkyong,
      my english is not good, thank you !!!

  • VenkataPrasad

    I tried this example successfully. I later tied a step ahead where I submit in page1(jsp page) then instead of displaying in page2(jsp), I have comeup with a form in pag2 where user inputs data. Page1 has a command class(mentioned in jsp and bean definition(commandclass and command name)), page2 has a different command class, with a different commandclassName. As I click submit on page 1, instead of displaying page2, it is giving the error
    “Neither BindingResult nor plain target object for bean name ‘customerForm’ available as request attribute”

  • http://www.internetworkconsulting.net Shawn Z

    Cutting and pasting that does not work in MVC3. To get the extension to work, I had to create a class file:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Mvc;

    namespace incMvcSite.Classes {
    public static class HtmlPrefixScopeExtensions {
    public static IDisposable BeginHtmlFieldPrefixScope(this HtmlHelper html, string htmlFieldPrefix) {
    return new HtmlFieldPrefixScope(html.ViewData.TemplateInfo, htmlFieldPrefix);
    }

    private class HtmlFieldPrefixScope : IDisposable {
    private readonly TemplateInfo templateInfo;
    private readonly string previousHtmlFieldPrefix;

    public HtmlFieldPrefixScope(TemplateInfo templateInfo, string htmlFieldPrefix) {
    this.templateInfo = templateInfo;

    previousHtmlFieldPrefix = templateInfo.HtmlFieldPrefix;
    templateInfo.HtmlFieldPrefix = htmlFieldPrefix;
    }

    public void Dispose() {
    templateInfo.HtmlFieldPrefix = previousHtmlFieldPrefix;
    }
    }
    }
    }
    In the Razor (.cshtml) file, I added the following:
    @using incMvcSite.Classes
    @using(Html.BeginHtmlFieldPrefixScope(“Permission”)) {

    Permission

    // The Html.EditorFor’s would go here…

    }
    Notice the using to bring me extension class into scope. That allows the second using line to work.
    Now the problem is that when posting back, the object is not updated. In my controller, I used a second parameter to specify my prefix:
    TryUpdateModel(modelUser.Permission, “Permission”);
    This added the prefix to all field in the HTML, and the TryUpdateModel loaded the object with prefixed control names. Now you can properly namespace your controls for embedded edit lists, and for partial views of models with the same property names.

    Shawn Zernik
    Internetwork Consulting

    • http://www.mkyong.com mkyong

      Thanks for your invaluable comment, your code is .NET, right? Not familiar with .NET MVC, is this similar with Spring MVC?

  • JC

    What Java compiler do you use? Got error as below:
    Description Resource Path Location Type
    Java compiler level does not match the version of the installed Java project facet. SpringMVCForm Unknown Faceted Project Problem (Java Version Mismatch)

    • http://www.mkyong.com mkyong

      I should be using jdk1.6 to compile this example.

      • JC

        I tried JDK 1.5 and 1.6; both got the same error. BTW, I used MyEClipse as IDE. Does that make difference?
        Thanks.

        • http://www.mkyong.com mkyong

          Then get a Eclipse and test :) . This example is tested under Eclipse 3.6 + Maven 2.x + JDK1.6

  • Terry Jarrard

    This was a great example. Probably the best I’ve seen so far!

    Thanks so much for taking the time to do this. one question though. I’ve not seen any examples where there are form inputs and data from other pages all on the same page.

    for example. If I entered a date on one page. Continue on in the process, and on a later page, where I’m asking for more data (from form inputs) display the dates from the previous step.

    Or using your example here, If you had the username entered on a separate page, and then on the customer page displayed that name and had the inputs for the rest of the data needed.
    Thanks for any help you can give.

  • thanksalot

    Thank you for a wonderful article.. very much helpful for noobs like me…

    • http://www.mkyong.com mkyong

      welcome, i’m noobs also, we all learn through the process :)

  • http://www.gmail.com praveenkumar reddy

    how to provide the default value in the text field using form:input tag in spring

  • http://www.gmail.com praveenkumar reddy

    how to provide the default value in the text field using tag

  • JJ

    Hi,
    For a late learner your identical examples of using annotations and doing the same without have been a lifesaver.

    I hope you keep writing!

    • http://www.mkyong.com mkyong

      Thanks for your kind comment, and good to know it did help someone :)

  • Tiny

    Where have you marked the welcome page to be customer.htm?Nothing is mentioned about customer.htm in your application.

    http://localhost:8080/SpringMVC/customer.htm

    • http://www.mkyong.com mkyong

      If URL “customer” pattern is requested, it will send to “CustomerController” controller to handle the request.

      You may interest at this article
      http://www.mkyong.com/spring-mvc/spring-mvc-hello-world-example/

      • Alan

        But you havent declared a bean name “/customer.htm” and map it to CustomerController class in the above example. Then how is it possible if an URL with “/customer.htm” pattern is requested, it will send to CustomerController controller to handle the request?

        • http://www.mkyong.com mkyong

          Please check the Spring MVC hello world example in this link http://www.mkyong.com/spring-mvc/spring-mvc-hello-world-example/

          In web.xml, you can map all the “.htm” file extension to Spring. So, if “customer.htm” is requested, Spring will assign to CustomerController.java automatically.

          • Nagendra

            If the request from /customer.htm goes to the controller class then why CustomerForm.jsp file is used here in this example ? confused. A html page cannot use spring form library. Please clarify.

          • Tom

            The form view name is defined in the mvc-dispatcher-servlet.xml as a property of the CustomerController bean:-

          • Jolly

            You did not answer his question. You keep on saying refer to the HelloWorld example. But this is a different example and it is not the HelloWorld example. The guy is asking how come customer.htm works even it is not defined in the dispatcher servlet’s “servlet.xml” file. Please don’t answer just by referring. Answer the damn fucking question. Your giving a lot of people headache for Fucks sake!

  • Pingback: [Guida] Validazione con Spring 3 « Diegoitaliait's Blog()

  • Pingback: Spring MVC form handling annotation example()

  • Pingback: Spring MVC – Neither BindingResult nor plain target object for bean name ‘xxx’ available as request attribute.()

  • Pingback: Spring MVC file upload example()

  • Pingback: Spring MVC hello world annotation example()

  • Pingback: Spring MVC handling multipage forms with AbstractWizardFormController()

  • http://www.pcornelissen.de Patrick

    Nice Blog Post, but why didn’t you use a single annotation?
    They make life so much easier and you don’t have to use subclassing for the controllers etc. and you can unittest it much better.

    • http://www.mkyong.com mkyong

      Hi, thanks for your suggestion, will create a form handling example in annotation version soon :)

      • Vijay

        Any progress?

  • Pingback: Handling duplicate form submission in Spring MVC()