Spring MVC Tutorial

Spring MVC, a Java Model-View-Contraller (MVC) web framework, which builds on the Spring Inversion of control(IoC) framework, extensive use of the Spring’s features make the Spring MVC framework highly decouple the components dependency and simplify the whole MVC configuration.
In this series of tutorials, it’s provides many step by step examples and explanations on using Spring MVC framework.
Added many Spring 3 MVC tutorials to intergrate REST, bean validation @Valid, XML, JSON and many others. For what new in Spring 3.0, you can refer to this official Spring 3 MVC documentation
Happy learning Spring MVC :)
Spring MVC Quick Start
Let go through a quick start to understand how Spring MVC framework works.
- Spring 3 MVC hello world example
what you need to develop Spring 3 MVC web application. - Spring 3 REST hello world example
Develop REST style web application in Spring 3 MVC. - Spring 2.5.6 MVC hello world example
XML-based Spring 2.5.6 MVC hello world example. - Spring 2.5.6 MVC hello world annotation example
Annotation-based Spring 2.5.6 MVC hello world example.
Handler Mapping
Define how web request (URL) maps to the Controller handlers.
- BeanNameUrlHandlerMapping example
Maps the requested URL to the name of the cocntroller. - ControllerClassNameHandlerMapping example
Uses convention to map the requested URL to Controller. - SimpleUrlHandlerMapping example
Allow developer to specify the mapping of URL patterns and handler mappings explicitly. - Configure the handler mapping priority
If multiple view handler mappings are applied, you have to declare priority to avoid conflict issue.
Controller
Controller class to handle the web request.
- MultiActionController example
Group related actions into a single controller class. - MultiActionController annotation example
Multi-actions controller by using annotation, @RequestMapping, he best and easy solution. - PropertiesMethodNameResolver example
A flexible method name resolver for the MultiActionController controller class, it allow to define the mapping between requested URL and method name explicitly. - ParameterMethodNameResolver example
Another method name resolver for the MultiActionController controller class, it allow to map URL to method name via requested parameter name. - ParameterizableViewController example
With ParameterizableViewController, you don’t need to hard code the view name in the controller class anymore, instead, you specify the view name through the ParameterizableViewController’s “viewName” property.
View Resolver
Resolve “view name” that returned from the controller class to a physical view page or JSP page.
- InternalResourceViewResolver example
Adding a predefined prefix and suffix to the view name (prefix + view name + suffix), and generate the final view page URL. - XmlViewResolver example
Puts view beans in XML file. - ResourceBundleViewResolver example
Puts view beans in “.properties” file. - Configure multiple view resolvers priority
If multiple view resolver strategies are applied, you have to declare priority to avoid conflict issue.
Form Handling
Form handling in Spring MVC.
- Form handling example
Form handling in Spring MVC, XML based version. - Form handling annotation example
Form handling in Spring MVC, annotation version. - Handling duplicate form submission
“Post/Redirect/Get” Design Pattern is the common solution for this duplicated form submission problem. - RedirectView example
A view which redirect to another absolute, context relative, or current request relative URL. - Handling multipage / wizard forms
How to handle multipage forms or wizard form.
Spring’s Form Tag Library
Render HTML form’s components via Spring’s form tag.
- Textbox example
<form:input /> tag, render a HTML textbox field. - Password example
<form:password /> tag, render a HTML password field. - Textarea example
<form:textarea /> tag, render a HTML textarea field. - Checkbox and Checkboxes example
<form:checkbox />, <form:checkboxes /> tags, render a single or multiple HTML check boxes. - Radiobutton and Radiobuttons example
<form:radiobutton />, <form:radiobuttons /> tags, render a single or multiple HTML radio buttons. - Dropdown and list box example
<form:select />, <form:option /> and <form:options /> tags, render a HTML dropdown box, multiple select box and listbox. - Hidden value example
<form:hidden /> tags, render a HTML hidden value field. - Form errors tag example
<form:errors /> tag , render those form components error messages. - File upload example
File upload handling via Spring MultipartResolver.
Integrating Spring MVC with other frameworks
Integrate Spring MVC with others.
- Spring 3 MVC and JSR303 @Valid example
Using Hibernate validator (JSR303 implementation) to validate bean in Spring MVC. - Spring 3 MVC and RSS feed example
Using ROME to generate RSS feed from Spring MVC. - Spring 3 MVC and XML example
Using JAXB to generate XML output from Spring MVC. - Spring 3 MVC and JSON example
Using Jackson to generate JSON output from Spring MVC. - Spring MVC and Excel file via AbstractExcelView
Using AbstractExcelView to export data to Excel file via Apache POI library. - Spring MVC and Excel file via AbstractJExcelView
Using AbstractJExcelView to export data to Excel file via JExcelAPI library. - Spring MVC and PDF file via AbstractPdfView
Using AbstractPdfView to export data to Pdf file via Bruno Lowagie’s iText library. - Spring MVC and Log4j integration example
Example to integrate Log4j into the Spring MVC application.
FAQ
Some common use cases.
- Spring 3 MVC ContentNegotiatingViewResolver example
This view resolver let you determine which view resolver will be returned, base on reuqested file extension. - Exception handling example
Exception handling in Spring MVC. - Handler interceptors example
Intercept web request through handler interceptors. - Internationalization example
Multiple languages supported in Spring MVC.
Common Errors
Spring MVC common errors and solution.
- ModelAndView’s model value is not displayed in JSP via EL
- 404 error code is not working in Spring MVC
- Cannot change HTTP accept header – use a different locale resolution strategy
- Spring MVC failed to convert property value in file upload form
- Neither BindingResult nor plain target object for bean name ‘xxx’ available as request attribute
- ClassNotFoundException : com.sun.syndication.feed.WireFeed
- ClassNotFoundException : com.thoughtworks.xstream.io.HierarchicalStreamReader






I must say the tutorials are amazing; thanks a lot.
Welcome, good to know it help.
can any one explain how to start spring 2.0 mvc with hibernate?(with example)
Thakz for uploading bro…..
I was looking for this only. It helped me alot. This is best tutorial on the web . :) :) thank you for publishing such a wonderful blog
Thanks for your kind words. Will publish more similar posts in future.
Can you provide spring mvc session attribute example
Hi,
No Doubt, this is really a good tutorial. . but Unfortunately My problem wasn’t solved. . .
Can I please know how to convert a jsp project which has only two jsp pages(index & data) to Spring MVC Project?
I would be really gratefull If any oe could assist me. . .
Thanks in Advance!
Thanks for posting good ‘working’ examples on Spring… God bless you..
Congratulations for your site.
I’m from Spain and my work is developer with Java.
My english language level is very poor, but your site is very good.
Thanks.
G8 tutorial
Hi MKYong,
may i know how to convert below project to Spring 2 ?
http://krams915.blogspot.com/2010/12/jqgrid-and-spring-3-mvc-integration.html
Thanks.
don’t know how to convert @ResponseBody Spring 3 to Spring 2..
Should i learn spring 3 or spring 2 ?
most of Malaysia’s IT companys still using spring 2 right ?
Depends on company, you should learn Spring DI concept, not the version.
Hi MkYong,
May i know how to integrate Spring 2.5.6 MVC (XML based) with Tiles 2.06 ?
Thanks for your sharing.
HI, may i know how to integrate Spring MVC with hibernate ?
Refer to above Spring tutorials, it’s simply a normal Spring + Hibernate integration.
Hello Mkyong .
can u help me to understand beans lifecycle in spring MVC ?
When beans declared for any controller will be created ?
i’m korean..Not English sorry..
Your Spring3MVC SourceCode..Tomcat7 Test Fail…
But Tomcat6 Test Test Success..
fail issue : Tomcat7 and Maven..???……
Sir i can understand you have the problem with the Tomcat 7 version,but please be reasonable as this is an open community where young and old of all ages read the good examples given by Mr Mkyong.So if you can refrain yourself from using F word or any other uncalled for statement, all the readers will be grateful to you.
Thank you.
Thanks for your kind words and help. f words comment is deleted.
I don’t know how to thank you, you are number one tutorial in the internet, the way you teach things is simple.
I have some questions please :
1)Can you please teach us how did you generate a maven project that has pom.xml ?
2)Any example for spring MVC and Jquery and Json ?
3)Any example for spring MVC getting/inserting data to a database .
Thanks Lot.
tihs is very good,it’s beautiful!
Hei MkYong,
Nice detailed tutorials. Can you please also add Spring security 3 tutorial. I think this is important part of any application.
Best Regards.
Noted, will post in soon period.
i passed the interview becoz of your site. almost all emerging technologies discuss here. i owe u a lot.
Congratulation :)
if you happen to drop by singapore. drop me an email.
want treat me a coffee? :)
yes no problem…
Thank you a lot, your tutorials are awsome. So easy to learn from them.
This is very useful for freshers for better and easily understanding
MKYong,
You have an excellent ability to breakdown a complicated and overwhelming subject into a simple and easy-to-understand sections. For past year, I have been going through number of Spring Framework books where most authors talk and talk like politicians and never come down to a level of beginner developer and explain with some simple examples. I was never able to get a good grasp of how all the moving pieces work together until I luckily hit your website. Thankyou so much for your simple and clear examples. I was also impressed with your reply to one of the reader that you are also in the process of learning. That also speaks a lot about your good attitude that is often missing in today’s professional world ! Thanks again.
SY
Hi Sriyell,
Thanks for your kind words, and glad to know it help you in some ways.
It is really very use ful site . get good basic Info
Clean and neat explanation to the point. Keep it up your good work. This is one of the best sites to start understanding topics related to Java.
It is cool that you blogged about this. I found you on yahoo and I had been searching for information about this. Nice site, thanks for the info.