gpt4 book ai didi

java - Struts2 ActionContext setName 方法不起作用

转载 作者:太空宇宙 更新时间:2023-11-04 15:04:47 25 4
gpt4 key购买 nike

我正在尝试在 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;
}

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>

每当我调用管理操作时,控制台输出

admin
ErrorPageForLinkAccess

但它仍然没有调用 ErrorPageForLinkAccess 操作,而是调用 admin 操作。为什么我会遇到这个问题?

最佳答案

您遇到此问题是因为该操作已由调度程序解析并调用,因此在操作上下文中更改操作名称是没有用的。 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);
}

关于java - Struts2 ActionContext setName 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22148006/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com