java - 了解 Struts2 内部结构 : Result Configuration

标签 java struts2

为了理解 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/

相关文章:

java - Java 中直接从 URL 读取

java - 如何使用 Java Tree Compiler API 查找标识符的类型声明?

用于 360 度视频的 ExifTools 进程中的 Java Process Builder 空白

java - 表单未提交 <s :select> 的选定值

java - 使用Struts2访问jsp中action类的方法中本地定义的变量的值

java - Jqgrid列更新后扩展

xml - 我可以将 struts.xml 文件更改为其他任何文件吗?

java - Struts2 INPUT 结果 : how does it work? 如何处理转换/验证错误?

java - 从包中读取 CSV 文件

java - 如何在相机 2 api 片段中捕获和预览显示到 ImageView ?