首页 技术 正文
技术 2022年11月20日
0 收藏 782 点赞 4,020 浏览 3116 个字

第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

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

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,000
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,512
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,358
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,141
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,771
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,849