簡體   English   中英

Struts2 ActionContext setName方法不起作用

[英]Struts2 ActionContext setName method not working

我正在嘗試在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
ErrorPageForLinkAccess

但是它仍然沒有調用動作ErrorPageForLinkAccess而是調用admin動作。 為什么我要面對這個問題?

您正面臨此問題,因為調度程序已經解決並調用了該操作,因此在操作上下文中更改操作名稱是無用的。 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);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM