1

I have a requirement that I need to get a request parameter from actionB on execution of actionA. You can see below that there is complex logic behind working out strB in actionB. I want to get the value of strB in actionB without having to repeat the complex logic. What's the best way to do that?

<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;
    }   
}
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Richie
  • 4,989
  • 24
  • 90
  • 177
  • Would execution of Action A be triggered by Action B? Or do you have in mind to store some state across actions? – Tim Biegeleisen Jul 11 '15 at 10:40
  • Na execution of action a is independent of action b. No state across action required. I just don't want to repeat the logic. – Richie Jul 11 '15 at 10:49
  • So you want to just reuse the _code_ without having to repeat the logic? – Tim Biegeleisen Jul 11 '15 at 10:52
  • You can use the same class to map the actions to different method. – Roman C Jul 11 '15 at 10:54
  • Na I don't think that's an option for me. ActionA and ActionB are already established action classes and used for different purposes. – Richie Jul 11 '15 at 11:12
  • There's a lot of ways passing parameters to the action which one is the best is primarily opinion based. – Roman C Jul 11 '15 at 12:42
  • I'm not sure what you're asking. If you want to store the results of a calculation you can use the session (being wary of session size). If the "complex logic" is simply duplicated code it doesn't belong in the action anyway. – Dave Newton Jul 11 '15 at 16:07
  • You are right that the complex code does not belong in the action. And I wod like to lambast the person who implemented it. Nether the less it is where it is now. Sound like the best thing to do is to bite the bullet and rework – Richie Jul 11 '15 at 21:13

1 Answers1

0

The best way is opinion based. Avoid asking questions this way;

One way to solve it, if you have two actions with a common logic and you want to achieve DRY, just create a parent Action, and make ActionA and ActionB extend ParentAction (that itself extends ActionSupport) instead of ActionSupport directly.

Put in the parent action all the common logic. (the common logic that does not belong to the business side... they should not stay in any action)

Community
  • 1
  • 1
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243