Struts2中如何将结果转发到另一个动作?

Art*_*nko 4 java struts2

我有这个动作:

package com.test;

import com.opensymphony.xwork2.Action;

public class TestAction implements Action{
    private String simpleParam;

    public String getSimpleParam() {
        return simpleParam;
    }

    public void setSimpleParam(String simpleParam) {
        this.simpleParam = simpleParam;
    }

    @Override
    public String execute() throws Exception {
        return SUCCESS;
    }
}
Run Code Online (Sandbox Code Playgroud)

当它执行时,我想调用struts内的另一个动作(例如不是重定向)并传递给它simpleParam。第二个动作是:

package com.test;

import com.opensymphony.xwork2.Action;

public class HelloAction implements Action {
    private String id;
    private String result;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getResult() {
        return result;
    }

    @Override
    public String execute() throws Exception {
        result = "result" + getId();
        return SUCCESS;
    }
}
Run Code Online (Sandbox Code Playgroud)

我在 struts.xml 中看到了工作示例,结果只需输入另一个操作名称和参数即可工作。所以我正在尝试这样做:

<struts>
    <package name="main" extends="struts-default">
        <action name="test" class="com.test.TestAction">
            <result name="success">hello.action?id=${simpleParam}</result>
        </action>
        <action name="hello" class="com.test.HelloAction">
            <result>/hello.jsp</result>
        </action>
    </package>
</struts>
Run Code Online (Sandbox Code Playgroud)

Idea 完全看到了这个操作,但在浏览器中我得到 404 状态。当我简单地从浏览器调用 hello.action 时,它就可以工作。重定向也有效。我也尝试过链式,但我的参数没有通过,而且不太方便。我做对了吗?如果是的话,404 状态的原因可能是什么?

Ste*_*erg 5

这可能会有所帮助:http ://struts.apache.org/docs/action-chaining.html

<struts>
    <package name="main" extends="struts-default">
        <action name="test" class="com.test.TestAction">
            <result name="success" type="chain">hello</result>
        </action>
        <action name="hello" class="com.test.HelloAction">
            <result>/hello.jsp</result>
        </action>
    </package>
</struts>
Run Code Online (Sandbox Code Playgroud)

要将参数从一个操作传递到另一个操作,您可以使用 HttpServletRequest ( http://www.mkyong.com/struts2/how-to-get-the-httpservletrequest-in-struts-2 ):

测试操作.java:

package com.test;

import com.opensymphony.xwork2.Action;

public class TestAction implements Action{
    private String simpleParam;

    public String getSimpleParam() {
        return simpleParam;
    }

    public void setSimpleParam(String simpleParam) {
        this.simpleParam = simpleParam;
    }

    @Override
    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        request.setAttribute("id", simpleParam);
        return SUCCESS;
    }
}
Run Code Online (Sandbox Code Playgroud)

HelloAction.java:

package com.test;

import com.opensymphony.xwork2.Action;

public class HelloAction implements Action {
    private String id;
    private String result;

    public String getId() {
        return id;
    }

    public String getResult() {
        return result;
    }

    @Override
    public String execute() throws Exception {
        HttpServletRequest request = ServletActionContext.getRequest();
        id = (String)request.getAttribute("id");

        result = "result" + getId();
        return SUCCESS;
    }
}
Run Code Online (Sandbox Code Playgroud)