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

<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;
    }
}

最佳答案

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

解决问题的一种方法是,如果您有两个具有共同逻辑的动作,并且想实现DRY,只需create a parent Action,然后让ActionA和ActionB扩展ParentAction(本身扩展ActionSupport)而不是直接使用ActionSupport。

将父操作放入所有通用逻辑。 (does not belong to the business side ...不应采取任何行动的常见逻辑)

10-07 17:42