三思考,实现自己定义404页:Tomcat、SpringMVC精确匹配、重写DispatchServlet

时间:2022-02-21 15:16:37
第1种方式:Tomcat直接处理
web.xml
<error-page>
<error-code>404</error-code>
<location>/error/404.htm</location>
</error-page>

这样的仅仅能展示纯静态的页面,很不灵活。
第2种方式:利用Spring MVC的最精确匹配
@Controller
public class UrlNotFoundController {
@RequestMapping("*")
public String test404(){
//TODO
return "404Page";
}
}

在网上找到这样的方法。利用SpringMVC的精确匹配,从而在其他Controller找不到相应请求的时候。来处理404。
可是,这样的方式也有问题,仅仅能拦截一部分。
比方,假设有这个一个Controller
@Controller("/home")
public class HomeController{
@RequestMapping("a")
public String a(){
//
}
}

直接訪问: http://localhost:8080/b.html,会被UrlNotFoundController处理。
可是http://localhost:8080/home/b.html,就不会被UrlNotFoundController处理。
这说明。通过精准匹配也是有局限性的。
第3种方式:自己定义org.springframework.web.servlet.DispatcherServlet。重载noHandlerFound方法。
<servlet>
<servlet-name>theDispatcher</servlet-name>
<servlet-class>base.web.MyDispatchServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring/spring-mvc-servlet.xml</param-value>
</init-param>
<init-param>
<param-name>fileNotFondUrl</param-name>
<param-value>/error/404</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>theDispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>

public class MyDispatchServlet extends DispatcherServlet {

	private static final long serialVersionUID = 1L;

	private static final UrlPathHelper urlPathHelper = new UrlPathHelper();

	private String fileNotFondUrl = "/error/404.html";

	public void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (pageNotFoundLogger.isWarnEnabled()) {
String requestUri = urlPathHelper.getRequestUri(request);
pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + requestUri +
"] in DispatcherServlet with name '" + getServletName() + "'");
} response.sendRedirect(request.getContextPath() + fileNotFondUrl);
} public String getFileNotFondUrl() {
return fileNotFondUrl;
} public void setFileNotFondUrl(String fileNotFondUrl) {
this.fileNotFondUrl = fileNotFondUrl;
} }

默认的DispatchServlet的noHandlerFound方法。
protected void noHandlerFound(HttpServletRequest request, HttpServletResponse response) throws Exception {
if (pageNotFoundLogger.isWarnEnabled()) {
String requestUri = urlPathHelper.getRequestUri(request);
pageNotFoundLogger.warn("No mapping found for HTTP request with URI [" + requestUri +
"] in DispatcherServlet with name '" + getServletName() + "'");
}
response.sendError(HttpServletResponse.SC_NOT_FOUND);
}
直接返回HTTP404。
特别须要说明的是:
  自己定义之后,不能再使用
  <!-- <mvc:default-servlet-handler /> -->
  
  通常情况下。使用这个配置,能够让SpringMVC相应js、css等静态页面,在合适的路径,自己主动去找。
  凝视之后,就仅仅能手动响应静态资源等请求了。
  2种方式:
  第1种:Tomcat处理。
  配置
  <servlet-mapping>
<servlet-name>default</servlet-name>
<url-pattern>/static/*</url-pattern>
</servlet-mapping>

  
  第2种:SpringMVC处理
  <mvc:resources mapping="/kindeditor/upload/image/**"
location="file:${kindeditorImagePath}/kindeditor/upload/image/**" />
  
  假设使用了“<mvc:default-servlet-handler />”
// Determine handler for the current request.
mappedHandler = getHandler(processedRequest, false);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
noHandlerFound(processedRequest, response);
return;
}
  DispatchServlet上述代码的mappedHandler就不为空了。因此无法进入noHandlerFound方法。
  

版权声明:本文博主原创文章。博客,未经同意不得转载。