shiro+SpringMVC 项目 配置指定的404页面

说起配置404,大家应该都会到web.xml中去配置。

 <error-page>
        <error-code>404</error-code>
        <location>/404.html</location>
 </error-page>

可当遇到我有业务需求,要记录下相关的信息,或者说404的展示页面有动态获取的资源,那么静态页面就力不从心了。

首先在Controller中写一个404的方法。

    @RequestMapping("/404")
    public String notFound(){
        return "404";
    }

代码是SpringMvc的,这里就不解释了。

接下来web.xml这里配置。(一直以为这里只可以配置静态资源,到今天才知道可以这样子)。如下

    <error-page>
        <error-code>404</error-code>
        <!-- 这里可以为servlet,不一定非要静态资源 -->
        <location>/404</location>
    </error-page>

如果你的项目中没有使用shiro就可以到这里结束了,使用了shiro继续看下去。

Shiro项目会报错。

org.apache.shiro.UnavailableSecurityManagerException: No SecurityManager accessible to the calling code

在网上查询,在web.xml中应该这个样子配置shiro。

    <!-- Shiro Filter is defined in the spring application context: -->
    <filter>
        <filter-name>shiroFilter</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
        <init-param>
            <param-name>targetFilterLifecycle</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>shiroFilter</filter-name>
        <url-pattern>/*</url-pattern>
        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>
    </filter-mapping>

重点是这几行代码。

        <dispatcher>REQUEST</dispatcher>
        <dispatcher>FORWARD</dispatcher>
        <dispatcher>INCLUDE</dispatcher>
        <dispatcher>ERROR</dispatcher>

到此在运行项目,404页面完美运行。

转载于:https://www.cnblogs.com/LUA123/p/6950904.html 侵权删除

原文地址:https://www.cnblogs.com/NotPig/p/13323353.html