Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Understanding Struts2 Internals : Result Configuration

Tags:

java

struts2

In an effort to understand how struts2 loads its configuration I wanted to display the path to the JSP which would be rendered. Given the following very minimal struts.xml:

<struts>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />

    <package name="base" namespace="/">
        <result-types>
            <result-type name="dispatcher" class="org.apache.struts2.dispatcher.ServletDispatcherResult" default="true"/>
        </result-types>
        <action name="test" class="com.kenmcwilliams.badwebapp.action.Test">
            <result>/WEB-INF/content/test.jsp</result>
        </action>
    </package>
</struts>

I want to be able to log "/WEB-INF/content/test.jsp" from within the action. Given the following action:

package com.quaternion.badwebapp.action;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.PreResultListener;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Test extends ActionSupport {
    //used for a sanity test on JSP
    public String getMessage() {
        return "From test";
    }

    @Override
    public String execute() throws Exception {
        System.out.println("ActionContext.getContext().getActionInvocation().getResultCode(): " + ActionContext.getContext().getActionInvocation().getResultCode());
        ActionInvocation ai = ActionContext.getContext().getActionInvocation();
        ai.addPreResultListener(new PreResultListener() {
            @Override
            public void beforeResult(ActionInvocation invocation, String resultCode) {
                try {
                    System.out.println("PreResultListener resultCode: " + resultCode);
                    System.out.println("PreResultListener result: " + invocation.getResult());
                } catch (Exception ex) {
                    Logger.getLogger(Test.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
        return SUCCESS;
    }
}

There are three print statements which produce the following output on my console:

INFO:   ActionContext.getContext().getActionInvocation().getResultCode(): null
INFO:   PreResultListener resultCode: success
INFO:   PreResultListener result: null

From testing both the result "invocation.getResult()" and the resultcode is null before the PreResultListener is called but within the PreResultListener the resultcode is set, yet the result still returns null!

From the JavaDoc of the getResult() method:

If the ActionInvocation has been executed before and the Result is an instance of {@link ActionChainResult}, this method will walk down the chain of ActionChainResult's until it finds a non-chain result, which will be returned. If the ActionInvocation's result has not been executed before, the Result instance will be created and populated with the result params.

Seems pretty clear that a result instance is not being created.

So how do I display "/WEB-INF/content/test.jsp" within this action? This is not for typical struts2 use, I'm want to test a configuration provider for which there is something wrong with the construction of the result for the action, hopefully understanding why this isn't working will let me fix that too.

like image 341
Quaternion Avatar asked Sep 06 '13 05:09

Quaternion


2 Answers

The problem is that you want to get result from the action invocation, you shouldn't. The action invocation result is for internal use, and should probably be protected.

To get the result you should consult the ActionConfig and get result from there.

ActionInvocation invocation = ActionContext.getContext().getActionInvocation();
ActionProxy proxy = invocation.getProxy();
ActionConfig config = proxy.getConfig();
Map<String, ResultConfig> results = config.getResults();
ResultConfig resultConfig = results.get(Action.SUCCESS);
String lastFinalLocation = null;
Map<String, String> params = resultConfig.getParams();
if (resultConfig.getClassName().equals("org.apache.struts2.dispatcher.ServletDispatcherResult")) {
  lastFinalLocation = params.get("location");
}
System.out.println("location: " + lastFinalLocation);
like image 153
Roman C Avatar answered Oct 13 '22 00:10

Roman C


A couple of things:

  1. At the moment in time where you are trying to print the + getResultCode() no such code exists yet - remember it's the action which will determine the result by returning the result string. So you'll want to print that in any of the interceptors you have in that action, after the invocation.invoke() part.

  2. getResultCode() will return the result string (success, error) and not the corresponding path.

like image 28
mmalmeida Avatar answered Oct 12 '22 23:10

mmalmeida