[ad name=”AD_INBETWEEN_POST”]
Introduction to Spring 3 MVC Framework
Spring MVC is the web component of Spring’s framework. It provides a rich functionality for building robust Web Applications. The Spring MVC Framework is architected and designed in such a way that every piece of logic and functionality is highly configurable. Also Spring can integrate effortlessly with other popular Web Frameworks like Struts, WebWork, Java Server Faces and Tapestry. It means that you can even instruct Spring to use any one of the Web Frameworks. More than that Spring is not tightly coupled with Servlets or JSP to render the View to the Clients. Integration with other View technologies like Velocity, Freemarker, Excel or Pdf is also possible now. [sc:SpringMVC_Tutorials] In Spring Web MVC you can use any object as a command or form-backing object; you do not need to implement a framework-specific interface or base class. Spring’s data binding is highly flexible: for example, it treats type mismatches as validation errors that can be evaluated by the application, not as system errors. Thus you need not duplicate your business objects’ properties as simple, untyped strings in your form objects simply to handle invalid submissions, or to convert the Strings properly. Instead, it is often preferable to bind directly to your business objects.Request Processing Lifecycle
Image courtesy: Springsource Spring’s web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central servlet that dispatches requests to controllers and offers other functionality that facilitates the development of web applications. Spring’sDispatcherServlet
is completely integrated with Spring IoC container and allows us to use every other feature of Spring. Following is the Request process lifecycle of Spring 3.0 MVC:- The client sends a request to web container in the form of http request.
- This incoming request is intercepted by Front controller (DispatcherServlet) and it will then tries to find out appropriate Handler Mappings.
- With the help of Handler Mappings, the DispatcherServlet will dispatch the request to appropriate Controller.
- The Controller tries to process the request and returns the Model and View object in form of ModelAndView instance to the Front Controller.
- The Front Controller then tries to resolve the View (which can be JSP, Freemarker, Velocity etc) by consulting the View Resolver object.
- The selected view is then rendered back to client.
Features of Spring 3.0
- Spring 3.0 framework supports Java 5. It provides annotation based configuration support. Java 5 features such as generics, annotations, varargs etc can be used in Spring.
- A new expression language Spring Expression Language SpEL is being introduced. The Spring Expression Language can be used while defining the XML and Annotation based bean definition.
- Spring 3.0 framework supports REST web services.
- Data formatting can never be so easy. Spring 3.0 supports annotation based formatting. We can now use the @DateFimeFormat(iso=ISO.DATE) and @NumberFormat(style=Style.CURRENCY) annotations to convert the date and currency formats.
- Spring 3.0 has started support to JPA 2.0.
Configuring Spring 3.0 MVC
The entry point of Spring 3.0 MVC is theDispatcherServlet
. DispatcherServlet is a normal servlet class which implements HttpServlet
base class. Thus we need to configure it in web.xml
.<web-app>
<servlet>
<servlet-name>example</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>example</servlet-name>
<url-pattern>*.html</url-pattern>
</servlet-mapping>
</web-app>
Code language: HTML, XML (xml)
In above code snippet, we have configure DispatcherServlet in web.xml. Note that we have mapped *.html url pattern with example DispatcherServlet. Thus any url with *.html pattern will call Spring MVC Front controller. Image courtesy: Springsource Once the DispatcherServlet is initialized, it will looks for a file names [servlet-name]-servlet.xml
in WEB-INF folder of web application. In above example, the framework will look for file called example-servlet.xml
. Note the above architecture diagram. The WebApplicationContext
specified in above diagram is an extension of the plain ApplicationContext
with some extra feature necessary for web applications. The WebApplicationContext is capable of resolving themes and it is also associated with corresponding servlet. The WebApplicationContext is bound in the ServletContext, and by using static methods on the RequestContextUtils class you can always look up the WebApplicationContext.
SPRING MVC is really powerful framework. I would be interested to see this working with the Spring Webflow which we have been using for long time now with older version of spring.
you are always right.
I will give you the answer tomorrow ok…….
I found Spring MVC complete overdone archite architecture. If you really need to see good MVC framework, go and look at portlet specifications or play around with it. You will hate Spring / Struct etc.
You are good looking
ha ha ha…cooll mean
gooooooooooooooooooooood
it’s looking very nice
Right..its a robust and most useful framework.
Yes right, Spring MVC is very powerfull framework, and it is very interested framework.
This tutorial is just what I need and just when I need it. I hope the rest of this series is coming soon. Thanks.
like this tutorial , thanks..^^
Thank you so much :)
This is a great serious of tutorials. It definitely helped me to get going with spring 3.0.
Excellent tutorial. Well done. I got it working first time round. Thanks so much.
thanks so much
Thank you so much. This is an excellent tutorial. I can get myself familiar with Spring 3 quickly after looking this tutorial.
Hi
If you have worked with Struts2, is it possible for you to recommend Struts2 or Spring3 MVC?
Thanks for your help
Very nice article dude.
What I like in spring is there IOC support , AOP support and various template they have provided for enterprise integration e.g. JMS Template , JDBCTemplate etc.
Thanks
Javin
Very nice and helpful tutorial. Thanks for this.
THANK YOU MAN! TAHTS GREAT! ALL MY CHEERS!
Hay man,
really you are genious, you really worked very good,
your each n every article is so simple and good to understand.
is it possible to make any example in spring MVC and Struts2 and Hibernate???
Thanks for evrything…
Hi this is very useful tutorial to help the clear concept and easy configration .
Nice work there…. An in-depth and through article.. will really be helpful. Im a avid spring programmer and I really like watever is mentioned here… good job :)
Friend, I am interesting what the widget you are using. Thank you sir forgive my english.
Spring MVC is new for me, but is very easy with this tutorial, thank you !
Good job
Hi ,
Thanks for the Very good article.
I would like to know which Controller to select in my application.
If you have any info in Deciding the Controllers it would be still great.
Regards,
Suman
Controller are requirement base !!!!
I always looks for this type of tutorials while i start to learn something new. It definitely helped me to get going with spring 3.0.This is a great serious of tutorials.
Thanks for all.
This is a great tutorial. It is easy to understand. I get familiar with w/ spring when I read this.. Keep up the gud work…
Thx for the tutorial. It gave me a lot help.
Thanks 4 giving only the examples that matters most effectivly . keep up the good work
your explaination way always great .whatever the technology you are the best one
pretty clear; lot of thanks
This is great work!!!!!!
I got through your information and using Spring.. but facing some to run. Can you provide some examples.
easy to follow …., can anyone know the best book for spring for beginners and also for intermediate….?
Its a great tutorial, the first time i started with spring was very difficult because i dont speak english, now, i really exited with this tools. I already know what i want to be , i want to be a bakery, its a joke.
thousands of thanks for this tutorial, i hope every one enjoy this good work
Nice Tutorials. So much helpful.
Great Tutorial Viral. I will surely bookmark this :)
Great tutorial Viral. Thank you for this good tutorial
Hi
I am trying to develop small Spring MVC 3 application using spring-framework-3.1.2.RELEASE.zip. I downloaded this release from http://www.springsource.org/download/community. When I included the Spring jar files, my application was not running and I was getting error “Servlet dispatcher not available”. I noticed that spring-framework-3.1.2.RELEASE.zip did not had Spring MVC jar file. I included this spring mvc jar file from lower version say Spring 3.0 then my application started running.
I have noticed the lower version and the higher version spring-3.2.0.M1-dist.zip both have spring mvc jar file then why this jar file is missing in spring-framework-3.1.2.RELEASE.zip which is the current stable version as it GA version. Please clarify.
Thanks
Vijay
Hi,
This tutorial was excellent and helping a lot, Can we expect the examples like
1. Integration of Struts2, Spring 3 and Hibernate 4 ORM, JPA
2. Apache CXF, Struts 2, Spring 3, Hibernate 4 and JPA
Thanks & Regards,
Sridhar Goranti
It is pocket purse of all java technologies
Nice tutorial for Spring beginners….
Try the link below where xxxx is the project name you gave. This will start the project
http://localhost:8080/xxxxxx/hello.html
Remember in web.xml in the above example it is mentioned url pattern has *.html. so in the url you need to mention the url as hello.html.
Hope this helps.
thanks ,its really helpful for fresher
thanks nice tutorial
Thank you for such great and Nice tutorial…………………
Excellent tutorials for Spring MVC guys. :)
Hi Viral,
Do you have Spring core tutorials
SC
HI, I’m using Spring3.0 framework.
I have a class NODE & it contains an object of class LINK. I want to bind jsp form:input tag with Bean object of NODE. I’m writing form:input tag for property of LINK elements as link.boundaymap & link.vlanid, still bean doesn’t receive any value from jsp.
Some binding exception still occurs. please let me know what to do.
better to check the commandName properly and use spring 3.0 features like go far annotations
first you to get the form through the
@RequestMapping(method=method = RequestMethod.GET)
public String showForm(Map model)
{
//assume if you are using loginform class ,what ever the properties is there for //loginform class you have to provide same properties for jsp page also
LoginForm loginForm = new LoginForm();
model.put(“loginForm”, loginForm);
return “loginForm”;
}
and then process the task by using post method like
@RequestMapping(method=method = RequestMethod.Post)
Hi Viral,
Your Post is very useful for beginner. if u can include one more thing trace of the program it will be nice for beginner to understand how the program flow goes. thanks for such a useful post.
give me suggesions about spring framework
Its a very nioce article on Spring . Thank you
very useful for beginers
Thanks viral, these are very helpful articals & examples to take a easy start with Spring framework.
Thanks :)
hi im new to spring mvc can u teach me how to learn from basic of spring mvc
spring mvc is a worst frame work for creating a textbox i need to work on form, model, controller,service,xml… bla.. bla.. bla… in html in simple code i can create textbox
Spring MVC is wonderful framework to develop a web application. I like Spring very much.
Thanks… It’s informative and very methodical.
hello!! Good!!
Very simple and really informative..Thanks alot Viral… :)
Hey,
I have done exactly as instructed, but i m getting HTTP Status 404 error.
Server i used is jBoss6.0 . please help me out.
pls check connection. Controller and dispatcher-servlet or the directory..
thankx for the post it was very helpful
Hi,
can you post any blogs which related to integration with spring hibernate and Dwr concept???
It is very simple and understandable.Thanks for giving good explanation.
hey its not working while i do same to same and its give error when i nevigate page from welcome page to hello.jsp Error “page not found “
Thanks this was really informative!!!
Thanks its very Informative and fruitful..
when i run the program i get this error message:
Very informative , Thanks a lot
Very informative!
Very Nice.
can we use two bean in a service method?
Hello, What is the diagram software you used to create following diagrams? is it creately ?
Hi,
I am have a created a sample HelloWorld project, but after successful deployment when I am hitting the url appropriate controller is invoked but after it returns the view name status 404 is shown and in the log file error message is displayed : “No handler found”
No mapping found for HTTP request with URI [/HelloWorld/WEB-INF/views/hello.jsp]in DispatcherServlet with name ‘spring’.
Url I hit is “http://localhost:8081/HelloWorld/hello” and ‘spring’ is my servlet name
good
Hi
I download your source code It’s working smoothly.
But I faced one issue when I have done following modification
index.jsp file renamed to index.html(index.html is valid html file)
in web.xml change welcome page as index.html and url pattern as /
below my web.xml content
———————————————————————————————–
Spring3MVC
index.jsp
spring
org.springframework.web.servlet.DispatcherServlet
1
spring
/
——————————————————————————————————–
as soon as I tried to use .htm file as welcome page it gives me 404 resource not found error
Can you Please comment on this behaviour .
A nice article here with some useful tips.Thanks