Struts2 でアプリを作っていて、Interceptor を使おうとしたら、めちゃはまった・・・
public class AuthenticationInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Map<String, Object> session = ActionContext.getContext().getSession();
Boolean auth = (Boolean)session.get(AuthenticationAction.AUTH_KEY);
if (null == auth || !auth.booleanValue()) {
return "auth-error";
} else {
// 次のインターセプター処理
return invocation.invoke();
}
}
}
というのが正解のソースコード。
じゃあ、何がダメなやつはどこがおかしかったのかと言うと
この Interceptor 独自の戻り値を struts.xml で定義した、global-results の値にマッチングさせたい時
次のインターセプター処理を呼び出してはいけない!!
って事ですな・・・
インターセプターが戻り値を持っている時点でもっと早く気がつけよ。って気がしないでもないです。はい。
ちなみに、 struts.xml はこんな感じ。
auth/login /WEB-INF/pages/admin/index.jsp auth/login admin/index /WEB-INF/pages/auth/login.jsp
0 Comments.