Struts 2 Wildcard Method Selection


I'm working on a Java web application that uses the Struts 2 web application framework. The Struts 2 XML configuration file for this application has dozens of action mapping nodes. Groups of three or four action mapping nodes use the same ActionSupport class but just call a different method. I wanted to clean up this configuration file to have fewer action mapping nodes and also for the action URL links to be more self-explanatory.

Struts 2 provides a technique called "Wildcard Method Selection" (see references below) that can reduce the number of action mapping nodes in the Struts 2 XML configuration file. I'd not used the wildcard method selection technique previously so I had to do some research. I decided to write a short article about how to implement the wildcard method technique and provide a simple example application.

Example Application

You can download the example application, which is a zipped Eclipse Maven project. You should be able to import the application directly into Eclipse. If you don't have Eclipse, you can unzip the application and copy the files into your Java IDE.

You can run the application using Jetty since the Maven Jetty plugin is included in the Maven pom. Just open a command window and navigate to where you unzipped the example download. Make sure you're in the project's root folder. Then type mvn clean and then mvn jetty:run. Once you see [INFO] Started Jetty Server you can open your web browser and go to this URL: http://localhost:8080/wildcard/index.jsp.

The example application is just a simple one that lists some people and lets you edit them, delete them, and add a new person to the list. Everything that the application needs to do with a Person (the model class) is controlled by the Struts 2 ActionSupport class PersonAction. The PersonAction class has several different methods (e.g. create, edit, delete) that are called depending on what the user want's to do.

Wildcard Method Selection

Without using the wildcard method selection technique, I'd have to write an action mapping node in the Struts 2 configuration file for each separate action I'd want to call. For example:

<action name="createPerson" class="action.PersonAction" method="create">
<result name="input">input.jsp</result>
<result name="success">success.jsp</result>

<action name="editPerson" class="action.PersonAction" method="edit">
<result name="input">input.jsp</result>
<result name="success">success.jsp</result>


So even for this simple application, I'd have to write four separate action mapping nodes (create, edit, delete, saveOrUpdate) in the configuration file. So you can easily see that a more complex application can have dozens of action mapping nodes.

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

<action name="*Person" class="action.PersonAction" method="{1}">
<result name="input">input.jsp</result>
<result name="success">success.jsp</result>

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


will be be processed by the the above action mapping and method create of class action.PersonAction will be called. While this URL


will cause the delete method of class action.PersonAction to be called.

What happens if we have a URL with nothing in front of Person? For example:


If there is no value in front of Person, then the Struts 2 framework will call the execute method of the class action.PersonAction.

Dynamic Method Invocation

The wildcard method selection technique explained above should not be confused with the "Dynamic Method Invocation" technique. The Struts 2 documentation explains this technique (which uses the bang, !, operator in the action name) and recommends against using the "Dynamic Method Invocation" technique due to security and other reasons related to how this technique is implemented internally.

The Struts 2 documentation also recommends turning off the option to use the dynamic method invocation by setting struts.enable.DynamicMethodInvocation to FALSE in the Struts configuration.


By using the wildcard method selection technique explained above, I can significantly reduce the number of action mapping nodes I need to write and manage in the Struts 2 XML configuration file. Check out the example application and references listed below to learn more.


  1. Example Application Source Code
  2. Apache Struts 2 Documentation, Wildcard Method
  3. Apache Struts 2 Documentation, Dynamic Method Invocation
  4. Struts 2 In Action, Manning Publications, 2008, pages 362-365

Related Blog Entries

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
This blog helped me a lot.. thank you
# Posted By brunda | 12/3/11 7:05 AM
Hi Bruce,
Nice article...I have one query regarding method invocation.
Suppose i want to call method dynamically and that method name will come from DB. so the scenario is, there is one Master module and 2 sub modules lets say M1 and M2, Now every request from any module will go through Master Module, so if module M1 ask for method name suppose getName() which exist in M2 action class only (but M1 is not aware about it) then first he will send a request to Master Module (with method name as a parameter) then master module will find out for which method this request is for and then he will call M2 getName() method. To do this we dont need to Map method getName() into Struts.xml, We have to set this in Action class of Master Module. I am not able to understand how to do this Cause every time Master Module will get some method name (as Parameter) and will call respective action which include that method name.
Please Help

# Posted By Tapan | 1/17/12 11:19 PM
Tapan - I've not done something like you describe so I cannot help.
# Posted By Bruce | 1/18/12 9:19 AM
BlogCFC was created by Raymond Camden. This blog is running version Contact Blog Owner