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

时间:2015-07-11 10:22:20

标签: java inheritance parameters struts2 request

我要求在执行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;
    }   
}
a.bc

1 个答案:

答案 0 :(得分:0)

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

解决它的一种方法,如果你有两个具有共同逻辑的动作并且想要实现DRY,只需要create a parent Action,并使ActionA和ActionB扩展ParentAction(它本身就会扩展) ActionSupport)直接代替ActionSupport。

将所有常用逻辑放入父操作中。 (does not belong to the business side ......他们不应该留在任何行动中的共同逻辑