您的位置:首页 > 运维架构 > Tomcat

三种思路实现自定义404页面:Tomcat、SpringMVC精准匹配、重写DispatchServlet

2015-03-17 21:10 399 查看
第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方法。 参考资料:http://blog.csdn.net/u012345283/article/details/39718245
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: