| <!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="/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-2021.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/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.html">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.html">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.html">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="highlighter-rouge">org.apache.struts.register.model</code> |
| in the <a href="coding-actions.html">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="n">String</span> <span class="n">firstName</span><span class="o">;</span> |
| <span class="kd">private</span> <span class="n">String</span> <span class="n">lastName</span><span class="o">;</span> |
| <span class="kd">private</span> <span class="n">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="n">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="n">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="n">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="n">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="n">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="n">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="n">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="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="highlighter-rouge">register.jsp</code> (in <code class="highlighter-rouge">src/main/webapp</code>)</p> |
| |
| <p><strong>register.jsp</strong></p> |
| |
| <pre><code class="language-jsp"><%@ taglib prefix="s" uri="/struts-tags" %> |
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| <title>Register</title> |
| </head> |
| <body> |
| <h3>Register for a prize by completing this form.</h3> |
| |
| <s:form action="register"> |
| <s:textfield name="personBean.firstName" label="First name" /> |
| <s:textfield name="personBean.lastName" label="Last name" /> |
| <s:textfield name="personBean.email" label ="Email"/> |
| <s:textfield name="personBean.age" label="Age" /> |
| <s:submit/> |
| </s:form> |
| </body> |
| </html> |
| </code></pre> |
| |
| <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="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="highlighter-rouge">Person</code> class |
| (e.g. <code class="highlighter-rouge">firstName</code>). The name attribute’s value also has a reference to an object called <code class="highlighter-rouge">personBean</code>. This object is |
| of type <code class="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 (see below).</p> |
| |
| <p>The complete name value, <code class="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="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="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="highlighter-rouge">setAge</code> method of object <code class="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.html">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="n">org</span><span class="o">.</span><span class="na">apache</span><span class="o">.</span><span class="na">struts</span><span class="o">.</span><span class="na">register</span><span class="o">.</span><span class="na">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="n">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="n">Person</span> <span class="n">personBean</span><span class="o">;</span> |
| |
| |
| <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="c1">//call Service class to store personBean's state in database</span> |
| |
| <span class="k">return</span> <span class="n">SUCCESS</span><span class="o">;</span> |
| <span class="o">}</span> |
| |
| <span class="kd">public</span> <span class="n">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="n">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="highlighter-rouge">Register</code> class note that we’ve declared an attribute named <code class="highlighter-rouge">personBean</code> of type <code class="highlighter-rouge">Person</code> and there is a public |
| get and set method for this object.</p> |
| |
| <p>The <code class="highlighter-rouge">Register</code> class also overrides the <code class="highlighter-rouge">execute</code> method. The <code class="highlighter-rouge">execute</code> method is the one we will specify in the |
| <code class="highlighter-rouge">struts.xml</code> to be called in response to the register action. In this example, the <code class="highlighter-rouge">execute</code> method just returns |
| the String constant <code class="highlighter-rouge">SUCCESS</code> (inherited from the <code class="highlighter-rouge">ActionSupport</code> class). But in a real application, within the <code class="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="highlighter-rouge">personBean</code> object of type <code class="highlighter-rouge">Person</code> declared in the Register Action class matches the <code class="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 |
| an object named <code class="highlighter-rouge">personBean</code>. It will create that object using the <code class="highlighter-rouge">Person</code> class’s default constructor. Then for each |
| form field that has a name value of personBean.someAttribute (e.g <code class="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="highlighter-rouge">execute</code> method of class <code class="highlighter-rouge">Register</code>, the <code class="highlighter-rouge">personBean</code> object in class <code class="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="highlighter-rouge">SUCCESS</code> is returned by the <code class="highlighter-rouge">execute</code> method we want to display a simple thank you page that shows the user’s |
| registration. Add the <code class="highlighter-rouge">thankyou.jsp</code> below to <code class="highlighter-rouge">src/main/webapp</code>.</p> |
| |
| <p><strong>thankyou.jsp</strong></p> |
| |
| <pre><code class="language-jsp"><%@ taglib prefix="s" uri="/struts-tags" %> |
| <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> |
| <!DOCTYPE html> |
| <html> |
| <head> |
| <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> |
| <title>Registration Successful</title> |
| </head> |
| <body> |
| <h3>Thank you for registering for a prize.</h3> |
| |
| <p>Your registration information: <s:property value="personBean" /> </p> |
| |
| <p><a href="<s:url action='index' />" >Return to home page</a>.</p> |
| </body> |
| </html> |
| </code></pre> |
| |
| <p>If you don’t recall how the Struts 2 property and url tags work consult the <a href="using-tags.html">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="highlighter-rouge">struts.xml</code>. Add this action node to <code class="highlighter-rouge">struts.xml</code> (<code class="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"><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">></span> |
| <span class="nt"><result</span> <span class="na">name=</span><span class="s">"success"</span><span class="nt">></span>/thankyou.jsp<span class="nt"></result></span> |
| <span class="nt"></action></span> |
| </code></pre></div></div> |
| |
| <p>The above action tells Struts 2 that when the register action is provided to call method <code class="highlighter-rouge">execute</code> of class <code class="highlighter-rouge">Register</code>. |
| If that method returns result “success” return to the browser the <code class="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="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="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"><p><a</span> <span class="na">href=</span><span class="s">"register.jsp"</span><span class="nt">></span>Please register<span class="nt"></a></span> for our prize drawing.<span class="nt"></p></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="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="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="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.html">Coding Struts 2 Actions</a></td> |
| <td>or</td> |
| <td>onward to <a href="form-validation.html">Form validation</a></td> |
| </tr> |
| </tbody> |
| </table> |
| |
| </section> |
| </article> |
| |
| |
| <footer class="container"> |
| <div class="col-md-12"> |
| Copyright © 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> |