1. Front controller (a servlet)
Configure in the web.xml file.
If there is no init-param specified configuration file, you need to create an xml file with the same name as the servlet under WEB-INF.
For example: if there is no init-param in the following example, you need to create a springMVC-servlet.xml file
2. Configure in the configuration file established or specified in 1.
View parser and packet scanning
Package scanning:
When context:include-filter is used, the default behavior is prohibited. use-default-filters="false"
When context:exclude-filter is used, there is no need to prohibit the default behavior
Configure the view resolver:
Because all requests are intercepted, if you access the static resource file, a 404 error will be reported. Solution:
Import the mvc namespace:
add: (because access to the static resource file is processed by tomcat, and the project does not concern these resources Access configuration)
<!-- 告诉SpringMVC,自己 映射的自己处理,不能自己映射的交给tomcat处理-->
<mvc:default-servlet-handler/>
Disadvantages: If you add this directly in this way, you can only guarantee that static resources can be accessed, and dynamically mapped ones cannot be accessed.
Need to add:
<mvc:annotation-driven></mvc:annotation-driven>
3. Filter writing. Written in web.xml file
Character encoding filter for front-end requests
<!-- 字符编码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>/*</url-pattern>
</filter-mapping>
If it is a Rest style, you need to add a filter.
<!-- 支持Rest风格的filter,字符编码在前 -->
<filter>
<filter-name>HiddenHttpMethodFilter</filter-name>
<filter-class>org.springframework.web.filter.HiddenHttpMethodFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>HiddenHttpMethodFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Examples of Rest style: