Hyderabad, , India
Hibernate, Core Java, JSP
0 Comment | 46 Views | 12 Mar 13  | Pallavi N
Spring Model_View_Contoller
Spring MVC Framework Spring MVC helps in building flexible and loosely coupled web applications. The Model-view-controller design pattern helps in seperating the business logic, presentation logic and navigation logic. Models are responsible for encapsulating the application data. The Views render response to the user with the help of the model object . Controllers are responsible for receiving the request from the user and calling the back-end service. When a request is sent to the Spring MVC Framework the following sequence of events happen. The DispatcherServlet first receives the request. The DispatcherServlet consults the HandlerMapping and invokes the Controller associated with the request. The Controller process the request by calling the appropriate service methods and returns aModeAndView object to the DispatcherServlet. The ModeAndView object contains the model data and the view name. The DispatcherServlet sends the view name to a ViewResolver to find the actual View to invoke. Now the DispatcherServlet will pass the model object to the View to render the result. The View with the help of the model data will render the result back to the user. To understand the Spring MVC Framework we will now create a simple hello world example using the Eclipse IDE. I am using Exclipse IDE 3.4 , Spring IDE plugin, Tomcat 6.0 and Spring 3.0 to demonstrate this example. Go to File -gt New -gt Dynamic Web Project, to create a web project. Enter the project name and click the Finish button. Right click the project folder, and select Spring Tools -gt Add Spring Project Nature, to add Spring capabilities to the web project. This feature will be available once you install the Spring IDE. Create a new package com.vaannila inside the src directory. The Spring controller class extends org.springframework.web.servlet.mvc.AbstractController class. To create a new controller class right click the src directory and create a new java class, enter the controller class name and super class name and the Finish button. Copy the following code inside the HelloWorldController class. view source print 01.import javax.servlet.http.HttpServletRequest 02.import javax.servlet.http.HttpServletResponse 03. 04.import org.springframework.web.servlet.ModelAndView 05.import org.springframework.web.servlet.mvc.AbstractController 06. 07.public class HelloWorldController extends AbstractController 08. 09.private String message 10. 11.Override 12.protected ModelAndView handleRequestInternalHttpServletRequest request, HttpServletResponse response throws Exception 13.return new ModelAndViewquotwelcomePagequot,quotwelcomeMessagequot, message 14. 15. 16.public void setMessageString message 17.this.message message 18. 19. 20. The DispatcherSevlet, as the name indicates, is a single servlet that manages the entire request-handling process. When a request is sent to the DispatcherServlet it delegates the job by invoking the appropriate controllers to process the request. Like any other servlet the DispatcherServlet need to be configured in the web deployment descriptor as shown. view source print 01.ltxml versionquot1.0quot encodingquotUTF-8quotgt 02.ltweb-app xmlnsxsiquothttp//www.w3.org/2001/XMLSchema-instancequotxmlnsquothttp//java.sun.com/xml/ns/javaeequotxmlnswebquothttp//java.sun.com/xml/ns/javaee/web-app25.xsdquotxsischemaLocationquothttp//java.sun.com/xml/ns/javaeehttp//java.sun.com/xml/ns/javaee/web-app25.xsdquot idquotWebAppIDquotversionquot2.5quotgt 03.ltservletgt 04.ltservlet-namegtdispatcherlt/servlet-namegt 05.ltservlet-classgt org.springframework.web.servlet.DispatcherServlet lt/servlet-classgt 06.ltload-on-startupgt1lt/load-on-startupgt 07.lt/servletgt 08.ltservlet-mappinggt 09.ltservlet-namegtdispatcherlt/servlet-namegt 10.lturl-patterngt.htmlt/url-patterngt 11.lt/servlet-mappinggt 12.ltwelcome-file-listgt 13.ltwelcome-filegtredirect.jsplt/welcome-filegt 14.lt/welcome-file-listgt 15.lt/web-appgt Here the servlet name is dispatcher. By default the DispatcherServlet will look for a file name dispatcher-servlet.xml to load the Spring MVC configuration. This file name is formed by concatenating the servlet name quotdispatcherquot with quot-servlet.xmlquot. Here we user the the url-pattern as quot.htmquot inorder to hide the implementations technology to the users. The redirect.jsp will be invoked first when we execute the Spring web application. This is the only jspfile outside the WEB-INF directory and it is here to provide a redirect to the DispatcherServlet. All the other views should be stored under the WEB-INF directory so that they can be invoked only through the controller process. To create a bean configuration file right click the WebContent folder and select New -gt Other. The following dialog box appears. Select the Spring Bean Configuration file and click Next. Enter the file name as quotdispatcher-servlet.xmlquot and click the Finish button. Now the Spring bean configuration file is created, we need to configure the Controller and theViewResolver classes. The following code shows how to do this. view source print 01.ltbean idquotviewResolverquot 02.classquot org.springframework.web.servlet.view. InternalResourceViewResolverquot gt 03.ltproperty namequotprefixquotgt 04.ltvaluegt/WEB-INF/jsp/lt/valuegt 05.lt/propertygt 06.ltproperty namequotsuffixquotgt 07.ltvaluegt.jsplt/valuegt 08.lt/propertygt 09.lt/beangt 10. 11.ltbean namequot/welcome.htmquot classquotcom.vaannila.HelloWorldControllerquot gt 12.ltproperty namequotmessagequot valuequotHello Worldquot /gt 13.lt/beangt 14. 15.lt/beansgt First let39s understand how to configure the controller. 1.ltbean namequot/welcome.htmquot classquotcom.vaannila.HelloWorldControllerquot gt 2.ltproperty namequotmessagequot valuequotHello Worldquot /gt 3.lt/beangt Here the name attribute of the bean element indicates the URL pattern to map the request. Since the id attribute can39t contain special characters like quot/quot , we specify the URL pattern using the name attribute of the bean element. By default the DispatcherServlet uses the BeanNameUrlHandlerMapping to map the incoming request. The BeanNameUrlHandlerMapping uses the bean name as the URL pattern. Since BeanNameUrlHandlerMapping is used by default, you need not do any seperate configuration for this. We set the message attribute of the HelloWorldController class thru setter injection. TheHelloWorldController class is configured just like an another JavaBean class in the Spring application context, so like any other JavaBean we can set values to it through Dependency InjectionDI. The redirect.jsp will redirect the request to the DispatcherServlet, which inturn consults with theBeanNameUrlHandlerMapping and invokes the HelloWorldController. The handleRequestInternalmethod in the HelloWorldController class will be invoked. Here we return the message property under the name welcomeMessage and the view name welcomePage to the DispatcherServlet. As of now we only know the view name, and to find the actual view to invoke we need a ViewResolver. The ViewResolver is configured using the following code. view source print 01.ltbean idquotviewResolverquot 02.classquot org.springframework.web.servlet.view.InternalResourceViewResolverquotgt 03.ltproperty namequotprefixquotgt 04.ltvaluegt/WEB-INF/jsp/lt/valuegt 05.lt/propertygt 06.ltproperty namequotsuffixquotgt 07.ltvaluegt.jsplt/valuegt 08.lt/propertygt 09.lt/beangt Here the InternalResourceViewResolver is used to resolve the view name to the actual view. The prefix value view name suffix value will give the actual view location. Here the actual view location is /WEB-INF/jsp/welcomePage.jsp The following library files are needed to run the example. view source print 01.antlr-runtime-3.0 02.commons-logging-1.0.4 03.org.springframework.asm-3.0.0.M3 04.org.springframework.beans-3.0.0.M3 05.org.springframework.context-3.0.0.M3 06.org.springframework.context.support-3.0.0.M3 07.org.springframework.core-3.0.0.M3 08.org.springframework.expression-3.0.0.M3 09.org.springframework.web-3.0.0.M3 10.org.springframework.web.servlet-3.0.0.M3

    • Social Share
      for this Blog

Porfolio And Blogs
Pallavi N follows various companies, these companies and employers can view Pallavi's resume.
Upload your resume to get best job opportunities.

Register free!