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.