First, the use of static resources
There is the following code snippet in WebConfig.java
1 @Override // 配置静态资源处理 2 public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { 3 // TODO Auto-generated method stub 4 configurer.enable(); 5 }
At this point, a default Handler will be registered: DefaultServletHttpRequestHandler
, this Handler is also used to process static files, and it will try to map /*
. When the DispatcherServelt is mapped /
( /
and /*
is different), and no suitable Handler is found to process the request, it will be handed over DefaultServletHttpRequestHandler
to be processed. Note: The static resources here are placed in the web root directory, not under WEB-INF.
Maybe the description here is a bit difficult to understand (I think so too), so just give an example, for example: there is a picture in the webroot directory: 1.png
we know that the files in the web root directory (webroot) in the Servelt specification can be accessed directly , but because the DispatcherServlet
configured mapping path is: /
, it intercepts almost all requests, resulting in 1.png
inaccessibility, then registering one DefaultServletHttpRequestHandler
can solve this problem. In fact, it can be understood as a DispatcherServlet
broken Servlet
feature (the files in the root directory can be accessed directly), DefaultServletHttpRequestHandler
which helps to return this feature.
Q: What is the difference between /
and /*
?
Answer: /
All urls except jsp will be intercepted, and all urls /*
will be intercepted, including jsp. For example: there is a test.jsp under the webroot, when DispatcherServlet
configuring the mapping /
, the browser enters: http://localhost:8083/test.jsp This jsp can be accessed directly and does not go through DispatcherServlet
, and when DispatcherServlet
configuring the mapping /*
, this The request will be DispatcherServlet
blocked.
Use JSTL tags:
<%@ taglib uri="http://www.springframework.org/tags" prefix="s"%>
<img src="<s:url value="/resources/profile/images/profile_img.png" />" > Use the images in the corresponding directory, and use other static resources similarly.
2. Dealing with Chinese garbled characters - the solution for garbled characters after the form submission controller obtains Chinese parameters
Note: jsp page encoding is set to UTF-8
1. The form submission method must be post, and the spring coding filter under the get method has no effect
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<form action="${ctx}/user/addUser" name="userForm" method="post">
2. Modify web.xml and add an encoding filter as follows (note that the value of the forceEncoding parameter needs to be set to true) web.xml is in the WEB-INF directory
<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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>