Spring MVC Fast Tutorial: Form Processing

What are we going to build?

A form to create a new car.

Spring Form Processing

Form Controller

Struts controllers extend Action whereas Spring controllers extend/implement a Controller class/interface. There are many of them you can choose from. The most basic is Controller, that we used previously. When it comes to process a form, SimpleFormController is generally used.

A form controller has two roles: initialize the form's initial values and process/persist the user input:

Command

A Command object is used to store the form values: it's equivalent to a Struts Action Form. However, it doesn't have to extend nor implement anything. So it's even possible to directly use the model class! (Car in our example)

Manager Classes

We need a method to create a car in 'WEB-INF/src/springmvc/service/CarManager.java':

public Car createCar(Car c) {
	Car car = new Car();
	car.setId((long)carList.size() + 1);
	car.setBrand(c.getBrand());
	car.setModel(c.getModel());
	car.setPrice(c.getPrice());
 
	carList.add(car);
 
	return car;
}

We also need a manager for brands, 'WEB-INF/src/springmvc/service/BrandManager.java':

package springmvc.service;
 
import java.util.LinkedList;
import java.util.List;
 
import springmvc.model.Brand;
 
public class BrandManager {
 
	private static List<Brand> brandList;
 
	static {
		Brand brand1 = new Brand();
		brand1.setId((long)1);
		brand1.setName("Mercedes");
		brand1.setCountry("Germany");		
 
		Brand brand2 = new Brand();
		brand2.setId((long)2);
		brand2.setName("Peugeot");
		brand2.setCountry("France");		
 
		brandList = new LinkedList<Brand>();
		brandList.add(brand1);
		brandList.add(brand2);		
	}
 
	public List<Brand> getBrandList() {
		return brandList;
	}	
 
	public Brand getBrandById(Long id) {
		for (Brand brand : brandList) {
			if (brand.getId().equals(id))
				return brand;
		}
		return null;
	}
}

Let's not frown upon the data duplication, it's not the point here :-)

Controller

In 'WEB-INF/springmvc-servlet.xml', we declare a new URL and its Controller:

<bean name="/new_car.html" class="springmvc.web.CarNewController">
    <property name="commandClass" value="springmvc.model.Car"/>
    <property name="formView" value="carNew"/>
    <property name="successView" value="list_cars.html"/>
</bean>
  • commandClass: the Command class
  • formView: the form view name
  • successView: where we go after the form has been successfully submitted

We now create this Controller: 'WEB-INF/src/springmvc/web/CarNewController.java':

package springmvc.web;
 
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
 
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import org.springframework.web.servlet.view.RedirectView;
 
import springmvc.model.Brand;
import springmvc.model.Car;
import springmvc.service.BrandManager;
import springmvc.service.CarManager;
 
public class CarNewController extends SimpleFormController {
 
    @Override
    protected Object formBackingObject(HttpServletRequest request) throws Exception {
    	Car defaultCar = new Car();
    	defaultCar.setModel("new model");
    	defaultCar.setPrice(new BigDecimal(15000));
    	return defaultCar;
    }
 
    @Override
    protected Map referenceData(HttpServletRequest request) throws Exception {
    	Map<Object, Object> dataMap = new HashMap<Object, Object>();
    	BrandManager brandManager = new BrandManager();
    	dataMap.put("brandList", brandManager.getBrandList());
    	return dataMap;
    }
 
    @Override
    protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
    	binder.setDisallowedFields(new String[] {"brand"});
 
    	Car car = (Car)binder.getTarget();
 
    	// set car's brand from request parameter brand id
    	BrandManager brandManager = new BrandManager();    	
    	Long brandId = null;
    	try {
	    	brandId = Long.parseLong(request.getParameter("brand"));
		} catch (Exception e) {}		
		if (brandId != null) {
			Brand brand = brandManager.getBrandById(brandId);
			car.setBrand(brand);
		}    
    }
 
    @Override
    public ModelAndView onSubmit(Object command) throws ServletException {
    	CarManager carManager = new CarManager();
    	carManager.createCar((Car)command);
 
    	return new ModelAndView(new RedirectView(getSuccessView()));
    }
 
}

These methods are called before the form is displayed:

  • formBackingObject: initialize the Command used to init the form.
  • referenceData: set the view attributes (using a Map)

These are called after:

  • initBinder: prevent Spring to do some bindings and so them by ourselves if needed. Here we used the brand id parameter to set the actual Brand.
  • onSubmit: the main code. In this case, we used the command object, which is a Car, to create a new Car.

View

Let's now create the view 'jsp/carNew.jsp':

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
 
<html>
<body>
	<h1>New Car</h1>
 
	<form:form method="post">
 
		Brand<br />
		<form:select path="brand">
		   <form:options items="${brandList}" itemLabel="name" itemValue="id" />
		</form:select>
		<br /><br />
 
		Model<br />
		<form:input path="model"/><br /><br />
 
		Price<br />
		<form:input path="price"/><br /><br />
 
		<input type="submit" value="Submit">
 
	</form:form>
</body>
</html>

It's blissfully simple. We use the brandList attribute defined in referenceData method in the controller to populate the form select.

Result

We rebuild (ant), relaunch Tomcat and check it's working: http://localhost:8180/springmvc/new_car.html

Summary

Your project should now look like that:

You can download it here.

This is how Spring process a form. Some advantages compared to Struts:

  • controller code is divided between several methods to override only when necessary
  • model beans can be used as form beans: no extra classes to write. For complex attributes (like brand in our example), we can override Spring and do these bindings by ourselves.
  • the xml configuration is clear and straightforward

Previous: Model View Controller --- Up: Spring Fast Tutorial --- Next: Form Validation

 

Feedback

Good ,we understand easyly,Good Examples. I think U can Give Some More Explanation(process of the code)about coding .
pasha
May 20, 2008
#1
Nice tutorial, appreciate if more examples added related to spring DAO and transection management.
Rakesh
November 11, 2008
#2
Great!!!
Chetan
December 1, 2008
#3
Very Good example.. In the browser I am getting the correct result. But at the server I got java.lang.NumberFormatException: null when I opened the application in browser. any help please..
Venkat
December 4, 2008
#4
Really good example, but how can I preselect a value for <form:select ? Thx a lot.
Daniel
December 5, 2008
#5
I found the answer, if we bind the object on formBackingObject, it will automatically showing the value in db as selected. This simple example are AAA++++++.
Daniel
December 6, 2008
#6
Very good example! It helped me a lot.
Enrico
January 16, 2009
#7
Excellent tutorial, anyone can easily understood. Please add more examples. Christopher
Christopher
January 20, 2009
#8
Nice!
Partha
February 17, 2009
#9
exception javax.servlet.ServletException: Servlet.init() for servlet satyam threw exception org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) java.lang.Thread.run(Unknown Source) root cause java.lang.NoClassDefFoundError: org/springframework/beans/PropertyAccessorFactory org.springframework.web.servlet.HttpServletBean.init(HttpServletBean.java:114) javax.servlet.GenericServlet.init(GenericServlet.java:211) org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105) org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148) org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869) org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664) org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527) org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80) org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684) java.lang.Thread.run(Unknown Source) I'm getting this error when i run the Hello_world application. Kindly help!!!
shruthi
February 19, 2009
#10
Shruthi, try "ant clean" and then "ant" before running the application.
Jérôme Jaglale
February 19, 2009
#11
Thanks Jerome, I got it.
shruthi
February 20, 2009
#12
I am getting the below error:- java.lang.StackOverflowError javax.servlet.ServletException.getRootCause(ServletException.java:96) org.springframework.web.util.NestedServletException.getCause(NestedServletException.java:69) javax.servlet.ServletException.getRootCause(ServletException.java:96)

April 20, 2009
#13
Hi, I am getting the below error:- java.lang.StackOverflowError javax.servlet.ServletException.getRootCause(ServletException.java:96) org.springframework.web.util.NestedServletException.getCause(NestedServletException.java:69) javax.servlet.ServletException.getRootCause(ServletException.java:96) Everything was working fine till MVC tutorial but I am not able to complete the form processing. Thanks
Anuj
April 20, 2009
#14
Hi Anuj, have you tried to restart Tomcat?
Jérôme Jaglale
April 20, 2009
#15
Yes. I restarted tomcat and also deleted the work dir, but the same result. BTW I am not using the ant script. I am deploying it as a tomcat project in eclipse but I think this should not make any diff.
Anuj
April 20, 2009
#16
I don't know, then. It's the first time someone reports that. That must be something in your configuration. Let us know if you manage to fix it.
Jérôme Jaglale
April 20, 2009
#17
I was able to solve the problem. The issue was that the build path in eclipse were pointing to spring 2.5 version but the lib directory of my project had spring 3.0 jars. I just replaced the 3.0 jars with 2.5 and it worked fine.
Anuj
April 21, 2009
#18
Great! Thank you Anuj.
Jérôme Jaglale
April 21, 2009
#19
hi on calling this page i am geeting following error .please help me i am new in spring Error 500--Internal Server Error java.lang.NoSuchMethodError: org.springframework.web.util.WebUtils.getDefaultHtmlEscape(Ljavax/servlet/ServletContext;)Ljava/lang/Boolean; at org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:215) at org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:76) at org.springframework.web.servlet.support.JspAwareRequestContext.(JspAwareRequestContext.java:50) at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:74) at jsp_servlet._web_45_inf._jsp.__carnew._jspService(__carnew.java:131) at weblogic.servlet.jsp.JspBase.service(JspBase.java:34) at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:226) at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:124) at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283) at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175) at weblogic.servlet.internal.RequestDispatcherImpl.invokeServlet(RequestDispatcherImpl.java:503) at weblogic.servlet.internal.RequestDispatcherImpl.forward(RequestDispatcherImpl.java:245) at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:171) at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:251) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1160) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:901) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:809) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:476) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:431) at javax.servlet.http.HttpServlet.service(HttpServlet.java:707) at javax.servlet.http.HttpServlet.service(HttpServlet.java:820) at weblogic.servlet.internal.StubSecurityHelper$ServletServiceAction.run(StubSecurityHelper.java:226) at weblogic.servlet.internal.StubSecurityHelper.invokeServlet(StubSecurityHelper.java:124) at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:283) at weblogic.servlet.internal.ServletStubImpl.execute(ServletStubImpl.java:175) at weblogic.servlet.internal.WebAppServletContext$ServletInvocationAction.run(WebAppServletContext.java:3370) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(AuthenticatedSubject.java:321) at weblogic.security.service.SecurityManager.runAs(Unknown Source) at weblogic.servlet.internal.WebAppServletContext.securedExecute(WebAppServletContext.java:2117) at weblogic.servlet.internal.WebAppServletContext.execute(WebAppServletContext.java:2023) at weblogic.servlet.internal.ServletRequestImpl.run(ServletRequestImpl.java:1359) at weblogic.work.ExecuteThread.execute(ExecuteThread.java:200) at weblogic.work.ExecuteThread.run(ExecuteThread.java:172)
sumadha
April 24, 2009
#20
ya it was my silly mistake (due to first time in spring ).i solved this problem. sumadha.omar@gmail.com
sumadha
April 27, 2009
#21
very nice tutorial and easy to understand. Please add more examples.
SR
July 8, 2009
#22
Hi its giving me error at binder.setDisallowedFields
swaroop
July 8, 2009
#23
hi Jerome.. i need to add a hyperlink on my page which should invoke Spring MVC controller ( something like "Forgot Password" link ), Could you tell me how to do that ?
Neha
July 23, 2009
#24
This is one of the tutorials which would get you started with spring MVC. Its pretty easy to understand and can get some basic idea about the features in spring mvc and the way its been done in spring.Much appreciated
Kumar
August 4, 2009
#25
I get this error whenever I click submit button. can someone please help? thanks! org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException root cause java.lang.NullPointerException
me82
August 13, 2009
#26
These codes are not working in my implementation. do i still need to initialize something?
qwerty
August 13, 2009
#27
I'd just like to add my thanks to the others; this is a great introduction - it is priceless and has saved me countless hours of hand-wringing and lost-sleep. Regards and God bless.
Lanre
August 26, 2009
#28
Nice Article for beginners and very easy to understand and implement and thanks to the Author - VJ

September 1, 2009
#29
Nice Article for beginners and very easy to understand and implement and thanks to the Author
VJ
September 1, 2009
#30
Best tutorial for me
Yothin
September 9, 2009
#31
Tutorial useful thus far, thanks! I'm using Netbeans 6.7.1 with Spring Web MVC 2.5. Not my first tutorial but helping me to solidify things I didn't really get on previous tutorials. Hope to tame the Spring beast soon... :) Thanks again!
PUK
September 9, 2009
#32
Why are we creating Car out of Car in method createCar(Car car) ?
Makatun
September 16, 2009
#33
Gr8 deal
Ashutosh
October 6, 2009
#34
nice one keep it up
amol
October 14, 2009
#35
Thank you!
Trail
October 17, 2009
#36
Very good... please add some examples for DAO & Transaction Management .....
Korea
October 30, 2009
#37
This sentence needs work: initBinder: prevent Spring to do some bindings and so them by ourselves if needed. Here we used the brand id parameter to set the actual Brand.
Sambo
November 16, 2009
#38
OH thank you so much!! I've been trying to find a tutorial on working with collections and the select tag and have had no joy for almost a week. Most of the ones I found were either too cumbersome or not well written. I stumbled across this tutorial and its just perfect. Simple explanation with a simple code to get one started. Absolutely brilliant. Keep up the good work
quophyie
November 23, 2009
#39
Thank you. Very nice tutorial. But my onSubmit() function doesn't work. Please help me
deegii
November 27, 2009
#40
please : i java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? at org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:84) at org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:217) at org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:75) at org.springframework.web.servlet.support.JspAwareRequestContext.(JspAwareRequestContext.java:49) at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:74) at org.apache.jsp.new_005fcar_jsp._jspx_meth_form_form_0(new_005fcar_jsp.java from :102) at org.apache.jsp.new_005fcar_jsp._jspService(new_005fcar_jsp.java from :74) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:486) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:380) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315) at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096) at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288) at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647) at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579) at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831) at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341) at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263) at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214) at com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:380) at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265) at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106) StandardWrapperValve[jsp]: PWC1406: Servlet.service() for servlet jsp threw exception java.lang.IllegalStateException: No WebApplicationContext found: no ContextLoaderListener registered? at org.springframework.web.servlet.support.RequestContextUtils.getWebApplicationContext(RequestContextUtils.java:84) at org.springframework.web.servlet.support.RequestContext.initContext(RequestContext.java:217) at org.springframework.web.servlet.support.JspAwareRequestContext.initContext(JspAwareRequestContext.java:75) at org.springframework.web.servlet.support.JspAwareRequestContext.(JspAwareRequestContext.java:49) at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:74) at org.apache.jsp.new_005fcar_jsp._jspx_meth_form_form_0(new_005fcar_jsp.java from :102) at org.apache.jsp.new_005fcar_jsp._jspService(new_005fcar_jsp.java from :74) at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:109) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:389) at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:486) at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:380) at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) at org.apache.catalina.core.ApplicationFilterChain.servletService(ApplicationFilterChain.java:427) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:315) at org.apache.catalina.core.StandardContextValve.invokeInternal(StandardContextValve.java:287) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:218) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593) at com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:94) at com.sun.enterprise.web.PESessionLockingStandardPipeline.invoke(PESessionLockingStandardPipeline.java:98) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:222) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:166) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:648) at org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:593) at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:587) at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:1096) at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:288) at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.invokeAdapter(DefaultProcessorTask.java:647) at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.doProcess(DefaultProcessorTask.java:579) at com.sun.enterprise.web.connector.grizzly.DefaultProcessorTask.process(DefaultProcessorTask.java:831) at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.executeProcessorTask(DefaultReadTask.java:341) at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:263) at com.sun.enterprise.web.connector.grizzly.DefaultReadTask.doTask(DefaultReadTask.java:214) at com.sun.enterprise.web.portunif.PortUnificationPipeline$PUTask.doTask(PortUnificationPipeline.java:380) at com.sun.enterprise.web.connector.grizzly.TaskBase.run(TaskBase.java:265) at com.sun.enterprise.web.connector.grizzly.ssl.SSLWorkerThread.run(SSLWorkerThread.java:106)
Giorgi Archvadze from Georgia (Country), Tbilisi
December 9, 2009
#41
Very good tuorial for beginner
Giorgi Archvadze from Georgia (Country), Tbilisi
December 9, 2009
#42
Very nice tutorial..

December 29, 2009
#43
I'm trying to run these samples but I get this exception, any ideas? org.apache.jasper.JasperException: An exception occurred processing JSP page /jsp/carNew.jsp at line 11 8: <form:form method="post"> 9: 10: Brand
11: <form:select path="brand"> 12: <form:options items="${brandList}" itemLabel="name" itemValue="id" /> 13: </form:select> 14:

Stacktrace: org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:505) ... root cause java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141)...
albemuth
January 7, 2010
#44
I tried the example with little more additions. It worked. Its a nice example.It gave more clear understanding of spring mvc.Thanks a ton.
Swati
January 11, 2010
#45
Hi, Thanks for this tut. I had to add a permission to /etc/tomcat5.5/policy.d/04webapps.policy on my debian etch machine. It was : grant codeBase "file:/projects/java/spikes/springmvc/-" { permission java.lang.RuntimePermission "getClassLoader"; };
Anders Thøgersen
January 19, 2010
#46
I could not achieve this as stated by someone above: "I found the answer, if we bind the object on formBackingObject, it will automatically showing the value in db as selected."

Can someone please help by illustrating this?
TechS
March 2, 2010
#47
hi,

Can you explain about these tag

<property name="commandClass" value="com.springmvc.model.Car"/>
<property name="formView" value="carNew"></property>
<property name="successView" value="new_car.htm"></property>

and class from CarNewController, when call these method
formBackingObject, referenceData, initBinder

thanks & regards,
Venkatesan
Venkatesan
March 9, 2010
#48
Hi Jerome,

Thanks for putting this tutorial on the web. It is really helpful in understanding Spring MVC for a beginner.

Thanks a million
regards
Ramesh ( India )
March 19, 2010
#49
First of all, thanks a lot for your work.
My questions are:
1. springmv-servlet.xml is not defined in web.xml. So how the application refers to this file?

2. in initBinder(), brandId is obtained as below:
brandId = Long.parseLong(request.getParameter("brand"));
Here request.getParameter("brand") would return Mercedes.
then brandId would be what?

Can you hep?
Larry
March 24, 2010
#50
Larry,

1. Spring servlet is declared in web.xml. It knows about springmvc-servlet.xml.

2. The request parameter "brand" contains the brand id: in the JSP, for the "brand" select:
itemValue="id"
Jérôme Jaglale
March 24, 2010
#51
Hi Jérôme,
I got 2. cleared

For 1, so how should I name it? I believe there should be a rule to name the spring config file. Am I right or wrong?

Thanks a lot
Larry

March 25, 2010
#52
Larry,

Sorry, you're right, I had forgotten about it.

The Spring servlet's name in web.xml determines its config file's name. Example: if the servlet name is springmvc, the config file must be named springmvc-servlet.xml. If it was foo, it would be foo-servlet.xml.

More details there.
Jérôme Jaglale
March 25, 2010
#53
Hello sir,

I am new for spring framework but your tutorial teached me as a good teacher.
Thank's a lot.
shankar
March 26, 2010
#54
Hi Jérôme,
Thanks. I got to that page searching for the answer as well.

Overall, I have some experience in Struts, so I see Spring MVC very similar, and easy to understand. I still haven' seen the big advantage of Spring MVC over struts, to be honest.

But your work is helpful to a lot of people, including me. We all appreciate your kindness.

Larry
Larry
March 26, 2010
#55
Hi Jérôme,

Thanks for putting up this nice tutorial on the Web.

I'm having problem with this statment in initBinder method:

Long brandID = null;
...
brandId = Long.parseLong(request.getParameter("brand"));

I got error for: Type mismatch: cannot convert from long to Long

Thanks for your help in advance.


What did I do wrong in here?
Jin
April 3, 2010
#56
Hi, I have a .jsp page only with 3 text fields but none of them appear when I run the application!! The only way they appear is putting &lt;input type="text" name="xxx"/&gt; instead of &lt;form:input path="xxx"/&gt; How can I make it work? I mean I want to use &lt;form:input path="xxx"/&gt; in order to use the validators later. Please help. Thank you
Agustin
April 10, 2010
#57
simple and nice. good if explained its methods deeply with more examples
vamsi
April 12, 2010
#58
oh my god ,thats a lot of effort,It can be done in a thrice using servlets ,exactly the same thing .

Robert Harvey
April 28, 2010
#59
Great tutorial!
Draxa
May 9, 2010
#60
Basic and helpfull for me!
loga
May 19, 2010
#61

May 24, 2010
#62
bla bla
raghu
May 25, 2010
#63
Easy to understand. Very good, fast tutorial. Thanks.
Samar
June 7, 2010
#64
so simple and clear to understand
keep it up man !!
alan max
June 15, 2010
#65
Thanks This is really good.
John
June 20, 2010
#66
cool cool cool
Guru
July 16, 2010
#67
Good and very easy example,which makes the clear concept.
Thank you lot, how ever has written this.
rak
July 23, 2010
#68