Spring MVC and springmvc
1. Create a Java Web project 1. Create a Java project. 2. Create a folder webapp under the project (name can be retrieved, this directory is the root directory of the website), and then create a new WEB-INF folder under the folder (name is fixed ), the WEB-INF is the secure folder after Tomcat is started, and the browser cannot access resources under this directory. By default, tomcat will read the corresponding configuration file in this directory. 3. Create a web. xml file (named fixed) under the WEB-INF, copy the basic document structure from the tomcat installation path/conf/web. xml, modify the corresponding encoding to UTF-8. After tomcat is started, it will load the large configuration file web. xml under the installation path. When we find that the WEB-INF of our project also has web. xml, it will be loaded as a configuration file. 4, in the WEB-INF to create a jsp folder (name can be taken), used to store the relevant jsp file (MVC View ). 5. Create a lib folder under the WEB-INF (named fixed) to store the relevant jar package. 6. Create a web. xml file under the WEB-INF (named fixed ). Ii. Spring InstallationOfficial: Http://maven.springframework.org/release/org/springframework/spring/1← download documents from the official website, such as spring-framework-4.1.9.RELEASE-dist.zip. 2. decompress and copy the jar package under the \ spring-framework-4.1.9.RELEASE \ libs directory to the lib directory of the project: (1) about servletservlet is a small program running on the server, for example, a server program written in java. The process is as follows: 1. The client (browser) accesses the website and sends a request to the server (tomcat or weblogic); 2. the server sends the request information to the servlet. 3. The servlet generates the response content (the program control response we write) and sends it to the server. 4. The server returns the response to the client. 5. display the client. 3. Spring configuration (1), web. xml configuration 1. Open the web in the project. add a Spring servlet in xml (of course,/conf/web in tomcat. xml is also acceptable, but it is generally not), so that the servlet is loaded when the program starts. <Servlet> <servlet-name> spring </servlet-name> <servlet-class> org. springframework. web. servlet. dispatcherServlet </servlet-class> <load-on-startup> 1 </load-on-startup> </servlet> the load-on-startup element indicates whether the container is started. load this servlet1) the value must be an integer, indicating the order in which the servlet should be loaded. 2) when the value is 0 or greater than 0, it indicates that the container loads and initializes the servlet when the application starts. 3) if the value is smaller than 0 or is not specified, the container is loaded only when the servlet is selected. 4) The smaller the positive value, the higher the servlet priority, the more loaded the application is at startup. 5) when the values are the same, the container selects the order for loading. Therefore, the values of x in <load-on-startup> x </load-on-startup>, 1, 2, 4, and 5 represent the priority rather than the start delay time. 2. Set spring url interception: <servlet-mapping> <servlet-name> spring </servlet-name> <url-pattern> *. do </url-pattern> </servlet-mapping> <servlet-name> spring </servlet-name> can be called by the spring name, you only need to match the servlet name configured above. It indicates that all requests whose URLs are. do with the suffix (the suffix can be obtained, but are generally named as. do) must be controlled by spring. 3. Configure the character encoding filter <filter> <filter-name> encodingFilter </filter-name> <filter-class> org. springframework. web. filter. characterEncodingFilter </filter-class> <init-param> <param-name> encoding </param-name> <param-value> UTF-8 </param-value> </init- param> <init-param> <param-name> forceEncoding </param-name> <param-value> true </param-value> </init-param> </filter> <filter-mapping> <filter-name> encodingFilter </filter -Name> <url-pattern> *. do </url-pattern> </filter-mapping> 4. Create a WEB-INF under the spring-servlet.xml (name (spring) and web. spring name in <servlet-name> spring </servlet-name> configured in xml must correspond to): (2), Spring-servlet.xml configuration this file mainly completes the url ing configuration; page Directory configuration and controller configuration. That is, start our MVC. 1. Page architecture (copy from documents and manuals) <? Xml version = "1.0" encoding = "UTF-8"?> <Beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance" xsi: schemaLocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd"> </beans> 2. page Directory configuration. <Bean class = "org. springframework. web. servlet. view. internalResourceViewResolver "> <property name =" prefix "value ="/WEB-INF/jsp "/> <property name =" suffix "value = ". jsp "/> </bean> Configure the view parser to specify the Directory and file Suffix of the controller page redirection. 3. url ing configuration. <Bean id = "urlMapping" class = "org. springframework. web. servlet. handler. simpleUrlHandlerMapping "> <property name =" mappings "> <props> <prop key ="/index. do "> IndexAction </prop> </props> </property> </bean> SimpleUrlHandlerMapping provides the simplest URL ing that maps the URL to the Controller through Properties. One. do maps one controller. 4. Configure <bean id = "IndexAction" class = "com. myweb. indexAction "> <property name =" view "> <value> index </value> </property> </bean> Configure attributes in bean (including attributes in bean format) in the java file corresponding to the bean, a property value with the same name as the property name must be defined, and the getter and setter methods with the property value must be included. 5. Write the controller according to com. myweb. indexAction to complete the servlet that processes the request. 6. Add the necessary dependency packages for spring Project startup: 7. The spring MVC environment has been set up. Result: