我有一个 LoginInterceptor 在大多数操作之前运行并检查成员是否登录。如果是,则显示该页面,否则将其重定向到登录页面。
但是我刚刚注意到拦截器“阻止”了所有 URL 参数。基本上,如果在一个动作之前有一个拦截器,这个动作的 URL 参数将不会传递给 setter。
这是我的拦截器:
public class LoginInterceptor extends AbstractInterceptor {
public String intercept(final ActionInvocation invocation) throws Exception {
final String REDIR = "loginRedirect";
AuthenticationService auth = new AuthenticationService();
if (auth.isMemberLoggedIn()) {
return invocation.invoke();
} else {
return REDIR;
}
}
}
我怀疑invocation.invoke()
调用动作,但没有参数。
我能做些什么呢?
更新:
AuthenticationService.isMemberLoggedIn()
public boolean isMemberLoggedIn() {
Map<String, Object> session = ActionContext.getContext().getSession();
String username = (String) session.get("username");
if (username != null) {
return true;
} else {
return false;
}
}
struts.xml
<package name="global" extends="struts-default">
<interceptors>
<interceptor name="loginInterceptor" class="community.interceptor.LoginInterceptor" />
</interceptors>
<global-results>
<result name="loginRedirect" type="redirect">/members/login</result>
</global-results>
</package>
然后每个包扩展global
,我在每个动作中调用它们:
<interceptor-ref name="loginInterceptor" />