我有一个要求,即在执行 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;
}
}
最佳答案
最好的方法是基于意见。避免以这种方式提问;
一种解决方法,如果你有两个具有共同逻辑的操作并且你想实现 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/