Victor
Victor

Reputation: 17107

invoke Struts 2 action method with different arguments

In my struts 2 actionclass, I have a method:

public String doXXX(String param){

//do stuff
return SUCCESS;
}

In my config xml, how can I call this method with different values for 'param' based on the action. Something like:

<action name="action1" class="struts2Class" method="doXXX" param = "foo" />
<action name="action2" class="struts2Class" method="doXXX" param = "bar" />

Upvotes: 1

Views: 5519

Answers (1)

Alex Barnes
Alex Barnes

Reputation: 7218

You can't pass arguments in the method which you intend to use as the action method. You can specify a param element in the struts.xml for your action as follows:

<action name="action1" class="struts2Class" method="doXXX">
  <param name="foo">bar</param>
</action>

You then need to declare a private variable called foo on your struts2Class with getters and setters. This property will be set when action1 is invoked.

The property foo will be set by the Param Interceptor as part of the action execution.

Upvotes: 4

Related Questions