| <!DOCTYPE html> |
| <html lang="en"> |
| <head> |
| <meta charset="UTF-8"/> |
| <meta name="viewport" content="width=device-width, initial-scale=1.0"/> |
| <meta name="Date-Revision-yyyymmdd" content="20140918"/> |
| <meta http-equiv="Content-Language" content="en"/> |
| <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> |
| |
| <title>Wildcard Method Selection</title> |
| |
| <link href="//fonts.googleapis.com/css?family=Source+Sans+Pro:300,400,600,700,400italic,600italic,700italic" rel="stylesheet" type="text/css"> |
| <link href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet"> |
| <link href="/css/main.css" rel="stylesheet"> |
| <link href="/css/custom.css" rel="stylesheet"> |
| <link href="/css/syntax.css" rel="stylesheet"> |
| |
| <script src="//code.jquery.com/jquery-1.11.0.min.js"></script> |
| <script type="text/javascript" src="/bootstrap/js/bootstrap.js"></script> |
| <script type="text/javascript" src="/js/community.js"></script> |
| |
| <!-- Matomo --> |
| <script> |
| var _paq = window._paq = window._paq || []; |
| /* tracker methods like "setCustomDimension" should be called before "trackPageView" */ |
| /* We explicitly disable cookie tracking to avoid privacy issues */ |
| _paq.push(['disableCookies']); |
| _paq.push(['trackPageView']); |
| _paq.push(['enableLinkTracking']); |
| (function() { |
| var u="//analytics.apache.org/"; |
| _paq.push(['setTrackerUrl', u+'matomo.php']); |
| _paq.push(['setSiteId', '41']); |
| var d=document, g=d.createElement('script'), s=d.getElementsByTagName('script')[0]; |
| g.async=true; g.src=u+'matomo.js'; s.parentNode.insertBefore(g,s); |
| })(); |
| </script> |
| <!-- End Matomo Code --> |
| </head> |
| <body> |
| |
| <a href="https://github.com/apache/struts" class="github-ribbon"> |
| <img decoding="async" loading="lazy" style="position: absolute; right: 0; border: 0;" width="149" height="149" src="https://github.blog/wp-content/uploads/2008/12/forkme_right_red_aa0000.png?resize=149%2C149" class="attachment-full size-full" alt="Fork me on GitHub" data-recalc-dims="1"> |
| </a> |
| |
| <header> |
| <nav> |
| <div role="navigation" class="navbar navbar-default navbar-fixed-top"> |
| <div class="container"> |
| <div class="navbar-header"> |
| <button type="button" data-toggle="collapse" data-target="#struts-menu" class="navbar-toggle"> |
| Menu |
| <span class="sr-only">Toggle navigation</span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| <span class="icon-bar"></span> |
| </button> |
| <a href="/index.html" class="navbar-brand logo"><img src="/img/struts-logo.svg"></a> |
| </div> |
| <div id="struts-menu" class="navbar-collapse collapse"> |
| <ul class="nav navbar-nav"> |
| <li class="dropdown"> |
| <a data-toggle="dropdown" href="#" class="dropdown-toggle"> |
| Home<b class="caret"></b> |
| </a> |
| <ul class="dropdown-menu"> |
| <li><a href="/index.html">Welcome</a></li> |
| <li><a href="/download.cgi">Download</a></li> |
| <li><a href="/releases.html">Releases</a></li> |
| <li><a href="/announce-2024.html">Announcements</a></li> |
| <li><a href="http://www.apache.org/licenses/">License</a></li> |
| <li><a href="https://www.apache.org/foundation/thanks.html">Thanks!</a></li> |
| <li><a href="https://www.apache.org/foundation/sponsorship.html">Sponsorship</a></li> |
| <li><a href="https://privacy.apache.org/policies/privacy-policy-public.html">Privacy Policy</a></li> |
| </ul> |
| </li> |
| <li class="dropdown"> |
| <a data-toggle="dropdown" href="#" class="dropdown-toggle"> |
| Support<b class="caret"></b> |
| </a> |
| <ul class="dropdown-menu"> |
| <li><a href="/mail.html">User Mailing List</a></li> |
| <li><a href="https://issues.apache.org/jira/browse/WW">Issue Tracker</a></li> |
| <li><a href="/security.html">Reporting Security Issues</a></li> |
| <li><a href="/commercial-support.html">Commercial Support</a></li> |
| <li class="divider"></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/WW/Migration+Guide">Version Notes</a></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/WW/Security+Bulletins">Security Bulletins</a></li> |
| <li class="divider"></li> |
| <li><a href="/maven/project-info.html">Maven Project Info</a></li> |
| <li><a href="/maven/struts2-core/dependencies.html">Struts Core Dependencies</a></li> |
| <li><a href="/maven/struts2-plugins/modules.html">Plugin Dependencies</a></li> |
| </ul> |
| </li> |
| <li class="dropdown"> |
| <a data-toggle="dropdown" href="#" class="dropdown-toggle"> |
| Documentation<b class="caret"></b> |
| </a> |
| <ul class="dropdown-menu"> |
| <li><a href="/birdseye.html">Birds Eye</a></li> |
| <li><a href="/primer.html">Key Technologies</a></li> |
| <li><a href="/kickstart.html">Kickstart FAQ</a></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/WW/Home">Wiki</a></li> |
| <li class="divider"></li> |
| <li><a href="/getting-started/">Getting Started</a></li> |
| <li><a href="/security/">Security Guide</a></li> |
| <li><a href="/core-developers/">Core Developers Guide</a></li> |
| <li><a href="/tag-developers/">Tag Developers Guide</a></li> |
| <li><a href="/maven-archetypes/">Maven Archetypes</a></li> |
| <li><a href="/plugins/">Plugins</a></li> |
| <li><a href="/maven/struts2-core/apidocs/index.html">Struts Core API</a></li> |
| <li><a href="/tag-developers/tag-reference.html">Tag reference</a></li> |
| <li><a href="https://cwiki.apache.org/confluence/display/WW/FAQs">FAQs</a></li> |
| <li><a href="http://cwiki.apache.org/S2PLUGINS/home.html">Plugin registry</a></li> |
| </ul> |
| </li> |
| <li class="dropdown"> |
| <a data-toggle="dropdown" href="#" class="dropdown-toggle"> |
| Contributing<b class="caret"></b> |
| </a> |
| <ul class="dropdown-menu"> |
| <li><a href="/youatstruts.html">You at Struts</a></li> |
| <li><a href="/helping.html">How to Help FAQ</a></li> |
| <li><a href="/dev-mail.html">Development Lists</a></li> |
| <li class="divider"></li> |
| <li><a href="/submitting-patches.html">Submitting patches</a></li> |
| <li><a href="/builds.html">Source Code and Builds</a></li> |
| <li><a href="/coding-standards.html">Coding standards</a></li> |
| <li><a href="/contributors/">Contributors Guide</a></li> |
| <li class="divider"></li> |
| <li><a href="/release-guidelines.html">Release Guidelines</a></li> |
| <li><a href="/bylaws.html">PMC Charter</a></li> |
| <li><a href="/volunteers.html">Volunteers</a></li> |
| <li><a href="https://gitbox.apache.org/repos/asf?p=struts.git">Source Repository</a></li> |
| <li><a href="/updating-website.html">Updating the website</a></li> |
| </ul> |
| </li> |
| <li class="apache"><a href="http://www.apache.org/"><img src="/img/apache.png"></a></li> |
| </ul> |
| </div> |
| </div> |
| </div> |
| </nav> |
| </header> |
| |
| |
| <article class="container"> |
| <section class="col-md-12"> |
| <a class="edit-on-gh" href="https://github.com/apache/struts-site/edit/master/source/getting-started/wildcard-method-selection.md" title="Edit this page on GitHub">Edit on GitHub</a> |
| |
| <a href="index.html" title="back to Getting started"><< back to Getting started</a> |
| |
| <h1 class="no_toc" id="wildcard-method-selection">Wildcard Method Selection</h1> |
| |
| <ul id="markdown-toc"> |
| <li><a href="#introduction" id="markdown-toc-introduction">Introduction</a></li> |
| <li><a href="#example-application" id="markdown-toc-example-application">Example Application</a></li> |
| <li><a href="#wildcard-method-selection-1" id="markdown-toc-wildcard-method-selection-1">Wildcard Method Selection</a></li> |
| <li><a href="#dynamic-method-invocation" id="markdown-toc-dynamic-method-invocation">Dynamic Method Invocation</a></li> |
| <li><a href="#summary" id="markdown-toc-summary">Summary</a></li> |
| </ul> |
| |
| <p>The example code for this tutorial, <strong>wildcard-method-selection</strong>, is available for checkout at <a href="https://github.com/apache/struts-examples">struts-examples</a></p> |
| |
| <h2 id="introduction">Introduction</h2> |
| |
| <p>In this tutorial we’ll cover how to configure an action node in the struts.xml configuration file so that one action |
| node can be used to relate several different Action URLs to specific methods of the Action class. This will reduce |
| the number of action nodes we must write in the struts.xml configuration file.</p> |
| |
| <p>The <a href="http://struts.apache.org/mail">Struts 2 user mailing list</a> is an excellent place to get help. If you are having |
| a problem getting the tutorial example applications to work search the Struts 2 mailing list. If you don’t find an answer |
| to your problem, post a question on the mailing list.</p> |
| |
| <h2 id="example-application">Example Application</h2> |
| |
| <p><img src="attachments/att21921805_Screen shot 2010-05-30 at 8.51.40 AM.png" alt="Screen shot 2010-05-30 at 8.51.40 AM.png" /></p> |
| |
| <p>The example application that supports this tutorial shows how to use Struts 2’s wildcard method selection methodology. |
| 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 <code class="language-plaintext highlighter-rouge">Person</code> (the model class) is controlled |
| by the Struts 2 <code class="language-plaintext highlighter-rouge">ActionSupport</code> class <code class="language-plaintext highlighter-rouge">PersonAction</code>. The <code class="language-plaintext highlighter-rouge">PersonAction</code> class has several different methods |
| (e.g. create, edit, delete) that are called depending on what the user wants to do.</p> |
| |
| <h2 id="wildcard-method-selection-1">Wildcard Method Selection</h2> |
| |
| <p>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:</p> |
| |
| <p><strong>Struts.xml Action Configuration</strong></p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><action</span> <span class="na">name=</span><span class="s">"createPerson"</span> <span class="na">class=</span><span class="s">"org.apache.struts.tutorials.wildcardmethod.action.PersonAction"</span> <span class="na">method=</span><span class="s">"create"</span><span class="nt">></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"input"</span><span class="nt">></span>input.jsp<span class="nt"></result></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"success"</span><span class="nt">></span>view.jsp<span class="nt"></result></span> |
| <span class="nt"></action></span> |
| |
| <span class="nt"><action</span> <span class="na">name=</span><span class="s">"editPerson"</span> <span class="na">class=</span><span class="s">"org.apache.struts.tutorials.wildcardmethod.action.PersonAction"</span> <span class="na">method=</span><span class="s">"edit"</span><span class="nt">></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"input"</span><span class="nt">></span>input.jsp<span class="nt"></result></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"success"</span><span class="nt">></span>view.jsp<span class="nt"></result></span> |
| <span class="nt"></action></span> |
| </code></pre></div></div> |
| |
| <p>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.</p> |
| |
| <p>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 |
| placeholder ( <code class="language-plaintext highlighter-rouge">{1}</code> ) for the method value. For example:</p> |
| |
| <p><strong>Struts.xml Action Configuration Using Wildcard Method Selection</strong></p> |
| |
| <div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><action</span> <span class="na">name=</span><span class="s">"*Person"</span> <span class="na">class=</span><span class="s">"org.apache.struts.tutorials.wildcardmethod.action.PersonAction"</span> <span class="na">method=</span><span class="s">"{1}"</span><span class="nt">></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"success"</span><span class="nt">></span>view.jsp<span class="nt"></result></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"input"</span><span class="nt">></span>input.jsp<span class="nt"></result></span> |
| <span class="nt"></action></span> |
| </code></pre></div></div> |
| |
| <p>The <code class="language-plaintext highlighter-rouge">*</code> 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 <code class="language-plaintext highlighter-rouge">{1}</code> plac holder will be replaced |
| with that value). For example this URL:</p> |
| |
| <p><a href="http://localhost:8080/wildcard-method-selection/createPerson.action">http://localhost:8080/wildcard-method-selection/createPerson.action</a></p> |
| |
| <p>will be be processed by the the above action mapping and method create of class PersonAction will be called. While this URL</p> |
| |
| <p><a href="http://localhost:8080/wildcard-method-selection/deletePerson.action">http://localhost:8080/wildcard-method-selection/deletePerson.action</a></p> |
| |
| <p>will cause the delete method of class <code class="language-plaintext highlighter-rouge">PersonAction</code> to be called.</p> |
| |
| <p>What happens if we have a URL with nothing in front of Person? For example:</p> |
| |
| <p><a href="http://localhost:8080/wildcard-method-selection/Person.action">http://localhost:8080/wildcard-method-selection/Person.action</a></p> |
| |
| <p>If there is no value in front of Person, then the Struts 2 framework will call the execute method of the class <code class="language-plaintext highlighter-rouge">PersonAction</code>.</p> |
| |
| <h2 id="dynamic-method-invocation">Dynamic Method Invocation</h2> |
| |
| <p>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, <code class="language-plaintext highlighter-rouge">!</code>, 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.</p> |
| |
| <p>The Struts 2 documentation also recommends turning off the option to use the dynamic method invocation by setting |
| <code class="language-plaintext highlighter-rouge">struts.enable.DynamicMethodInvocation</code> to <code class="language-plaintext highlighter-rouge">FALSE</code> in the Struts configuration.</p> |
| |
| <h2 id="summary">Summary</h2> |
| |
| <p>By using the wildcard method selection technique explained above, you can significantly reduce the number of action |
| mapping nodes you need to write and manage in the Struts 2 XML configuration file.</p> |
| |
| <table> |
| <tbody> |
| <tr> |
| <td>Return to <a href="form-validation-using-xml">Form validation using XML</a></td> |
| <td>or</td> |
| <td>onward to <a href="themes">Themes</a></td> |
| </tr> |
| </tbody> |
| </table> |
| |
| </section> |
| </article> |
| |
| |
| <footer class="container"> |
| <div class="col-md-12"> |
| Copyright © 2000-2022 <a href="https://www.apache.org/">The Apache Software Foundation</a>. |
| Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts project logos are |
| trademarks of The Apache Software Foundation. All Rights Reserved. |
| </div> |
| <div class="col-md-12">Logo and website design donated by <a href="https://softwaremill.com/">SoftwareMill</a>.</div> |
| </footer> |
| |
| <script>!function (d, s, id) { |
| var js, fjs = d.getElementsByTagName(s)[0]; |
| if (!d.getElementById(id)) { |
| js = d.createElement(s); |
| js.id = id; |
| js.src = "//platform.twitter.com/widgets.js"; |
| fjs.parentNode.insertBefore(js, fjs); |
| } |
| }(document, "script", "twitter-wjs");</script> |
| <script src="https://apis.google.com/js/platform.js" async="async" defer="defer"></script> |
| |
| <div id="fb-root"></div> |
| |
| <script>(function (d, s, id) { |
| var js, fjs = d.getElementsByTagName(s)[0]; |
| if (d.getElementById(id)) return; |
| js = d.createElement(s); |
| js.id = id; |
| js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1"; |
| fjs.parentNode.insertBefore(js, fjs); |
| }(document, 'script', 'facebook-jssdk'));</script> |
| |
| |
| </body> |
| </html> |