Explain dynamic method invocation in Struts 2

Dynamic Method invocation is the concept in the Struts action mapping file, where instead of writing <action> for every method, one can map the wild character with the method name having same prefix or suffix.

Example : Instead of writing addOperation, subOperation and divOperation, programmer can write *Operation.

Consider below jsp code:

<s:form action="calculator_add" >
<s:textfield name="num1" label="Number 1"></s:textfield>
<s:textfield name="num2" label="Number 2"></s:textfield>
<s:submit action="calculator_add" value="Add" />
<s:submit action="calculator_sub" value="Substract" />

Struts configuration:

<package name="calc" extends="struts-default">
		<action name="calculatorInput">

		<action name="calculatorResult">

		<action name="calculator_*" method="{1}"
			<result name="success" type="chain">calculatorResult</result>


As you can see, instead of writing different action, action name is provided like “calculator_*”, and method=”{1}”, means if the input is “calculator_add” then the add() method will be executed in Action class. That means we can invoke any methods dynamically other than execute() in action class.





Related Posts


6 responses to “Explain dynamic method invocation in Struts 2”

  1. Atulkumarsharma1986 Avatar

    hi i want an custom tag that shows applet whenever request comes on it

  2. amey630 Avatar

    it’s a wildcard method selection technique, Dynamic Method Invocation technique uses bang(!) operator

    1. ulluDada Avatar

      Dynamic Method Invocation technique uses dabang(!) operator not bang(!) . please take care of it.

  3. jkjjljjlj Avatar

    it is not working

    1. ulluDada Avatar

      It won’t, instead of try to copy somebody’s code try to get the idea and apply this.

    2. subodh Avatar

      use constants in strus.xml file

Leave a Reply to Atulkumarsharma1986 Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: