我正在使用的Struts2 + Spring + Hibernate的Web應用程序登錄模塊,並且我要強制用戶登錄,如果他們想瀏覽低谷的網站。Struts2的攔截器溢出異常
我有一個LoginInterceptor
public class LoginInterceptor implements Interceptor {
public LoginInterceptor() {
}
public void destroy() {
System.out.println("LoginInterceptor destroy() is called...");
}
public void init() {
System.out.println("LoginInterceptor init() is called...");
}
public String intercept(ActionInvocation actionInvocation) throws Exception {
final ActionContext context = actionInvocation.getInvocationContext();
HttpServletRequest request = (HttpServletRequest) context.get(ServletActionContext.HTTP_REQUEST);
HttpSession session = request.getSession(true);
//Code
try {
Users userObj = (Users) session.getAttribute("userObj");
if (userObj == null) {
System.out.println("if from LoginInterceptor");
return "noLogin";
}
} catch (Exception e) {
Logger.getLogger(LoginInterceptor.class.getName()).log(Level.INFO, "message", e);
}
//Invoke action
String result = actionInvocation.invoke();
return result;
}
}
我在struts.xml
<interceptors>
<interceptor name="myLocale" class="com.deveto.struts.interceptors.LocaleInterceptor"/>
<interceptor name="login" class="com.deveto.struts.interceptors.LoginInterceptor"/>
<interceptor name="access" class="com.deveto.struts.interceptors.AccessInterceptor"/>
<interceptor-stack name="defaultStack">
<interceptor-ref name="myLocale"/>
<interceptor-ref name="login"/>
<interceptor-ref name="exception"/>
<interceptor-ref name="alias"/>
<interceptor-ref name="prepare"/>
<interceptor-ref name="i18n"/>
<interceptor-ref name="chain"/>
<interceptor-ref name="debugging"/>
<interceptor-ref name="profiling"/>
<interceptor-ref name="fileUpload"/>
<interceptor-ref name="checkbox"/>
<interceptor-ref name="params">
<param name="excludeParams">dojo\..*</param>
</interceptor-ref>
<interceptor-ref name="conversionError"/>
<interceptor-ref name="validation">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
<interceptor-ref name="workflow">
<param name="excludeMethods">input,back,cancel,browse</param>
</interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack"/>
<global-results>
<result name="noLogin" type="redirectAction">show-login</result>
<result name="noAccess" type="redirectAction">access-required</result>
</global-results>
<action
name="show-login"
class="com.deveto.struts.actions.UsersAction" >
<interceptor-ref name="defaultStack"/>
<result name="success" type="tiles">tiles.login</result>
</action>
但是當我運行該項目,我有一個溢出異常,在堆棧跟蹤我什麼都沒有,但Mozilla的告訴我即:
The page isn't redirecting properly
Firefox has detected that the server is redirecting the request for this address in a way that will never complete.
和
System.out.println("if from LoginInterceptor");
重複幾次。我不明白爲什麼,但我有更多的攔截器,以及除此之外的其他作品。
如何你'noLogin'結果定義? 我懷疑它重定向到再次得到由你'LoginInterceptor'然後重定向...你的想法截獲一些登錄頁面。 –
我在第二個框中提供了代碼,請向下滾動第二個框,然後您會看到它。 – Denees
對不起,我沒有看到滾動條:/請參閱下面的答案。 –