Struts 2 中的拦截器未正确重定向页面
The page is not redirecting properly by interceptor in Struts 2
就这样,我写了一个拦截登录,结果是
<result name="login" type="redirectAction">access</result>
一切正常,但我认为这是回忆它,结果去访问并再次执行拦截器并去访问等。我相信因为我的浏览器显示消息:
the page is not redirecting properly.
我正在使用 Struts 2 和 Rest 插件。
这是我的拦截器:
@Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession(false);
Object loginObject = session.getAttribute("login");
boolean login = false;
if (loginObject != null) {
login = (Boolean) loginObject;
if (!login) {
return Action.LOGIN;
} else {
return invocation.invoke();
}
} else {
return Action.LOGIN;
}
当您从拦截器中 return Action.LOGIN 时,将执行结果,并且由于结果的类型为 redirectAction
,将创建另一个请求并通过拦截器堆栈进行过滤.然后执行将再次触发您的拦截器,这是不需要的结果。
您需要为 access
操作排除登录拦截器的执行,例如使用默认堆栈。
<default-interceptor-ref name="myCustomLoginStack"/>
<action name="access" class="foo.bar.actions.Access">
<result>access.jsp</result>
<interceptor-ref name="defaultStack" />
</action>
<action name="foobar" class="foo.bar.actions.Foobar">
<result>foo.jsp</result>
<result name="login" type="redirectAction">access</result>
</action>
login
或 access
都没有将 loginObject
设置为会话,也没有将其设置为 true
,这是多余的。因此,如果您将此拦截器配置到操作,它将阻止操作调用和执行。
扩展 Roman 的评论:将会话值设置为 true
以及为什么它是多余的:
与其检查会话值的值,不如检查它是否存在。注销时删除会话对象,明确地(如果您需要保留其他会话数据)或使会话无效。
这样做的额外好处是可以将您的代码减少到大约这样:
public String intercept(ActionInvocation invocation) throws Exception {
Map session = invocation.getInvocationContext().getSession()
Object loginObject = session.getAttribute("login");
if (loginObject == null) {
return LOGIN;
}
return invocation.invoke();
}
关于 getSession(false)
,通常不需要包含布尔参数,因为 JSP 页面将自动创建会话,除非明确声明不这样做。
就这样,我写了一个拦截登录,结果是
<result name="login" type="redirectAction">access</result>
一切正常,但我认为这是回忆它,结果去访问并再次执行拦截器并去访问等。我相信因为我的浏览器显示消息:
the page is not redirecting properly.
我正在使用 Struts 2 和 Rest 插件。
这是我的拦截器:
@Override
public String intercept(ActionInvocation invocation) throws Exception {
HttpSession session = ServletActionContext.getRequest().getSession(false);
Object loginObject = session.getAttribute("login");
boolean login = false;
if (loginObject != null) {
login = (Boolean) loginObject;
if (!login) {
return Action.LOGIN;
} else {
return invocation.invoke();
}
} else {
return Action.LOGIN;
}
当您从拦截器中 return Action.LOGIN 时,将执行结果,并且由于结果的类型为 redirectAction
,将创建另一个请求并通过拦截器堆栈进行过滤.然后执行将再次触发您的拦截器,这是不需要的结果。
您需要为 access
操作排除登录拦截器的执行,例如使用默认堆栈。
<default-interceptor-ref name="myCustomLoginStack"/>
<action name="access" class="foo.bar.actions.Access">
<result>access.jsp</result>
<interceptor-ref name="defaultStack" />
</action>
<action name="foobar" class="foo.bar.actions.Foobar">
<result>foo.jsp</result>
<result name="login" type="redirectAction">access</result>
</action>
login
或 access
都没有将 loginObject
设置为会话,也没有将其设置为 true
,这是多余的。因此,如果您将此拦截器配置到操作,它将阻止操作调用和执行。
扩展 Roman 的评论:将会话值设置为 true
以及为什么它是多余的:
与其检查会话值的值,不如检查它是否存在。注销时删除会话对象,明确地(如果您需要保留其他会话数据)或使会话无效。
这样做的额外好处是可以将您的代码减少到大约这样:
public String intercept(ActionInvocation invocation) throws Exception {
Map session = invocation.getInvocationContext().getSession()
Object loginObject = session.getAttribute("login");
if (loginObject == null) {
return LOGIN;
}
return invocation.invoke();
}
关于 getSession(false)
,通常不需要包含布尔参数,因为 JSP 页面将自动创建会话,除非明确声明不这样做。