First, web.xml configuration
<!-- spring-mvc --><servlet><servlet-name>springServlet</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class><init-param><param-name>contextConfigLocation</param-name><param-value>cl asspath:springmvc-servlet.xml</param-value></init-param><load-on-startup>1</load-on-startup></servlet><servlet-mapping><servlet-name>springServlet</servlet-name><url-pattern>/</url-pattern></servlet-mapping>
url-pattern configuration/, if configured/*, you will ask questions, and even return to the view jsp will be intercepted. For the specific reason, please see the source code
Question: If you configure this way, everyone will ignore a problem, that is, a/b/c can be accessed, but a/b/c.do; a/b/c.mm; a/b/c.zz can all access a/b/c controller. Without modifying the source code, we will
Solve this problem. Although we intercept / such requests, we only let requests like a/b/c enter the controller. If the suffix of [.] is suffixed, we will not let it enter. We will go directly to page 404.
Solution:
Add to filter on web.xml
<!-- Simple filtering of url--><filter><filter-name>servletRequest</filter-name><filter-class>cn.tomcat.quickstart.common.filter.ServletRequestFilter</filter-class></filter><filter-mapping><filter-name>servletRequest</filter-name><url-pattern>/*</url-pattern></filter-mapping>ServletRequestFilter.javaimport java.io.IOException;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import org.springframework.web.filter.OncePerRequestFilter;/*** * Process the incoming url and put it in front of the filter, springmvc <url-pattern>/</url-pattern>* By default, all requests are intercepted, ex:a/b/c.do,a/b/c.html,a/b/c.action,a/b/c* Remove access with suffix, imitate restful style, and only accept requests from a/b/c* */public class ServletRequestFilter extends OncePerRequestFilter {@Overrideprotected void doFilterInternal(HttpServletRequest request,HttpServletResponse response, FilterChain filterChain)throws ServletException, IOException {String requestPath = request.getServletPath();//Don't worry about intercepting static files. For example, *.js, *.css is similar to this, and is processed in web.xml // All requests with the suffix [.] are directly used to go to 404, and do not accept such requests if(requestPath.lastIndexOf(".")!=-1){request.getRequestDispatcher("/WEB-INF/pages/error/404.jsp").forward(request, response);}else{filterChain.doFilter(request, response);}}}}} Direct go 404 with the ending, haha, maybe you would ask if this is the case, wouldn’t the static file be filtered out? Similar to *.js, *.css,...N many such types of files