blob: 8c478b7bee5b468b8f09adfef684b54cd5ea646b [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>Processing forms</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/processing-forms.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="processing-forms">Processing Forms</h1>
<ul id="markdown-toc">
<li><a href="#introduction" id="markdown-toc-introduction">Introduction</a></li>
<li><a href="#forms-and-a-java-model-class" id="markdown-toc-forms-and-a-java-model-class">Forms and a Java model class</a></li>
<li><a href="#form-structure" id="markdown-toc-form-structure">Form structure</a></li>
<li><a href="#creating-the-action-class-to-handle-the-form-submission" id="markdown-toc-creating-the-action-class-to-handle-the-form-submission">Creating the Action class to handle the form submission</a></li>
<li><a href="#adding-the-view-for-the-result" id="markdown-toc-adding-the-view-for-the-result">Adding the view for the result</a></li>
<li><a href="#create-action-mapping-in-strutsxml" id="markdown-toc-create-action-mapping-in-strutsxml">Create action mapping in struts.xml</a></li>
<li><a href="#create-a-link-to-registerjsp" id="markdown-toc-create-a-link-to-registerjsp">Create a link to register.jsp</a></li>
</ul>
<p>This tutorial assumes you’ve completed the <a href="coding-actions">Coding Struts 2 Actions</a> tutorial and have a working
<strong>coding-actions</strong> project. The example code for this tutorial, <strong>form-processing</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>In this tutorial we’ll explore using Struts 2 to do more involved processing of a form submission. We’ll cover how to
use a Java model class to store the form input and how to create the Struts 2 form to match up with that model class.</p>
<p>The code provided in this tutorial may be added to the <a href="coding-actions">Coding Struts 2 Actions</a> example or you
can download this complete example from the github respository - <a href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a>.</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="forms-and-a-java-model-class">Forms and a Java model class</h2>
<p>For this tutorial let’s say we need to provide a form that a user may submit to register for a prize drawing. Our
business rules state the user must provide his/her first name, last name, email address, and age.</p>
<p>To encapsulate this data, we’ll use a simple Java class that follows the basic Java Bean specifications (public set/get
methods for each instance field). If you’re following along add this class to the package <code class="language-plaintext highlighter-rouge">org.apache.struts.register.model</code>
in the <a href="coding-actions">Coding Struts 2 Actions</a> example.</p>
<p><strong>Person.java</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Person</span> <span class="o">{</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">firstName</span><span class="o">;</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">lastName</span><span class="o">;</span>
<span class="kd">private</span> <span class="nc">String</span> <span class="n">email</span><span class="o">;</span>
<span class="kd">private</span> <span class="kt">int</span> <span class="n">age</span><span class="o">;</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">getFirstName</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">firstName</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setFirstName</span><span class="o">(</span><span class="nc">String</span> <span class="n">firstName</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">firstName</span> <span class="o">=</span> <span class="n">firstName</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">getLastName</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">lastName</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setLastName</span><span class="o">(</span><span class="nc">String</span> <span class="n">lastName</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">lastName</span> <span class="o">=</span> <span class="n">lastName</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">getEmail</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">email</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setEmail</span><span class="o">(</span><span class="nc">String</span> <span class="n">email</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">email</span> <span class="o">=</span> <span class="n">email</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">int</span> <span class="nf">getAge</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">age</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setAge</span><span class="o">(</span><span class="kt">int</span> <span class="n">age</span><span class="o">)</span> <span class="o">{</span>
<span class="k">this</span><span class="o">.</span><span class="na">age</span> <span class="o">=</span> <span class="n">age</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">toString</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="s">"First Name: "</span> <span class="o">+</span> <span class="n">getFirstName</span><span class="o">()</span> <span class="o">+</span> <span class="s">" Last Name: "</span> <span class="o">+</span> <span class="n">getLastName</span><span class="o">()</span> <span class="o">+</span>
<span class="s">" Email: "</span> <span class="o">+</span> <span class="n">getEmail</span><span class="o">()</span> <span class="o">+</span> <span class="s">" Age: "</span> <span class="o">+</span> <span class="n">getAge</span><span class="o">()</span> <span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p>Note a few points about the above class. There is a public set/get method for each instance field. The age attribute
is of type integer. We’ve defined a public <code class="language-plaintext highlighter-rouge">toString</code> method that returns a String representing the state of the object.
Since we haven’t specified a constructor, Java will provide a default constructor that will set all instance fields to
their null values.</p>
<h2 id="form-structure">Form structure</h2>
<p>To collect the above information we’ll use a Struts 2 form. When creating this form the key concept we need to employ
is to tie each form field to a specific instance field of an object of type Person. Let’s look over the form first and
then discuss some key points. Create a view page named <code class="language-plaintext highlighter-rouge">register.jsp</code> (in <code class="language-plaintext highlighter-rouge">src/main/webapp</code>)</p>
<p><strong>register.jsp</strong></p>
<div class="language-jsp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;%@ taglib </span><span class="na">prefix=</span><span class="s">"s"</span><span class="na"> uri=</span><span class="s">"/struts-tags"</span> <span class="nt">%&gt;</span>
<span class="nt">&lt;%@ page </span><span class="na">language=</span><span class="s">"java"</span><span class="na"> contentType=</span><span class="s">"text/html; charset=UTF-8"</span><span class="na"> pageEncoding=</span><span class="s">"UTF-8"</span><span class="nt">%&gt;</span>
<span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;meta</span> <span class="na">http-equiv=</span><span class="s">"Content-Type"</span> <span class="na">content=</span><span class="s">"text/html; charset=UTF-8"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;title&gt;</span>Register<span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;h3&gt;</span>Register for a prize by completing this form.<span class="nt">&lt;/h3&gt;</span>
<span class="nt">&lt;s:form </span><span class="na">action=</span><span class="s">"register"</span><span class="nt">&gt;</span>
<span class="nt">&lt;s:textfield </span><span class="na">name=</span><span class="s">"personBean.firstName"</span><span class="na"> label=</span><span class="s">"First name"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;s:textfield </span><span class="na">name=</span><span class="s">"personBean.lastName"</span><span class="na"> label=</span><span class="s">"Last name"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;s:textfield </span><span class="na">name=</span><span class="s">"personBean.email"</span><span class="na"> label =</span><span class="s">"Email"</span><span class="nt">/&gt;</span>
<span class="nt">&lt;s:textfield </span><span class="na">name=</span><span class="s">"personBean.age"</span><span class="na"> label=</span><span class="s">"Age"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;s:submit/&gt;</span>
<span class="nt">&lt;/s:form&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</code></pre></div></div>
<p>Since we are using Struts 2 tags, at the top of the page we need the Struts tag library declaration.</p>
<p>The Struts 2 form will submit to an action named register. We’ll need to define that action in our <code class="language-plaintext highlighter-rouge">struts.xml</code> file.</p>
<p>Note the four Struts 2 textfield tags. Each tag has a name value that includes an attribute of the <code class="language-plaintext highlighter-rouge">Person</code> class
(e.g. <code class="language-plaintext highlighter-rouge">firstName</code>). The name attribute’s value also has a reference to an object called <code class="language-plaintext highlighter-rouge">personBean</code>. This object is
of type <code class="language-plaintext highlighter-rouge">Person</code>. When we create the Action class that handles this form submission, we’ll have to specify that object
in that Action class and annotate it (see below).</p>
<p>The complete name value, <code class="language-plaintext highlighter-rouge">personBean.firstName</code>, instructs Struts 2 to use the input value for that textfield as
the argument to the personBean object’s <code class="language-plaintext highlighter-rouge">setFirstName</code> method. So if the user types “Bruce” in the textfield that has
the label “First name”, the personBean’s <code class="language-plaintext highlighter-rouge">firstName</code> instance field will have a value of “Bruce”.</p>
<p>Note that we have a Struts 2 textfield for each instance field of the class Person. Remember that Person class’s age
attribute is of type integer. All form field input values are Strings. Struts 2 will automatically convert the String
value (“25”) the user entered for the age form field to 25 when calling the <code class="language-plaintext highlighter-rouge">setAge</code> method of object <code class="language-plaintext highlighter-rouge">personBean</code>.</p>
<h2 id="creating-the-action-class-to-handle-the-form-submission">Creating the Action class to handle the form submission</h2>
<p>When the user clicks on the submit button of the above form, the action “register” and the form data will be sent to
the Struts 2 framework. We need an Action class to process this form. If you recall from the tutorial
<a href="coding-actions">Coding Struts 2 Actions</a> our Action class should extend the Struts 2 ActionSupport class.</p>
<p>Here is the Action class used for this example. Place it in package org.apache.struts.register.action.</p>
<p><strong>Register.java Struts 2 Action Class</strong></p>
<div class="language-java highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="kn">package</span> <span class="nn">org.apache.struts.register.action</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">com.opensymphony.xwork2.ActionSupport</span><span class="o">;</span>
<span class="kn">import</span> <span class="nn">org.apache.struts.register.model.Person</span><span class="o">;</span>
<span class="kd">public</span> <span class="kd">class</span> <span class="nc">Register</span> <span class="kd">extends</span> <span class="nc">ActionSupport</span> <span class="o">{</span>
<span class="kd">private</span> <span class="kd">static</span> <span class="kd">final</span> <span class="kt">long</span> <span class="n">serialVersionUID</span> <span class="o">=</span> <span class="mi">1L</span><span class="o">;</span>
<span class="kd">private</span> <span class="nc">Person</span> <span class="n">personBean</span><span class="o">;</span>
<span class="kd">public</span> <span class="nc">String</span> <span class="nf">execute</span><span class="o">()</span> <span class="kd">throws</span> <span class="nc">Exception</span> <span class="o">{</span>
<span class="c1">//call Service class to store personBean's state in database</span>
<span class="k">return</span> <span class="no">SUCCESS</span><span class="o">;</span>
<span class="o">}</span>
<span class="nd">@StrutsParameter</span><span class="o">(</span><span class="n">depth</span> <span class="o">=</span> <span class="mi">1</span><span class="o">)</span>
<span class="kd">public</span> <span class="nc">Person</span> <span class="nf">getPersonBean</span><span class="o">()</span> <span class="o">{</span>
<span class="k">return</span> <span class="n">personBean</span><span class="o">;</span>
<span class="o">}</span>
<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setPersonBean</span><span class="o">(</span><span class="nc">Person</span> <span class="n">person</span><span class="o">)</span> <span class="o">{</span>
<span class="n">personBean</span> <span class="o">=</span> <span class="n">person</span><span class="o">;</span>
<span class="o">}</span>
<span class="o">}</span>
</code></pre></div></div>
<p>In the <code class="language-plaintext highlighter-rouge">Register</code> class, note that we’ve declared an attribute named <code class="language-plaintext highlighter-rouge">personBean</code> of type <code class="language-plaintext highlighter-rouge">Person</code>, there are public
getter and setter methods for this object, and the getter is annotated with <code class="language-plaintext highlighter-rouge">@StrutsParameter(depth = 1)</code>.</p>
<p>In the previous <a href="coding-actions">Coding Struts 2 Actions</a> tutorial, we annotated the username <strong>setter</strong>, which took a
simple String as its parameter type, with <code class="language-plaintext highlighter-rouge">@StrutsParameter</code>. In this example, we are using a “Bean” object (sometimes
referred to as a DTO or model object) to encapsulate the form data. When we choose to use a DTO instead of a primitive,
String, or other TypeConverter supported object, we must annotate the <strong>getter</strong> method instead, and also assign a depth
corresponding to how deep the DTO graph is. In this case, the <code class="language-plaintext highlighter-rouge">Person</code> object does not have any further DTOs or
collections within it, so a depth of 1 will suffice.</p>
<p>For more information on these annotations and their security implications, please refer
to <a href="../security/index#defining-and-annotating-your-action-parameters">Security</a>.</p>
<p>The <code class="language-plaintext highlighter-rouge">Register</code> class also overrides the <code class="language-plaintext highlighter-rouge">execute</code> method. The <code class="language-plaintext highlighter-rouge">execute</code> method is the one we will specify in the
<code class="language-plaintext highlighter-rouge">struts.xml</code> to be called in response to the register action. In this example, the <code class="language-plaintext highlighter-rouge">execute</code> method just returns
the String constant <code class="language-plaintext highlighter-rouge">SUCCESS</code> (inherited from the <code class="language-plaintext highlighter-rouge">ActionSupport</code> class). But in a real application, within the <code class="language-plaintext highlighter-rouge">execute</code>
method we would call upon other classes (Service objects) to perform the business processing of the form, such as storing
the user’s input into a data repository.</p>
<p>The <code class="language-plaintext highlighter-rouge">personBean</code> getter of return type <code class="language-plaintext highlighter-rouge">Person</code> declared in the Register Action class matches the <code class="language-plaintext highlighter-rouge">personBean</code> name we
used in the form’s textfields. When the form is submitted, the Struts 2 framework will inspect the Action class and look
for a getter for <code class="language-plaintext highlighter-rouge">personBean</code>. If it returns <code class="language-plaintext highlighter-rouge">null</code> and a matching setter exists, it will create that object using the
<code class="language-plaintext highlighter-rouge">Person</code> class’s default constructor and set it using the setter. Note that the setter can be omitted if your Action
initialises the field on construction. Then for each form field that has a name value of personBean.someAttribute
(e.g <code class="language-plaintext highlighter-rouge">personBean.firstName</code>) it will call the personBean’s public set method for that attribute and pass it the form
field’s value (the user input). This all happens before the execute method occurs.</p>
<p>When Struts 2 runs the <code class="language-plaintext highlighter-rouge">execute</code> method of class <code class="language-plaintext highlighter-rouge">Register</code>, the <code class="language-plaintext highlighter-rouge">personBean</code> object in class <code class="language-plaintext highlighter-rouge">Register</code> now has values
for its instance fields that are equal to the values the user entered into the corresponding form fields.</p>
<p>By using a Java model class to encapsulate the data provided by the form we don’t have to have a separate attribute
(with public set/get methods) in the Action class (Register) for each form field.</p>
<h2 id="adding-the-view-for-the-result">Adding the view for the result</h2>
<p>When <code class="language-plaintext highlighter-rouge">SUCCESS</code> is returned by the <code class="language-plaintext highlighter-rouge">execute</code> method we want to display a simple thank you page that shows the user’s
registration. Add the <code class="language-plaintext highlighter-rouge">thankyou.jsp</code> below to <code class="language-plaintext highlighter-rouge">src/main/webapp</code>.</p>
<p><strong>thankyou.jsp</strong></p>
<div class="language-jsp highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;%@ taglib </span><span class="na">prefix=</span><span class="s">"s"</span><span class="na"> uri=</span><span class="s">"/struts-tags"</span> <span class="nt">%&gt;</span>
<span class="nt">&lt;%@ page </span><span class="na">language=</span><span class="s">"java"</span><span class="na"> contentType=</span><span class="s">"text/html; charset=UTF-8"</span><span class="na"> pageEncoding=</span><span class="s">"UTF-8"</span><span class="nt">%&gt;</span>
<span class="cp">&lt;!DOCTYPE html&gt;</span>
<span class="nt">&lt;html&gt;</span>
<span class="nt">&lt;head&gt;</span>
<span class="nt">&lt;meta</span> <span class="na">http-equiv=</span><span class="s">"Content-Type"</span> <span class="na">content=</span><span class="s">"text/html; charset=UTF-8"</span> <span class="nt">/&gt;</span>
<span class="nt">&lt;title&gt;</span>Registration Successful<span class="nt">&lt;/title&gt;</span>
<span class="nt">&lt;/head&gt;</span>
<span class="nt">&lt;body&gt;</span>
<span class="nt">&lt;h3&gt;</span>Thank you for registering for a prize.<span class="nt">&lt;/h3&gt;</span>
<span class="nt">&lt;p&gt;</span>Your registration information: <span class="nt">&lt;s:property </span><span class="na">value=</span><span class="s">"personBean"</span> <span class="nt">/&gt;</span> <span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;p&gt;&lt;a</span> <span class="na">href=</span><span class="s">"</span><span class="nt">&lt;s:url </span><span class="na">action=</span><span class="s">'index' </span><span class="nt">/&gt;</span><span class="s">"</span> <span class="nt">&gt;</span>Return to home page<span class="nt">&lt;/a&gt;</span>.<span class="nt">&lt;/p&gt;</span>
<span class="nt">&lt;/body&gt;</span>
<span class="nt">&lt;/html&gt;</span>
</code></pre></div></div>
<p>If you don’t recall how the Struts 2 property and url tags work consult the <a href="using-tags">Using Struts 2 Tags</a> tutorial.</p>
<h2 id="create-action-mapping-in-strutsxml">Create action mapping in struts.xml</h2>
<p>To specify the relationship between the form submission page, the Struts 2 Action class, and the success view page
we need to add an action node to <code class="language-plaintext highlighter-rouge">struts.xml</code>. Add this action node to <code class="language-plaintext highlighter-rouge">struts.xml</code> (<code class="language-plaintext highlighter-rouge">src/main/resources</code>) after
the hello action and before the closing package node.</p>
<p><strong>action node for struts.xml</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">"register"</span> <span class="na">class=</span><span class="s">"org.apache.struts.register.action.Register"</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>/thankyou.jsp<span class="nt">&lt;/result&gt;</span>
<span class="nt">&lt;/action&gt;</span>
</code></pre></div></div>
<p>The above action tells Struts 2 that when the register action is provided to call method <code class="language-plaintext highlighter-rouge">execute</code> of class <code class="language-plaintext highlighter-rouge">Register</code>.
If that method returns result “success” return to the browser the <code class="language-plaintext highlighter-rouge">thankyou.jsp</code>.</p>
<p>Note that we don’t need to tell Struts 2 anything about processing the form. The transfer of the form field input values
to the <code class="language-plaintext highlighter-rouge">personBean</code> object will happen automatically provided we’ve followed the convention of naming our form fields
to match personBean.attributeName (e.g. <code class="language-plaintext highlighter-rouge">personBean.lastName</code>).</p>
<h2 id="create-a-link-to-registerjsp">Create a link to register.jsp</h2>
<p>So that the user can find the registration page, add this link to index.jsp</p>
<p><strong>Link to register.jsp</strong></p>
<div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt">&lt;p&gt;&lt;a</span> <span class="na">href=</span><span class="s">"register.jsp"</span><span class="nt">&gt;</span>Please register<span class="nt">&lt;/a&gt;</span> for our prize drawing.<span class="nt">&lt;/p&gt;</span>
</code></pre></div></div>
<p><strong>Run The Example</strong></p>
<p>If everything is correct, you should be able to run the application (using <code class="language-plaintext highlighter-rouge">mvn jetty:run</code>), and open this URL in your
web browser:
<a href="http://localhost:8080/form-processing/index.action">http://localhost:8080/form-processing/index.action</a>. On that page
should be a link to register.
Click on that link and you should see the <code class="language-plaintext highlighter-rouge">register.jsp</code> page.</p>
<p><img src="attachments/att14974999_registerjsp.png" alt="registerjsp.png" /></p>
<p>Fill out the form and click the submit button. You should then see the <code class="language-plaintext highlighter-rouge">thankyou.jsp</code> page.</p>
<p><img src="attachments/att14975000_thanyoujsp.png" alt="thanyoujsp.png" /></p>
<table>
<tbody>
<tr>
<td>Return to <a href="coding-actions">Coding Struts 2 Actions</a></td>
<td>or</td>
<td>onward to <a href="form-validation">Form validation</a></td>
</tr>
</tbody>
</table>
</section>
</article>
<footer class="container">
<div class="col-md-12">
Copyright &copy; 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>