我试图在Struts2中编写一个拦截器,该拦截器根据某些条件将请求重定向到不同的操作。我的拦截器工作正常,如下所示。
public String intercept(ActionInvocation actionInvocation) throws Exception {
ActionContext actionContext=actionInvocation.getInvocationContext();
String actionName=actionContext.getName();
String actionResult=null;
if(actionName.equals("admin"))
{
System.out.println("admin");
//if(based on some condition)
actionContext.setName("ErrorPageForLinkAccess");
System.out.println(actionContext.getName());
}
actionResult = actionInvocation.invoke();
return actionResult;
}支柱配置
<action name="other">
<result>Jsp/other.jsp</result>
</action>
<action name="admin" class="com.example.Admin" method="adminDemo">
<result name="success">Jsp/admin.jsp</result>
</action>
<action name="ErrorPageForLinkAccess">
<result name="success">Jsp/ErrorPageForLinkAccess.jsp</result>
</action>当我调用admin操作时,控制台输出
admin
ErrorPageForLinkAccess但它仍然不是调用动作ErrorPageForLinkAccess,而是调用admin操作。我为何要面对这个问题呢?
发布于 2014-03-03 14:10:27
您正面临此问题,因为该操作已经由dispatcher解决和调用,因此在操作上下文中更改操作名称是无用的。Struts已经在过滤器中实现了这一点。
ActionMapping mapping = prepare.findActionMapping(request, response, true);
if (mapping == null) {
boolean handled = execute.executeStaticResourceRequest(request, response);
if (!handled) {
chain.doFilter(request, response);
}
} else {
execute.executeAction(request, response, mapping);
}https://stackoverflow.com/questions/22148006
复制相似问题