blob: 8aef3193c7a22fd82c41e94bc455c5401866e0b4 [file] [log] [blame]
<!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>Coding actions</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="/highlighter/github-theme.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>
</head>
<body>
<a href="http://github.com/apache/struts" class="github-ribbon">
<img style="position: absolute; right: 0; border: 0;" src="https://s3.amazonaws.com/github/ribbons/forkme_right_red_aa0000.png" alt="Fork me on GitHub">
</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.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>
</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 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><a href="/contributors/">Contributors Guide</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="https://cwiki.apache.org/confluence/display/WW/Contributors+Guide">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/coding-actions.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="coding-actions">Coding actions</h1>
<ul id="markdown-toc">
<li><a href="#introduction" id="markdown-toc-introduction">Introduction</a></li>
<li><a href="#struts-2-action-classes" id="markdown-toc-struts-2-action-classes">Struts 2 Action Classes</a></li>
<li><a href="#processing-form-input-in-the-action-class" id="markdown-toc-processing-form-input-in-the-action-class">Processing Form Input In The Action Class</a></li>
<li><a href="#summary" id="markdown-toc-summary">Summary</a></li>
</ul>
<p>This tutorial assumes you’ve completed the <a href="using-tags.html">Using Struts 2 Tags</a> tutorial and have a working <strong>using-tags</strong>
project. The example code for this tutorial, <strong>coding-actions</strong>, is available for checkout from the Struts 2 GitHub repository
<a href="https://github.com/apache/struts-examples">struts-examples</a>.</p>
<h2 id="introduction">Introduction</h2>
<p>Coding a Struts 2 Action involves several parts:</p>
<ol>
<li>Mapping an action to a class</li>
<li>Mapping a result to a view</li>
<li>Writing the controller logic in the Action class</li>
</ol>
<p>In the previous tutorials we covered how to configure Struts to map a URL such as <code class="highlighter-rouge">hello.action</code> to an Action class such as <code class="highlighter-rouge">HelloWorldAction</code> (specifically the execute method).</p>
<p><strong>Action Mapping</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;action</span> <span class="na">name=</span><span class="s">"hello"</span> <span class="na">class=</span><span class="s">"org.apache.struts.helloworld.action.HelloWorldAction"</span> <span class="na">method=</span><span class="s">"execute"</span><span class="nt">&gt;</span>
<span class="nt">&lt;result</span> <span class="na">name=</span><span class="s">"success"</span><span class="nt">&gt;</span>/HelloWorld.jsp<span class="nt">&lt;/result&gt;</span>
<span class="nt">&lt;/action&gt;</span>
</code></pre></div></div>
<p>The Action mapping above also specified that if the <code class="highlighter-rouge">execute</code> method of class <code class="highlighter-rouge">HelloWorldAction</code> returns <code class="highlighter-rouge">success</code> then the view <code class="highlighter-rouge">HelloWorld.jsp</code> will be returned to the browser.</p>
<p>This tutorial will introduce you to the basics of writing the controller logic in the Action class.</p>
<h2 id="struts-2-action-classes">Struts 2 Action Classes</h2>
<p>Action classes act as the controller in the MVC pattern. Action classes respond to a user action, execute business logic (or call upon other classes to do that), and then return a result that tells Struts what view to render.</p>
<p>Struts 2 Action classes usually extend the <code class="highlighter-rouge">ActionSupport</code> class, which is provided by the Struts 2 framework. Class <code class="highlighter-rouge">ActionSupport</code> provides default implementations for the most common actions (e.g. execute, input) and also implements several useful Struts 2 interfaces. When your Action class extends class <code class="highlighter-rouge">ActionSupport</code> your class can either override the default implementations or inherit them.</p>
<p>If you examine class HelloWorldAction from tutorial <a href="using-tags.html">Using Struts 2 Tags</a> you’ll see that it extends the class <code class="highlighter-rouge">ActionSupport</code> and then overrides method <code class="highlighter-rouge">execute</code>.</p>
<p>The method <code class="highlighter-rouge">execute</code> is where we placed what we want this controller to do in response to the <code class="highlighter-rouge">hello.action</code>.</p>
<p><strong>Method execute of HelloWorldAction</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="n">String</span> <span class="nf">execute</span><span class="o">()</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span>
<span class="n">messageStore</span> <span class="o">=</span> <span class="k">new</span> <span class="n">MessageStore</span><span class="o">()</span> <span class="o">;</span>
<span class="n">helloCount</span><span class="o">++;</span>
<span class="k">return</span> <span class="n">SUCCESS</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div></div>
<blockquote>
<p>Note that method execute declares it throws an Exception. We’ll cover in a later tutorial how to configure Struts to handle any Exceptions thrown from the Action classes methods.</p>
</blockquote>
<h2 id="processing-form-input-in-the-action-class">Processing Form Input In The Action Class</h2>
<p>One of the most common responsibilities of the Action class is to process user input on a form and then make the result of the processing available to the view page. To illustrate this responsibility, let’s say that on our view page, <code class="highlighter-rouge">HelloWorld.jsp</code>, we want to display a personal hello, such as “Hello Struts User Bruce.”</p>
<p>In the <a href="using-tags.html">Using Struts 2 Tags</a> example application we added a Struts 2 form to <code class="highlighter-rouge">index.jsp</code>.</p>
<p><strong>Struts 2 Form Tags</strong></p>
<div class="language-xml highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;s:form</span> <span class="na">action=</span><span class="s">"hello"</span><span class="nt">&gt;</span>
<span class="nt">&lt;s:textfield</span> <span class="na">name=</span><span class="s">"userName"</span> <span class="na">label=</span><span class="s">"Your name"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;s:submit</span> <span class="na">value=</span><span class="s">"Submit"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;/s:form&gt;</span>
</code></pre></div></div>
<p>Make a note of the value of the name attribute for the Struts 2 textfield tag, which is userName. When the user clicks on the submit button for the above form, the action hello will be executed (<code class="highlighter-rouge">hello.action</code>). The form field values will be posted to the Struts 2 Action class (<code class="highlighter-rouge">HelloWorldAction</code>). The Action class may automatically receive those form field values provided it has a public set method that matches the form field name value.</p>
<p>So for the HelloWorldAction class to automatically receive the userName value it must have a public method setUserName (note the JavaBean convention discussed in tutorial <a href="hello-world-using-struts2.html">Hello World</a>).</p>
<p>For the example application associated with this tutorial, add the following Java code to class <code class="highlighter-rouge">HelloWorldAction</code>.</p>
<p><strong>Add userName to HelloWorldAction</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">private</span> <span class="n">String</span> <span class="n">userName</span><span class="o">;</span>
<span class="kd">public</span> <span class="n">String</span> <span class="nf">getUserName</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">userName</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setUserName</span><span class="o">(</span><span class="n">String</span> <span class="n">userName</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">userName</span> <span class="o">=</span> <span class="n">userName</span><span class="o">;</span>
<span class="o">}</span>
</code></pre></div></div>
<p>To personalize the MessageStore message (recall that class MessageStore is storing the message to display) add the following Java code to the HelloWorldAction’s execute method after the statement that instantiates the MessageStore object.</p>
<p><strong>Add userName value to message</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="k">if</span> <span class="o">(</span><span class="n">userName</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">)</span> <span class="o">{</span>
<span class="n">messageStore</span><span class="o">.</span><span class="na">setMessage</span><span class="o">(</span> <span class="n">messageStore</span><span class="o">.</span><span class="na">getMessage</span><span class="o">()</span> <span class="o">+</span> <span class="s">" "</span> <span class="o">+</span> <span class="n">userName</span><span class="o">);</span>
<span class="o">}</span>
</code></pre></div></div>
<p>Now build and run (<code class="highlighter-rouge">mvn jetty:run</code>) the application. Enter your name in the form and click the submit button. You should see the following page.</p>
<p><img src="attachments/coding_actions_form_submit_result.png" alt="coding_actions_form_submit_result.png" /></p>
<p>When the form is submitted, Struts will call any set methods of the HelloWorldAction class that match the form field names. So in this example method <code class="highlighter-rouge">setUserName</code> was called and passed the value the user entered in the <code class="highlighter-rouge">userName</code> form field.</p>
<p>On the <code class="highlighter-rouge">index.jsp</code> we also have a Struts 2 action link (see tutorial <a href="using-tags.html">Using Struts 2 Tags</a>) that includes a query string parameter: <code class="highlighter-rouge">userName=Bruce+Phillips</code>. If you click on that link you should see the following result:</p>
<p><img src="attachments/coding_actions_link_with_param_result.png" alt="coding_actions_link_with_param_result.png" /></p>
<p>Since the query string parameter is <code class="highlighter-rouge">userName</code>, Struts passed the value of that parameter to the <code class="highlighter-rouge">setUserName</code> method.</p>
<p>On the view page, <code class="highlighter-rouge">HelloWorld.jsp</code>, you can also access the <code class="highlighter-rouge">userName</code> value by using the Struts 2 property tag (see tutorial <a href="using-tags.html">Using Struts 2 Tags</a>). Try showing just the <code class="highlighter-rouge">userName</code> value on the view page.</p>
<h2 id="summary">Summary</h2>
<p>This tutorial introduced you to how to code the Action class so it can process user input on a form or values in a query string parameter. If the form had numerous fields, it would be cumbersome to have a set method that matches up with each form field. So our next tutorial will cover how to integrate a model class, form fields in the view and form processing in the Action class.</p>
<table>
<tbody>
<tr>
<td>Return to <a href="using-tags.html">Using Struts 2 Tags</a></td>
<td>or</td>
<td>onward to <a href="processing-forms.html">Processing Forms</a></td>
</tr>
</tbody>
</table>
</section>
</article>
<footer class="container">
<div class="col-md-12">
Copyright &copy; 2000-2018 <a href="http://www.apache.org/">The Apache Software Foundation </a>.
All Rights Reserved.
</div>
<div class="col-md-12">
Apache Struts, Struts, Apache, the Apache feather logo, and the Apache Struts project logos are
trademarks of The Apache Software Foundation.
</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>