Java Struts2 ActionContext setName方法不工作
我正试图在Struts2中编写一个拦截器,根据某些条件将请求重定向到不同的操作。我的拦截器工作正常,如下所示Java Struts2 ActionContext setName方法不工作,java,struts2,servlet-filters,interceptor,Java,Struts2,Servlet Filters,Interceptor,我正试图在Struts2中编写一个拦截器,根据某些条件将请求重定向到不同的操作。我的拦截器工作正常,如下所示 public String intercept(ActionInvocation actionInvocation) throws Exception { ActionContext actionContext=actionInvocation.getInvocationContext(); String actionName=actionContext.
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;
}
Struts配置
<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>
但仍然不是调用actionErrorPageForLinkAccess
而是调用admin
操作。
为什么我会遇到此问题?您会遇到此问题,因为该操作已被dispatcher解析并调用,因此在操作上下文中更改操作名称是无用的。Struts已经在filter中完成了这项工作
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);
}
设置名称不会更改执行的操作。相反,从拦截器返回一些不同的结果,并在struts.xml中配置此结果。在不重写默认行为之前,您试图设置什么无效struts将为您设置操作名称。@Aleksandr您的建议很好,但我的要求不同。我想调用不同的操作,而不仅仅是返回结果。因为我需要为每个操作配置结果名称。@Roman您可能是对的,我需要找到一些解决方案您可以为此配置全局结果。那么为什么他们提供setName()@mahesh将
名称设置为bean是一个setter。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);
}