有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在执行actionA时从actionB获取请求参数

我需要在执行actionA时从actionB获取请求参数。您可以在下面看到,在actionB中计算strB的背后有复杂的逻辑。我想在actionB中获得strB的值,而不必重复复杂的逻辑。最好的方法是什么

<action name="actionA"
    class="com.mycompany.action.ActionA"
    method="input">         
    <result name="input" type="tiles">page.actionA</result>
</action>

<action name="actionB"
    class="com.mycompany.action.ActionB"
    method="readFromCache">         
    <result name="input" type="tiles">page.actionB</result>
</action>
public class ActionA extends ActionSupport
    private String strA = new String();
    private String strB = new String();
    public String input() throws Exception {
        strA = "Hello";
        // do something here to get strB from ActionB
        strB = ...need help here...
        return INPUT;
    }   
    public String setStrA(String strA) throws Exception {
        strA = strA;
    }
    public String getStrA() throws Exception {
        return strA;
    }   
}
public class ActionB extends ActionSupport
    private String strB = new String();
    public String readFromCache() throws Exception {
        strB = ...complex logic here...;
        return INPUT;
    }   
    public String setStrB(String strB) throws Exception {
        strB = strB;
    }
    public String getStrB() throws Exception {
        return strB;
    }   
}

共 (1) 个答案

  1. # 1 楼答案

    最好的方式是基于意见的。避免以这种方式提问

    一种解决方法,如果您有两个具有公共逻辑的操作,并且希望实现干式,只需create a parent Action,并使ActionA和ActionB扩展ParentAction(其本身扩展了ActionSupport),而不是直接扩展ActionSupport

    在父操作中放入所有公共逻辑。(常见的逻辑是does not belong to the business side…他们不应该停留在任何动作中)