Question

I am trying to write a interceptor in Struts2 which redirects request to different action based on some condition. My interceptor works fine which is as given below.

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 configuration

<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>

When ever i call admin action, console output

admin
ErrorPageForLinkAccess

But still its not calling the action ErrorPageForLinkAccess rather calling admin action. Why i am facing this problem?

Was it helpful?

Solution

You are facing this problem because the action is already resolved and invoked by the dispatcher, so changing the action name in the action context is useless. Struts has done this in 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);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top