无意发现这个问题,由于web.xml里
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/500.html</location>
</error-page>
<error-page>
<error-code>500</error-code>
<location>/500.html</location>
</error-page>
<error-page>
<error-code>404</error-code>
<location>/404.html</location>
</error-page>
<error-page>
<error-code>405</error-code>
<location>/404.html</location>
</error-page>
配置一段类似这样的配置,tomcat内部会帮我们处理,这几种错误,自动跳转指定的页面错误页面。。
然后项目还配置了登陆的拦截器
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
System.out.println("------------------进入登录拦截器-------" + request.getRequestURI());
// redisSessionDao.loginToRedis(request.getRequestedSessionId(), 1);
Serializable attribute = sessionProvider.getAttribute(request,SessionConstants.FBD_ADMIN_USER_INFO);
if (attribute!=null) {
return true;
} else {
response.sendRedirect(request.getContextPath() + "/login");
}
return false;
}
sessionProvider 里提供了封装好的spring-session分布式session的方法
然后奇怪的事就发生,当一个url第一次被拦截的时候,没有问题,然后这个url执行之后,发送了web.xml错误配置的错误时候,会再次进入这个拦截器。
这时进入拦截器的时候,这个request里是没有session的信息,那么这个时候,肯定会判断无登陆,就会被重定向到登陆页面,但实际上我们并不想到登陆页面,
而是希望进入错误页面。
当然解决办法也很简单,
<mvc:interceptor>
<mvc:mapping path="/**" />
<!-- 排除欢迎页面文件 -->
<mvc:exclude-mapping path="/"/>
<mvc:exclude-mapping path="/index.html"/>
<!-- 排除404,500页面 -->
<mvc:exclude-mapping path="/404.html"/>
<mvc:exclude-mapping path="/500.html"/>
<!-- 排除静态文件 -->
<mvc:exclude-mapping path="/resources/**" />
<!-- 排除频繁访问验证页面 -->
<mvc:exclude-mapping path="/validation" />
<!-- 排除网站头像 -->
<mvc:exclude-mapping path="/favicon.ico" />
<!-- 排除登录页面 -->
<mvc:exclude-mapping path="/login"/>
<bean class="com.interceptor.AdminLogInInterceptor" />
</mvc:interceptor>
为这个拦截器添加排除的url就可以了,其实本来也应该排除这两个url,只是通过这个过程,说明一下这个点
当web.xml内部机制跳转指定错误页面的时候,request会丢失session信息。