Explain dynamic method invocation in Struts 2

Author posted by Jitendra on Posted on under category Categories Struts and tagged as Tags with 6 Comments on Explain dynamic method invocation in Struts 2

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" />
</s:form>

Struts configuration:

<package name="calc" extends="struts-default">
		<action name="calculatorInput">
			<result>/pages/calculator.jsp</result>
		</action>

		<action name="calculatorResult">
			<result>/pages/calculatorResult.jsp</result>
		</action>

		<action name="calculator_*" method="{1}"
			class="com.G2.Actions.CalculatorAction">
			<result name="success" type="chain">calculatorResult</result>
		</action>

	</package>

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 thoughts on “Explain dynamic method invocation in Struts 2”

Leave a Reply

Your email address will not be published. Required fields are marked *