java - 如何在执行操作时从操作中获取请求参数

标签 java inheritance parameters struts2 request

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

<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 ...他们不应该停留在任何行动中)

关于java - 如何在执行操作时从操作中获取请求参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31356138/

相关文章:

Java 列和行

java - 通过java将数据追加到xlsx文件中

ruby-on-rails - rails模型的条件类继承

c++ - 在复制赋值运算符中分配基类成员

url - 处理请求参数后 GWT 更改 URL

java - 为什么我的 Java 桌面应用程序无法运行?

java - 这些页面包括带参数的编写函数

c# - unity3d 中用于继承类的 CustomPropertyDrawer

c# - 如何将鼠标单击方法作为参数传递?

java - 构造函数中有多个参数的异常