1. Create a Maven project and select Skeleton creation, Web-app

2. Improve the directory structure by adding Java and Resources

3. Maven imports dependencies

<! -- Version Control -->
<properties>
  <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  <maven.compiler.source>1.8</maven.compiler.source>
  <maven.compiler.target>1.8</maven.compiler.target>
  <spring.version>5.0.2. RELEASE</spring.version>
</properties>

<dependencies>
  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>${spring.version}</version>
  </dependency>

  <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>servlet-api</artifactId>
    <version>2.5</version>
    <scope>provided</scope>
  </dependency>

  <dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.0</version>
    <scope>provided</scope>
  </dependency>
</dependencies>
Copy the code

4. Configure the front-end controller in WEB_INF>>>web.xml

<web-app>
  <display-name>Archetype Created Web Application</display-name>

  <filter>
    <filter-name>characterEncodingFilter</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>
  </filter>
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>

  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
</web-app>
Copy the code

5. Configure the Spring Resources >>> Springmvc. XML file

What it does: Turns on annotation scanning and loads the class into the container by calling springMVC.xml from the front controller


      
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       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.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">

    <! -- Configure the package to scan when Spring creates containers -->
    <context:component-scan base-package="com.qunar"></context:component-scan>
    <! -- Configure Spring to enable annotated MVC support -->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>
Copy the code

6. To configure tomcat

7. Write code

@Controller
@RequestMapping(path = "/test")
@ResponseBody
public class HelloController {
    @RequestMapping(path="/hello")
    public String sayHello(String info){
        System.out.println("hello springMVC"+info);
        return "success";
    }
    @RequestMapping(path = "/testRequestMapping")
    public String testRequestMapping(a){
        System.out.println("Test RequeMapping annotations");
        return "success"; }}Copy the code

Request: localhost: 8080 / test/hello? Info = input information Request response: Hello springMVC input information

8. Springmvc executes the process

9. Solutions to Chinese garbled characters

  • Post request mode: Solve the problem by configuring filters
<web-app>
  <display-name>spring_study</display-name>

  <! -- Configure front-end filter -->
  <filter>
    <filter-name>characterEncodingFilter</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <! Set the value of the attribute in the filter -->
    <init-param>
      <param-name>encoding</param-name>
      <param-value>UTF-8</param-value>
    </init-param>
    <! -- Start filter -->
    <init-param>
      <param-name>forceEncoding</param-name>
      <param-value>true</param-value>
    </init-param>
  </filter>
  <! Filter all requests -->
  <filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/</url-pattern>
  </filter-mapping>


  <! -- Configure front-end controller -->
  <servlet>
    <servlet-name>dispatcherServlet</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>classpath:springmvc.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>dispatcherServlet</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>

</web-app>
Copy the code

This can be configured in the SpringMVC configuration file, static resources are not filtered:

<mvc:resources location="/css/" mapping="/css/**"/>
<mvc:resources location="/images/" mapping="/images/**"/>
<mvc:resources location="/scripts/" mapping="/javascript/**"/>
Copy the code
  • Get mode: Get request encoding problem, need to change Tomcat server.xml
<Connector connectionTimeout="20000" port="8080" protocol="HTTP / 1.1" redirectPort="8443"/>To:<Connector connectionTimeout="20000" port="8080" protocol="HTTP / 1.1" redirectPort="8443" useBodyEncodingForURI="true"/>URIEncoding=" utF-8 "if ajax requests are still garbled, change :useBodyEncodingForURI="true" to URIEncoding="UTF-8".Copy the code