Dynamic Method Selection in Struts2

Struts 2 framework provides feature to execute method from action dynamically. Suppose you have five methods (execute, add, delete, update and query) in action class and want to configure in struts.xml file. You have to configure seperate action for seperate method. But you can configure all four methods with one action through Dynamic Method Selection feature of Struts 2 framework.

To implement the wildcard method selection technique Struts 2 framework provide technique to dynamically select the correct method to call at runtime. You just need to use the wildcard character * in name value and an attribute value place holder ( {1} ) for the method value.

For example:
<action name="*Action1" class="techmyguru.actions.EmpAction1" method="{1}">
<result name="success">/EmpSuccess.jsp</result>
</action>

The * is the wildcard character. Any action name values that end in "EmpAction1" will be handled by this action mapping. Whatever value is before "EmpAction1" will be used for the method attribute (the {1} place holder will be replaced with that value).

Lets demostrate how framework select method from action class dynamically .

I have created user interface with four input controls ( three textfield & a selection box ) and five buttons. Seperate action is defined with each button accept submit. When user click submit button, the execute method will be executed , click Add Person the add method will be executed, click Delete Person the delete method will be executed, click Update Person the update method will be executed,click Query Person the query method will be executed from action class.

Follow the following steps to create this project :

Step 1: Create a Dynamic Web Project in eclipse - "Struts2Dynamicmethod1".

Step 2: Edit deployment descriptor web.xml. [ Click here If you are not familiar with Struts2 application environment setup in Eclipse. ]

Step 3: Copy the required Struts2 jar files in WEB-INF\lib folder in your project:
commons-collections-3.1.jar
commons-digester-2.0.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-logging-api-1.1.jar
freemarker-2.3.19.jar
javassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.8.jar
xwork-core-2.3.8.jar

Step 4: Create user interface index.jsp :
<%taglib uri="/struts-tags" prefix="s"%>
<html>
<body>
<h2>Wildcard method selection</h2>
  <s:form action="Action1" method="post">
  <s:textfield name="eid" label="Enter id"/>
  <s:textfield name="ename" label="Enter name"/>
  <s:textfield name="ecity" label="Enter city"/>
  <s:select multiple="true" name="skills" label="Skills"  size="4" 
  list="#{'Playing':'Playing','Teaching':'Teaching','Dancing':'Dancing',
  'Leadership':'Leadership','Speaking':'Speaking','Cooking':'Cooking'}" />
  <s:submit/>
  <s:submit action="addAction1" value="Add Person"/>
  <s:submit action="deleteAction1" value="Delete Person"/>
  <s:submit action="updateAction1" value="Update Person"/>
  <s:submit action="queryAction1" value="Query Person"/>
  </s:form>
</body>
</html>

Step 5: Create action class EmpAction1.java :
package techmyguru.actions;
import com.opensymphony.xwork2.ActionSupport;
public class EmpAction1 extends ActionSupport {
  String eid, ename, ecity, skills[],message;
  // default constructor
  public EmpAction1() {
    eid = "";
    ename = "";
    ecity = "";
    message="";
  }
  // getter and setter methods
  // execute method 
  @Override
  public String execute() throws Exception {
  setMessage("execute method executed");
    return SUCCESS;
  }
  // add method
  public String add() {
    setMessage("Add method executed");
    return SUCCESS;
  }
  // delete method
  public String delete() {
    setMessage("delete method executed");
    return SUCCESS;
  }
  // update method
  public String update() {
    setMessage("update method executed");
    return SUCCESS;
  }
  // query method
  public String query() {
    setMessage("query method executed");
    return SUCCESS;
  }
}

Step 6: Configure Action in struts.xml file :
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
    "http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
  <constant name="devMode" value="true" />
  <package name="default" extends="struts-default">
    <action name="*Action1" class="techmyguru.actions.EmpAction1" method="{1}">
      <result name="success">/EmpSuccess.jsp</result>
    </action>
  </package>
</struts>


Step 7: Create target View page EmpSuccess.jsp :
<%@taglib uri="/struts-tags" prefix="s"%>
<html>
<body>
<h2>Employee details</h2>
<hr>
<b>Employee id :</b> <s:property value="eid"/><br>
<b>Employee Name :</b> <s:property value="ename"/><br>
<b>Employee City :</b> <s:property value="ecity"/><br>
<b>Employee Skills :</b> <s:property value="skills"/><br>
<font size="+1" color="blue">Method execution message :<s:property value="message"/></font><br>
<hr>
<a href="index.jsp">Home</a>
</body>
</html>


Step 8: Verify the project directory structure :
Dynamic method selection

Step 9: Run the project and get output :
Dynamic method selection

Fill the form and click submit button , you will be get the below screen :
Dynamic method selection

Fill form and click Add Person button , you will the below screen :
Dynamic method selection

Fill form and click Delete Person button , you will the below screen :
Dynamic method selection

Fill form and click Update Person button , you will the below screen :
Dynamic method selection

Fill form and click Query Person button , you will the below screen :
Dynamic method selection

Previous Download Source Code Next

Comment by : Jagannath December 10th, 2014
Comment : how can I create dynamic submit buttons same like above example from action class and call dynamic methods.


Write your Comment

Name :
Email Id : [ We will not publish your email id ]
Your Comment : [ Maximum 200 chars ]
Enter Security code