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.

Posted

in

by

Tags:


Related Posts

Comments

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

  1. Atulkumarsharma1986 Avatar
    Atulkumarsharma1986

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

  2. amey630 Avatar
    amey630

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

    1. ulluDada Avatar
      ulluDada

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

  3. jkjjljjlj Avatar
    jkjjljjlj

    it is not working

    1. ulluDada Avatar
      ulluDada

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

    2. subodh Avatar
      subodh

      use constants in strus.xml file

Leave a Reply to jkjjljjljCancel reply

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

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading