为了理解struts2如何加载其配置,我想显示将要呈现的JSP的路径.给出以下非常小的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>
Run Code Online (Sandbox Code Playgroud)
我希望能够从操作中记录"/WEB-INF/content/test.jsp".鉴于以下行动:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
有三个print语句在我的控制台上产生以下输出:
INFO: ActionContext.getContext().getActionInvocation().getResultCode(): null
INFO: PreResultListener resultCode: success
INFO: PreResultListener result: null
Run Code Online (Sandbox Code Playgroud)
在调用PreResultListener 之前测试结果"invocation.getResult()"并且结果代码为null ,但在 PreResultListener中设置了结果代码,但结果仍然返回null!
从getResult()方法的JavaDoc:
如果之前已执行ActionInvocation并且Result是{@link ActionChainResult}的实例,则此方法将沿着ActionChainResult的链向下走,直到找到将返回的非链结果.如果之前未执行ActionInvocation的结果,则将创建Result实例并使用结果参数填充.
很明显,没有创建结果实例.
那么如何在此操作中显示"/WEB-INF/content/test.jsp"?这不适用于典型的struts2使用,我想测试一个配置提供程序,对于该操作的结果构造有问题,希望理解为什么这不起作用也会让我解决这个问题.
问题是您希望从操作调用中获得结果,但您不应该这样做。操作调用结果供内部使用,并且应该受到保护。
要获得结果,您应该咨询ActionConfig并从那里获得结果。
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);
Run Code Online (Sandbox Code Playgroud)