Spring MVC Article 1. Build Spring MVC Framework and Spring MVC Framework
This project aims to build a simple Spring MVC Framework and understand the basic configuration of Spring MVC.
I. project structure
This project is created using idea intellij and managed with maven. The java folder is the sources folder and resources is the resource folder. The spring folder contains the Spring context configuration and Spring MVC configuration files. Note that after the project is automatically generated, there will be two web file directories, one is the web folder (I have deleted it here) and the other is the default webapp folder. Here I use the default directory, that is, the webapp folder. All the page files are stored in this directory. * You can configure the default path for web files. *
Ii. Configuration File
Configure maven and add various DependenciesOmitted. This is not the focus. Note that packages of different versions cannot be added repeatedly when dependency is added.
Web. xml file configurationFirst, add the spring Context configuration, specifying that the Spring Context is provided by the app-context.xml in the spring folder under classpath:
<context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:spring/app-context.xml</param-value></context-param>
Then add the Spring listener:
<listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener> <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class></listener><listener> <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class></listener>
Next, configure Spring MVC's dispatherservlet and configure the URL to be intercepted by the servlet.
<Servlet> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: spring/mvc-servlet.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <! -- Configure the URL to intercept --> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern>/</url-pattern> </servlet -mapping>
Finally, configure a welcom-file-list.
All web. xml Code is as follows:
<? Xml version = "1.0" encoding = "UTF-8"?> <Web-appxmlns = "http://java.sun.com/xml/ns/j2ee" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" version = "2.4" xsi: schemaLocation = "http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <! -- Spring context configuration file --> <context-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: spring/app-context.xml </param-value> </context-param> <! -- Spring listener Configuration --> <listener-class> org. springframework. web. context. contextLoaderListener </listener-class> </listener> <listener-class> org. springframework. web. context. request. requestContextListener </listener-class> </listener> <! -- Spring anti-memory overflow listener --> <listener-class> org. springframework. web. util. IntrospectorCleanupListener </listener-class> </listener> <! -- Spring mvc servlet configuration file --> <servlet-name> springmvc </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <init-param> <param-name> contextConfigLocation </param-name> <param-value> classpath: spring/mvc-servlet.xml </param-value> </init-param> <load-on-startup> 1 </load-on-startup> </servlet> <! -- Configure the URL to intercept --> <servlet-mapping> <servlet-name> springmvc </servlet-name> <url-pattern>/</url-pattern> </servlet -mapping> <welcome-file-list> <welcome-file> </welcome-file-list> </web-app>
Configure the Spring MVC File
The following code configures automatic annotation, mvc resource reference, and view Parser:
<? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xmlns: mvc = "http://www.springframework.org/schema/mvc" xmlns: context = "http://www.springframework.org/schema/context" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http: // Www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <mvc: resources location ="/js/"mapping ="/js/** "/> <bean class =" org. springframework. beans. factory. annotation. autowiredAnnotationBeanPostProcessor "/> <mvc: annotation-driven/> <context: annotation-config/> <mvc: default-servlet-handler/> <! -- Add a component scan to make the annotation below the package take effect --> <context: component-scan base-package = "com. wxspringmvc. controller"/> <! -- Add a page view parser --> <bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/page/"/> <property name =" suffix "value = ". jsp "/> <property name =" contentType "value =" text/html; charset = UTF-8 "/> </bean> </beans>
Configure applicationContext. xml
The default context is provided here. You can directly use the generated file without additional configuration. The code is omitted. Note that applicationContext. xml must be configured. If not configured in the web. xml file, the system will automatically look under the WEB-INF directory.
View and ControllerHere, we use a simple user login. After that, the user name and password process will be displayed on the page. View: index. jsp
<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%> The form is submitted to the/user/index path using post.
Controller: UserController. java
@Controller@RequestMapping(value = "/user")public class UserController { @RequestMapping(value = "/index" ,method= RequestMethod.POST) public ModelAndView userIndex(String username,String password){ ModelAndView mav = new ModelAndView("user/success"); mav.addObject("username",username); mav.addObject("password",password); return mav; }}Here you can add a simple verification. If either the user name or password is empty, you cannot submit the Verification:
The code for modifying UserController. java is as follows:
@Controller@RequestMapping(value = "/user")public class UserController { @RequestMapping(value = "/index" ,method= RequestMethod.POST) public ModelAndView userIndex(String username,String password){ ModelAndView mav = new ModelAndView("user/success"); if(!matchParams( username, password)){ return new ModelAndView("/index"); } mav.addObject("username",username); mav.addObject("password",password); return mav; } private boolean matchParams(String username,String password){ if(isEmpty(username)||isEmpty(password)) return false; else return true; } private boolean isEmpty(String s){ if(s==null || "".equals(s)) return true; else return false; }}View: Successful Logon interface: success. jsp
<% @ Page contentType = "text/html; charset = UTF-8 "language =" java "%> Effect display
Incorrect page: the input is incorrect, and the submission result is: the input is correct and submitted: The above is a simple Spring MVC demonstration.