拦截器没有被调用但仍然执行?
Interceptor not being called but executes anyway?
我在不同的包中定义了两个拦截器。
一个是"affirmative"(检查session
是否包含currentId
)另一个"non-affirmative"(检查session
是否不包含currentId
).
struts.xml
:
<!-- actions available to guests -->
<package name="guest" extends="struts-default">
<interceptors>
<interceptor name="containskey" class="com.mypackage.interceptor.ContainsKeyInterceptor" />
</interceptors>
<action name="index" class="com.mypackage.action.IndexAction">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="containskey" />
<result type="redirect">/index.jsp</result>
<result name="index" type="redirect">/index.jsp</result>
</action>
<action name="login" class="com.mypackage.action.LoginAction">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="containskey" />
<result type="redirect">/index.jsp</result>
<result name="input">/login.jsp</result>
<result name="index" type="redirect">/index.jsp</result>
</action>
</package>
<!-- actions available to members -->
<package name="member" extends="struts-default">
<interceptors>
<interceptor name="notcontainskey" class="com.mypackage.interceptor.NotContainsKeyInterceptor" />
</interceptors>
<action name="changepassword" class="com.mypackage.action.ChangePasswordAction">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="notcontainskey" />
<result type="redirect">/usercp.jsp</result>
<result name="input">/usercp-change-password.jsp</result>
<result name="index" type="redirect">/index.jsp</result>
</action>
<action name="logout" class="com.mypackage.action.LogoutAction">
<result type="redirectAction">
<param name="actionName">index</param>
</result>
</action>
</package>
NotContainsKeyInterceptor
:
public class NotContainsKeyInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("NotContainsKeyInterceptor");
final ActionContext actionContext = actionInvocation.getInvocationContext();
Map<String, Object> session = actionContext.getSession();
if(!session.containsKey("currentId")) {
return "index";
}
String result = actionInvocation.invoke();
return result;
}
}
http://localhost:8080/mysite/changepassword
- 当我在没有登录的情况下调用
changepassword
操作时(即
session
不包含currentId
),println()
的
NotContainsKeyInterceptor
没有执行(这让我假设
它没有被调用),但是,它执行预期的行为
重定向到 index.jsp
.
ChangePasswordAction
class的validate()
方法也是
被调用。
为什么会这样?
(如果你需要看ChangePasswordAction
class,请告诉我,我会把它添加到post。)
实际上它没有被调用..你可以这样改变你的配置并尝试
<interceptor-ref name="notcontainskey" />
<interceptor-ref name="defaultStack" />
我的意思是先指定拦截器再指定defaultstack
我在不同的包中定义了两个拦截器。
一个是"affirmative"(检查session
是否包含currentId
)另一个"non-affirmative"(检查session
是否不包含currentId
).
struts.xml
:
<!-- actions available to guests -->
<package name="guest" extends="struts-default">
<interceptors>
<interceptor name="containskey" class="com.mypackage.interceptor.ContainsKeyInterceptor" />
</interceptors>
<action name="index" class="com.mypackage.action.IndexAction">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="containskey" />
<result type="redirect">/index.jsp</result>
<result name="index" type="redirect">/index.jsp</result>
</action>
<action name="login" class="com.mypackage.action.LoginAction">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="containskey" />
<result type="redirect">/index.jsp</result>
<result name="input">/login.jsp</result>
<result name="index" type="redirect">/index.jsp</result>
</action>
</package>
<!-- actions available to members -->
<package name="member" extends="struts-default">
<interceptors>
<interceptor name="notcontainskey" class="com.mypackage.interceptor.NotContainsKeyInterceptor" />
</interceptors>
<action name="changepassword" class="com.mypackage.action.ChangePasswordAction">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="notcontainskey" />
<result type="redirect">/usercp.jsp</result>
<result name="input">/usercp-change-password.jsp</result>
<result name="index" type="redirect">/index.jsp</result>
</action>
<action name="logout" class="com.mypackage.action.LogoutAction">
<result type="redirectAction">
<param name="actionName">index</param>
</result>
</action>
</package>
NotContainsKeyInterceptor
:
public class NotContainsKeyInterceptor extends AbstractInterceptor {
private static final long serialVersionUID = 1L;
@Override
public String intercept(ActionInvocation actionInvocation) throws Exception {
System.out.println("NotContainsKeyInterceptor");
final ActionContext actionContext = actionInvocation.getInvocationContext();
Map<String, Object> session = actionContext.getSession();
if(!session.containsKey("currentId")) {
return "index";
}
String result = actionInvocation.invoke();
return result;
}
}
http://localhost:8080/mysite/changepassword
- 当我在没有登录的情况下调用
changepassword
操作时(即session
不包含currentId
),println()
的NotContainsKeyInterceptor
没有执行(这让我假设 它没有被调用),但是,它执行预期的行为 重定向到index.jsp
. ChangePasswordAction
class的validate()
方法也是 被调用。
为什么会这样?
(如果你需要看ChangePasswordAction
class,请告诉我,我会把它添加到post。)
实际上它没有被调用..你可以这样改变你的配置并尝试
<interceptor-ref name="notcontainskey" />
<interceptor-ref name="defaultStack" />
我的意思是先指定拦截器再指定defaultstack