Spring mvc, springmvc
1.1. spring mvc 1.1.1. MVC: Model-View-Control
The main work to be done at the C-layer of the framework is to encapsulate web requests as a data object, call the business logic layer to process data objects, return data processing results, and send corresponding views to users.
1.1.2. Brief Introduction spring mvc
The core of the Spring C-layer framework is DispatcherServlet, which distributes requests to different backend processors, that is, a mode called FrontController is used (this mode is briefly described later ). Spring's C-layer framework uses backend controllers, ing processors, and view parsers to complete the main work of the C-layer framework. In addition, the C-layer framework of spring is really assembling the data results processed by the business layer and the corresponding views into an object, that is, the ModelAndView object we will often use later.
1.2. Spring MVC entry case 1.2.1. Create a web project 1.2.2. Introduce spring jar package and log4j package
File directory structure
1.2.3. Configure web. xml
<? Xmlversion ="1.0"Encoding =UTF-8"?> <Web-appxmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance" Xmlns =Http://java.sun.com/xml/ns/javaee" Xsi: schemaLocation =Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" Id ="WebApp_ID"Version ="3.0"> <Display-name> HelloSpringMVC </display-name> <Welcome-file-list> <Welcome-file> index. jsp </welcome-file> </Welcome-file-list> <! -- Configure springMvcCoreServlet--> <Servlet> <Servlet-name> spring </servlet-name> <Servlet-class> org. springframework. web. servlet. DispatcherServlet </servlet-class> <Init-param> <Param-name> contextConfigLocation </param-name> <! -- Specify springMvcThe path to the configuration file defaults to/WEB-INF/<[Servlet-Name]-servlet. xml> example here is/WEB-INF/spring-servlet.xml The following is the default value --> <Param-value>/WEB-INF/spring-servlet.xml </param-value> </Init-param> <! -- Configure Automatic initialization with server startup --> <Load-on-startup> 1 </load-on-startup> </Servlet> <Servlet-mapping> <Servlet-name> spring </servlet-name> <! -- Filter all requests whose suffix is. do --> <Url-pattern> *. do </url-pattern> </Servlet-mapping> </Web-app> |
1.2.4. Create a Controller that implements the Controller Interface
Package com. morris. controller; Import javax. servlet. http. HttpServletRequest; Import javax. servlet. http. HttpServletResponse; Import org. springframework. web. servlet. ModelAndView; Import org. springframework. web. servlet. mvc. Controller; Public class HelloController implements Controller { Public ModelAndView handleRequest (HttpServletRequest arg0, HttpServletResponse arg1) throws Exception { ModelAndView mav = new ModelAndView ("hello. jsp "); Mav. addObject ("message", "Hello World "); Return mav; } } |
1.2.5. Configure the spring-servlet.xml File
<? Xmlversion ="1.0"Encoding =UTF-8"?> <Beansxmlns =Http://www.springframework.org/schema/beans" Xmlns: xsi =Http://www.w3.org/2001/XMLSchema-instance"Xmlns: p =Http://www.springframework.org/schema/p" Xmlns: mvc =Http://www.springframework.org/schema/mvc"Xmlns: context =Http://www.springframework.org/schema/context" Xmlns: util =Http://www.springframework.org/schema/util" Xsi: schemaLocation =Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd Http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd Http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <Beanid ="SimpleUrlHandlerMapping" Class ="Org. springframework. web. servlet. handler. SimpleUrlHandlerMapping"> <Propertyname ="Mappings"> <Props> <Propkey ="/Hello. do"> HelloControl </prop> <! -- Note: The hello. do request will be processed by the bean named helloControl. --> </Props> </Property> </Bean> <Beanid ="HelloControl"Class ="Com. morris. controller. HelloController"> </Bean> </Beans> |
1.2.6. Test
Start tomcat and enter http: // localhost: 8080/HelloSpringMVC/hello. do in the browser.
The following interface is displayed: