Spring MVC tutorial provides an elegant solution to use MVC in spring framework by the help of DispatcherServlet.
In Spring Web MVC, DispatcherServlet class works as the front controller. It is responsible to manage the flow of the spring mvc application.
The @Controller annotation is used to mark the class as the controller in Spring 3.
The @RequestMapping annotation is used to map the request url. It is applied on the method.
Understanding the flow of Spring Web MVC
As displayed in the figure, all the incoming request is intercepted by the DispatcherServlet that works as the front controller. The DispatcherServlet gets entry of handler mapping from the xml file and forwards the request to the controller. The controller returns an object of ModelAndView. The DispatcherServlet checks the entry of view resolver in the xml file and invokes the specified view component.
Spring MVC Framework Example
Let's see the simple example of spring web MVC framework. There are given 7 steps for creating the spring MVC application. The steps are as follows:
- Create the request page (optional)
- Create the controller class
- Provide the entry of controller in the web.xml file
- Define the bean in the xml file
- Display the message in the JSP page
- Load the spring core and mvc jar files
- Start server and deploy the project
Spring MVC Directory Structure
Directory Structure of Spring MVC using Maven
Required Jar files or Maven Dependency
To run this example, you need to load:
- Spring Core jar files
- Spring Web jar files
Download Link: Download all the jar files for spring including core, web, aop, mvc, j2ee, remoting, oxm, jdbc, orm etc.
If you are using Maven, you don't need to add jar files. Now, you need to add maven dependency in pom.xml file.
pom.xml1) Create the request page (optional)
This is the simple jsp page containing a link. It is optional page. You may direct invoke the action class instead.
index.jsp2) Create the controller class
To create the controller class, we are using two annotations @Controller and @RequestMapping.
The @Controller annotation marks this class as Controller.
The @Requestmapping annotation is used to map the class with the specified name.
This class returns the instance of ModelAndView controller with the mapped name, message name and message value. The message value will be displayed in the jsp page.
HelloWorldController.java3) Provide the entry of controller in the web.xml file
In this xml file, we are specifying the servlet class DispatcherServlet that acts as the front controller in Spring Web MVC. All the incoming request for the html file will be forwarded to the DispatcherServlet.
web.xml4) Define the bean in the xml file
This is the important configuration file where we need to specify the ViewResolver and View components.
The context:component-scan element defines the base-package where DispatcherServlet will search the controller class.
Here, the InternalResourceViewResolver class is used for the ViewResolver.
The prefix+string returned by controller+suffix page will be invoked for the view component.
This xml file should be located inside the WEB-INF directory.
spring-servlet.xml5) Display the message in the JSP page
This is the simple JSP page, displaying the message returned by the Controller.
It must be located inside the WEB-INF/jsp directory for this example only.
hellopage.jsp
Thank you so much for sharing this great blog.Very inspiring and helpful too.
ReplyDeleteHibernate Training in Chennai
Spring Hibernate Training in Chennai
Hibernate Training in Velachery
Spring Training in Chennai
Spring framework Training in Chennai
Struts Training in Chennai
Wordpress Training in Chennai
Hibernate Training in Chennai