问题
I want to redirect to home page if session get invalid. My spring-servlet.xml is
<mvc:interceptors>
<bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor" />
<mvc:interceptor>
<mvc:mapping path="/**" />
<bean class="com.xxx.MyInterceptor" />
</mvc:interceptor>
</mvc:interceptors>
Interceptor :
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if ((null == request.getSession(false))
|| (null == request.getSession(false).getAttribute(
"user"))) {
System.out.println("user logged out...");
RequestDispatcher rd = request.getRequestDispatcher("loginForm.htm");
rd.forward(request, response);
return false;
}
return super.preHandle(request, response, handler);
}
But its not working... Whenever application get started, the message get printed multiple times and at the end it gives stack overflow..
Thank You.
回答1:
It seems like the problem is in your mapping path. Since its mapped with /**
your loginForm.htm is also getting intercepted. You have two solutions available to resolve this problem.
Either define <mvc:resources location="/resources/" mapping="/resources/**" />
so that the *.htm requests will not be intercepted. Replace the location and mapping values as per your path where the *.htm files are.
And another option is to change your mapping in intercepter with something like /*.do
or something else.
Hope this helps you. Cheers.
来源:https://stackoverflow.com/questions/11132653/spring-3-0-unable-to-forward-request-from-handlerinterceptoradapter