Struts 2ログインブロック
ブロッキングに使用してセッションにユーザー情報がすでに存在するかどうかを検出します.
LoginIntercept.java
LoginIntercept.java
public class LoginIntercept extends MethodFilterInterceptor{
@Override
protected String doIntercept(ActionInvocation arg0) throws Exception {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
String name = (String)session.getAttribute("name");// session , session
if(name == null){
return "login";
}else{
return arg0.invoke();
}
}
}
struts.xml構成:<package name="test" namespace="/" extends="struts-default">
<interceptors>
<!-- -->
<interceptor name="logininterceptor" class="intercept.LoginIntercept" />
<interceptor-stack name="logininterceptorStack" >
<interceptor-ref name="defaultStack" />
<interceptor-ref name="logininterceptor" />
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="defaultStack" />
<global-results>
<!-- Action -->
<result name="login">/index.jsp</result>
</global-results>
<action name="show" class="action.ShowAction">
<result name="success">/success.jsp</result>
<!-- action -->
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="logininterceptor"></interceptor-ref>
</action>
</package>