为了理解 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>
我希望能够从操作中记录“/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;
}
}
有三个打印语句在我的控制台上产生以下输出:
INFO: ActionContext.getContext().getActionInvocation().getResultCode(): null
INFO: PreResultListener resultCode: success
INFO: PreResultListener result: null
通过测试结果“invocation.getResult()”和 resultcode 为 null before PreResultListener 被调用但是在 PreResultListener 中设置了 resultcode,但是结果仍然返回 null!
来自 getResult() 方法的 JavaDoc:
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.
很明显结果实例没有被创建。
那么如何在此操作中显示“/WEB-INF/content/test.jsp”?这不是典型的 struts2 使用,我想测试一个配置提供程序,它的操作结果的构造有问题,希望理解为什么这不起作用也会让我修复它。
最佳答案
问题是你想从 Action 调用中得到结果,你不应该这样做。操作调用结果供内部使用,应该受到保护。
要获得结果,您应该查阅 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);
关于java - 了解 Struts2 内部结构 : Result Configuration,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18650377/