配置WEB错误页面

  项目运行时,难免会出现错误,这些错误我们不可以也不方便直接让用户看到,所以配置错误页面是非常必要的。

  一下是项目的Web.xml文件,在最下方阴影部分是配置错误界面。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0" metadata-complete="false">

    <display-name>esp-web</display-name>

    <!-- Spring配置文件开始 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
             classpath:spring-common.xml,
             classpath:spring-config.xml
         </param-value>
    </context-param>
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <!-- Spring配置文件结束 -->

    <!-- 可以使用RequestContextHolder.currentRequestAttributes() 获取到请求的attr -->
    <listener>
        <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
    </listener>

    <!-- 设置servlet编码开始 -->
    <filter>
        <filter-name>Set Character Encoding</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Set Character Encoding</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    <!-- 设置servlet编码结束 -->

    <!-- 记录访问日志,比如审计之类的可用 -->
    <filter>
        <filter-name>AccessLogFilter</filter-name>
        <filter-class>com.pcitc.common.web.filter.AccessLogFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>blackListURL</param-name>
            <param-value>/static/**,*.js,*.css,*.jpg,*.gif,*.png,*.ico,/admin/monitor/druid/**</param-value>
        </init-param>
        <init-param>
            <param-name>whiteListURL</param-name>
            <param-value>/**</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>AccessLogFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>

    <!-- shiro 安全过滤器 -->
    <!-- The filter-name matches name of a 'shiroFilter' bean inside applicationContext.xml -->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <!-- Make sure any request you want accessible to Shiro is filtered. /* 
        catches all -->
    <!-- requests. Usually this filter mapping is defined first (before all 
        others) to -->
    <!-- ensure that Shiro works in subsequent filters in the filter chain: -->
    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
    </filter-mapping>


    <!-- 验证码过滤器需要放到Shiro之后 因为Shiro将包装HttpSession 如果不 可能造成两次的sesison id 不一样 -->
    <filter>
        <filter-name>JCaptchaFilter</filter-name>
        <filter-class>com.pcitc.common.web.jcaptcha.JCaptchaFilter</filter-class>
        <async-supported>true</async-supported>
    </filter>
    <filter-mapping>
        <filter-name>JCaptchaFilter</filter-name>
        <url-pattern>/jcaptcha.jpg</url-pattern>
    </filter-mapping>

    <!--druid 监控 -->
    <filter>
        <filter-name>DruidWebStatFilter</filter-name>
        <filter-class>com.alibaba.druid.support.http.WebStatFilter</filter-class>
        <async-supported>true</async-supported>
        <init-param>
            <param-name>exclusions</param-name>
            <param-value>/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*</param-value>
        </init-param>
        <init-param>
            <param-name>principalSessionName</param-name>
            <param-value>username</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>DruidWebStatFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- druid监控 -->
    <servlet>
        <servlet-name>DruidStatView</servlet-name>
        <servlet-class>com.alibaba.druid.support.http.StatViewServlet</servlet-class>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>DruidStatView</servlet-name>
        <url-pattern>/admin/monitor/druid/*</url-pattern>
    </servlet-mapping>


    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    <!-- 自定义tld标签 -->
    <jsp-config>
        <taglib>
            <taglib-uri>http://mytag.sf.net</taglib-uri>
            <taglib-location>/WEB-INF/mytag.tld</taglib-location>
        </taglib>
    </jsp-config>

    <!-- 错误页面映射 -->
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/jsp/error/error.jsp</location>
    </error-page>
    <error-page>
        <error-code>400</error-code>
        <location>/WEB-INF/jsp/error/error.jsp</location>
    </error-page>
    <error-page>
        <error-code>500</error-code>
        <location>/WEB-INF/jsp/error/error.jsp</location>
    </error-page>
    <error-page>
        <error-code>503</error-code>
        <location>/WEB-INF/jsp/error/error.jsp</location>
    </error-page>
    <error-page>
        <exception-type>java.lang.Exception</exception-type>
        <location>/WEB-INF/jsp/error/error.jsp</location>
    </error-page>

    <!--如果 发现报 该错误 No mapping found for HTTP request with URI 加入下mapping <servlet-mapping> 
        <servlet-name>jsp</servlet-name> <url-pattern>*.jsp</url-pattern> </servlet-mapping -->

</web-app>
原文地址:https://www.cnblogs.com/kuoAT/p/8182983.html