第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方法。
  

三种思路实现自定义404页面:Tomcat、SpringMVC精准匹配、重写DispatchServlet的更多相关文章

  1. Android 三种方式实现自定义圆形页面加载中效果的进度条

    转载:http://www.eoeandroid.com/forum.php?mod=viewthread&tid=76872 一.通过动画实现 定义res/anim/loading.xml如 ...

  2. 在 Tomcat 中自定义 404 页面(简单配置)

      打开 Tomcat 中的 web.xml,(tomcat/conf/web.xml) 添加如下代码: <error-page>  <error-code>404</e ...

  3. Web---演示Servlet的相关类、下载技术、线程问题、自定义404页面

    Servlet的其他相关类: ServletConfig – 代表Servlet的初始化配置参数. ServletContext – 代表整个Web项目. ServletRequest – 代表用户的 ...

  4. JavaWeb 自定义404页面

    本来,Tomcat中自定义404页面不过是在web.xml文件中写4行代码的事情. 直接引用 Tomcat官方FAQ 怎样自定义404页面? 编辑web.xml <error-page> ...

  5. IIS配置404页面配置,IIS自定义404页面

    .NET 环境下 用到404页的场景一般有两种: 场景一:报黄页,程序性的错误,代码层可以捕捉到的. 场景二:用户输入不存在的页面,代码层捕捉不到的. IIS 默认会有404的配置,不过这种呈现出的都 ...

  6. 通过修改 Apache 的配置文件 htaccess 文件实现自定义404页面

    最近在学习使用Apache服务器的配置,做一个记录. Apache下有个.htaccess文件,是Apache的一个特殊的配置文件.这个配置文件默认是没有的,要手动在各自的项目的根目录编写才行. 要实 ...

  7. 关于thinkphp5手动抛出Http异常时自定义404页面报错的问题

    在使用HttpException手动抛出异常时,希望跳转到自定义的错误页面,官方的文章中是这样描述的. 可以使用\think\exception\HttpException类来抛出异常 // 抛出 H ...

  8. iis7 设置自定义404页面无效解决方案

    想给自己做的的网站自定义一个404页面,开始 双击红框提示的错误页图标 双击上图红框提示的所示404行 修改上图红框提示的内容如下:我是直接在根目录放了一个自己做的404.html,实际情况要填写你自 ...

  9. 如何给webview页面自定义404页面

    //示例地图类 package com.can2do.doimobile.news; import android.os.Bundle; import android.os.Handler; impo ...

随机推荐

  1. 【LightOJ - 1205】Palindromic Numbers

    [链接]https://cn.vjudge.net/problem/LightOJ-1205 [题意] 求出L..R范围内的回文个数 [题解] 数位DP; 先求出1..x里面的回文串个数.则做一下前缀 ...

  2. Microsoft SQL Server Version List(SQL Server 版本)

    原帖地址 What version of SQL Server do I have? This unofficial build chart lists all of the known Servic ...

  3. python基础--数值类型和序列类型

    Python中数值类型:int(整数),float(浮点数),True/False(布尔值,首字母必须大写) int:1    #任意整数 float:2.3   #小数 python赋值: a = ...

  4. WCF REST (二)

    今天主要写下  POST等其他方式 发送请求 以及 流方式 文件的上传与下载 一.Post 提交数据 先来想下 POST和Get 的不同   Get 方式 我们直接通过 url  来传递参数   先来 ...

  5. logback--How do I configure an AsyncAppender with code? 转载

    原文地址:https://github.com/tony19/logback-android/issues/54 Please provide an example of how to configu ...

  6. PS中矢量形状图层的合并交叉等运算

    操作中将用到下图所示的几个按钮   图1.2 减去顶层形状图层为例 1. 上图中,选择矩形工具,以新建图层的形式,新建两个矩形的形状图层,如上右图. PS:可以Shift+A快捷键选中一个形状,然后填 ...

  7. 介绍linux设备驱动编程

    目前,Linux软件工程师大致可分为两个层次: (1)Linux应用软件工程师(Application Software Engineer):       主要利用C库函数和Linux API进行应用 ...

  8. POJ 1595 Prime Cuts (ZOJ 1312) 素数打表

    ZOJ:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=312 POJ:http://poj.org/problem?id=159 ...

  9. 【例题5-7 UVA - 136】Ugly Numbers

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 每个丑数x,都能生成3个丑数2x,3x,5x 则我们以1作为起点. 生成丑数. 每次取出set里面最小的那个数. 然后用它去生成其他 ...

  10. Java 接口中定义抽象方法有什么意义

    接口方法声明只能是public abstract的,所以不管你在声明的时候加不加abstract,都是可以的.Java 8开始,接口还引入了默认方法,也就是可以给接口的方法提供默认的实现,默认方法应当 ...