91-9990449935 0120-4256464 |
Spring MVC TutorialSpring 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 MVCAs 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 ExampleLet's see the simple example of spring 3 web MVC. There are given 7 steps for creating the spring MVC application. The steps are as follows:
Directory StructureRequired Jar filesTo run this example, you need to load:
1) 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 classTo 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 fileIn 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 fileThis 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 pageThis 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.jspOutputDownload spring MVC exampleWe have created this application in MyEclipse IDE which already provides the jar files. If you use eclipse or other IDE's, you need to load the jar file for spring MVC. Spring 3 MVC Multiple Controller ExampleWe can have a lot of controller classes in Spring Framework. In this example, we are creating two Controller classes HelloWorldController and WelcomeWorldController. 1) Controller ClassesHelloWorldController.java WelcomeWorldController.java2) View componentsTo run this example, It must be located inside the WEB-INF/jsp directory. hellopage.jsp welcomepage.jsp3) Index pageIt is the optional welcome page, that provide the links to invoke both controller. index.jspOther pages are same e.g. spring-servlet.xml and web.xml.Spring MVC Login ExampleWe can simply create login application by following the Spring MVC. We need to pass HttpServletRequest and HttpServletResponse objects in the request processing method of the Controller class. Let's see the example: 1) Controller ClassHelloWorldController.java2) View componentsTo run this example, It must be located inside the WEB-INF/jsp directory. hellopage.jsp errorpage.jsp3) Index pageIt is the login page, that recieve name and password from the user. index.jspOther pages are same e.g. spring-servlet.xml and web.xml.Spring MVC Form ExampleBy the help of form handling, we will be able to create CRUD application using Spring MVC. Click here for more details about spring mvc form handling example. Spring MVC CRUD ExampleBy the help of form handling and JdbcTemplate, we will be able to create CRUD application using Spring MVC. Click here for more details about spring mvc crud example. Spring MVC Pagination ExampleLet's see a simple example of pagination using Spring MVC. Click here for more details about spring mvc pagination example. Spring MVC File Upload ExampleLet's see a file upload application using Spring MVC. Click here for more details about spring mvc file upload example. Spring MVC Tiles ExampleBy the help of Tiles framework, we can manage the layout of the spring mvc web application. Click here for more details about spring mvc example with tiles.
Next TopicSpring MVC Form Handling Example
|