| <!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>Form tags</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/form-tags.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="form-tags">Form Tags</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="#struts-2-select-tag" id="markdown-toc-struts-2-select-tag">Struts 2 Select Tag</a></li> |
| <li><a href="#struts-2-radio-tag" id="markdown-toc-struts-2-radio-tag">Struts 2 Radio Tag</a></li> |
| <li><a href="#struts-2-select-tag---object-backed" id="markdown-toc-struts-2-select-tag---object-backed">Struts 2 Select Tag - Object Backed</a></li> |
| <li><a href="#struts-2-checkbox-tag" id="markdown-toc-struts-2-checkbox-tag">Struts 2 Checkbox Tag</a></li> |
| <li><a href="#struts-2-checkboxlist-tag" id="markdown-toc-struts-2-checkboxlist-tag">Struts 2 checkboxlist Tag</a></li> |
| <li><a href="#summary" id="markdown-toc-summary">Summary</a></li> |
| </ul> |
| |
| <p>The example code for this tutorial, <strong>form-tags</strong>, can be checked out from <a href="https://github.com/apache/struts-examples">struts-examples</a>.</p> |
| |
| <h2 id="introduction">Introduction</h2> |
| |
| <p>In this tutorial we’ll explore some of the other Struts 2 form controls. In our previous tutorials that explained how |
| to use Struts 2 forms (<a href="processing-forms.html">Processing forms</a>, <a href="form-validation.html">Form validation</a>, |
| and <a href="message-resource-files.html">Message resource files</a> ) we covered how to use the Struts 2 head, form, textfield |
| controls and the key attribute. This tutorial will explore using the Struts 2 select, radio, checkbox, and checkboxlist |
| form controls.</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="example-application">Example Application</h2> |
| |
| <p>The example application that supports this tutorial shows how to use Struts 2 form tags so that a user can edit his |
| information. The information that can be edited is encapsulated in an object of class Person. A Person object knows |
| these things: first name, last name, favorite sport, gender, state of residency, is or is not over 21, and car models |
| owned.</p> |
| |
| <p>To enable the user to edit his information that is stored in the Person object, we need to create a form like this one:</p> |
| |
| <p><img src="attachments/att19660802_Screen shot 2010-04-25 at 8.39.59 AM.png" alt="Screen shot 2010-04-25 at 8.39.59 AM.png" /></p> |
| |
| <p>The form allows the user to make changes. After submitting the form, the Struts 2 framework will update the state |
| of the Person object.</p> |
| |
| <p>The first and last names are shown on the form (see edit.jsp) using the Struts 2 textfield tag, which we’ve discussed |
| in previous tutorials.</p> |
| |
| <h2 id="struts-2-select-tag">Struts 2 Select Tag</h2> |
| |
| <p>A user can select one favorite sport from several choices. The example application uses the Struts 2 select tag |
| to provide the list of options for the select box.</p> |
| |
| <p><strong>Struts 2 Select Tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><s:select</span> <span class="na">key=</span><span class="s">"personBean.sport"</span> <span class="na">list=</span><span class="s">"sports"</span> <span class="nt">/></span> |
| </code></pre></div></div> |
| |
| <p>In these form tags, we are using the key attribute as discussed in the <a href="message-resource-files.html">Message resource files</a> |
| tutorial. The key attribute is used by the Struts 2 framework to determine values for the other attributes (e.g. label |
| and value). We are also using a property file associated with the EditAction class to provide the label values based on |
| the key attribute value (see the <a href="message-resource-files.html">Message Resource Files</a> tutorial for information on using |
| Struts 2 property files).</p> |
| |
| <blockquote> |
| <p>Note that there are many attributes for the Struts 2 form tags, most of which mirror the HTML attributes associated |
| with the tags. You can read about all the attributes for a Struts 2 form tag by consulting the Struts 2 documentation.</p> |
| </blockquote> |
| |
| <p>The value of the list attribute of the Struts 2 select tag is used by the framework to determine what method |
| of the action class to call in order to create the option values. In our example application, the list attribute |
| value of “sports” results in the framework calling the getSports method of class EditAction. That method returns |
| a String array containing “football”, “baseball”, and “basketball”. Those values are used to create the option tags |
| inside the select tag.</p> |
| |
| <p>The Struts 2 framework determines which option is preselected by using the key attribute’s value to call a method |
| on the personBean object. Since the key attribute’s value is “personBean.sport”, the framework calls the personBean |
| object’s getSport method. If the value returned by that method matches one of the option values, that option will be |
| marked as “selected”.</p> |
| |
| <p>Here is the HTML that results from using the above Struts 2 select tag.</p> |
| |
| <p><strong>HTML Created By Struts 2 Select Tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><tr></span> |
| <span class="nt"><td</span> <span class="na">class=</span><span class="s">"tdLabel"</span><span class="nt">></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"save_personBean_sport"</span> <span class="na">class=</span><span class="s">"label"</span><span class="nt">></span>Favorite sport:<span class="nt"></label></span> |
| <span class="nt"></td></span> |
| <span class="nt"><td></span> |
| <span class="nt"><select</span> <span class="na">name=</span><span class="s">"personBean.sport"</span> <span class="na">id=</span><span class="s">"save_personBean_sport"</span><span class="nt">></span> |
| <span class="nt"><option</span> <span class="na">value=</span><span class="s">"football"</span><span class="nt">></span>football<span class="nt"></option></span> |
| <span class="nt"><option</span> <span class="na">value=</span><span class="s">"baseball"</span><span class="nt">></span>baseball<span class="nt"></option></span> |
| <span class="nt"><option</span> <span class="na">value=</span><span class="s">"basketball"</span> <span class="na">selected=</span><span class="s">"selected"</span><span class="nt">></span>basketball<span class="nt"></option></span> |
| <span class="nt"></select></span> |
| <span class="nt"></td></span> |
| <span class="nt"></tr></span> |
| </code></pre></div></div> |
| |
| <p>Note the table formatting created by the Struts 2 framework when using the Struts 2 select tag. The CSS classes are |
| defined in style sheets included by the Struts 2 s:head tag. The Struts 2 s:head tag is placed inside the edit.jsp’s |
| head section.</p> |
| |
| <p>Since the personBean’s getSport method returns “baskeball”, the basketball option value is marked as selected.</p> |
| |
| <h2 id="struts-2-radio-tag">Struts 2 Radio Tag</h2> |
| |
| <p>The Struts 2 radio tag—like its standard HTML counterpart—is used to display 2 or more choices, only one of which can |
| be selected by the user. Here is the code for the Struts 2 radio button from the example application.</p> |
| |
| <p><strong>Struts 2 Radio Tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><s:radio</span> <span class="na">key=</span><span class="s">"personBean.gender"</span> <span class="na">list=</span><span class="s">"genders"</span> <span class="nt">/></span> |
| </code></pre></div></div> |
| |
| <p>Again the key attribute’s value determines the value for the label and value attributes. The label’s text is derived |
| from the EditAction.properties file (key personBean.gender). Just like the Struts 2 select tag, the list attribute |
| of the Struts 2 radio tag causes the framework to call the getGenders method of the EditAction class. |
| The Array of String objects returned are used to create the individual radio buttons.</p> |
| |
| <p><strong>HTML Created By Struts 2 Radio Tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><tr></span> |
| <span class="nt"><td</span> <span class="na">class=</span><span class="s">"tdLabel"</span><span class="nt">></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"save_personBean_gender"</span> <span class="na">class=</span><span class="s">"label"</span><span class="nt">></span>Gender:<span class="nt"></label></span> |
| <span class="nt"></td></span> |
| <span class="nt"><td></span> |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">name=</span><span class="s">"personBean.gender"</span> <span class="na">id=</span><span class="s">"save_personBean_gendermale"</span> <span class="na">value=</span><span class="s">"male"</span><span class="nt">/></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"save_personBean_gendermale"</span><span class="nt">></span>male<span class="nt"></label></span> |
| |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">name=</span><span class="s">"personBean.gender"</span> <span class="na">id=</span><span class="s">"save_personBean_genderfemale"</span> <span class="na">value=</span><span class="s">"female"</span><span class="nt">/></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"save_personBean_genderfemale"</span><span class="nt">></span>female<span class="nt"></label></span> |
| |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"radio"</span> <span class="na">name=</span><span class="s">"personBean.gender"</span> <span class="na">id=</span><span class="s">"save_personBean_gendernot sure"</span> <span class="na">checked=</span><span class="s">"checked"</span> <span class="na">value=</span><span class="s">"not sure"</span><span class="nt">/></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"save_personBean_gendernot sure"</span><span class="nt">></span>not sure<span class="nt"></label></span> |
| <span class="nt"></td></span> |
| <span class="nt"></tr></span> |
| </code></pre></div></div> |
| |
| <p>Also just like the Struts 2 select tag the result returned by calling the personBean object’s getGender method is used |
| to determine which of the radio buttons is checked.</p> |
| |
| <h2 id="struts-2-select-tag---object-backed">Struts 2 Select Tag - Object Backed</h2> |
| |
| <p>You may need to create a Struts 2 select tag where the options displayed to the user each have their own value that is |
| different from what is displayed. In the example application, the user’s residency is stored as a two-letter |
| abbreviation (e.g. KS), but the form select box should display the full state name (e.g. Kansas). To create such |
| a select box in Struts 2, you would use this code</p> |
| |
| <p><strong>Struts 2 Select Tag Object Backed</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><s:select</span> <span class="na">key=</span><span class="s">"personBean.residency"</span> <span class="na">list=</span><span class="s">"states"</span> <span class="na">listKey=</span><span class="s">"stateAbbr"</span> <span class="na">listValue=</span><span class="s">"stateName"</span> <span class="nt">/></span> |
| </code></pre></div></div> |
| |
| <p>The list value tells the framework to call the getStates method of the EditAction class. That method returns |
| an ArrayList of State objects. Each State object has getStateAbbr and getStateName methods.</p> |
| |
| <p>The listKey attribute tells the framework to use the value returned by calling the getStateAbbr method as the value for |
| the value attribute of the HTML option tag and the value returned by calling the getStateName method as the value |
| displayed to the user. So the above Struts 2 select tag code results in this HTML.</p> |
| |
| <p><strong>HTML Created By Struts 2 Select Tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><tr></span> |
| <span class="nt"><td</span> <span class="na">class=</span><span class="s">"tdLabel"</span><span class="nt">></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"save_personBean_residency"</span> <span class="na">class=</span><span class="s">"label"</span><span class="nt">></span>State resident:<span class="nt"></label></span> |
| <span class="nt"></td></span> |
| <span class="nt"><td></span> |
| <span class="nt"><select</span> <span class="na">name=</span><span class="s">"personBean.residency"</span> <span class="na">id=</span><span class="s">"save_personBean_residency"</span><span class="nt">></span> |
| <span class="nt"><option</span> <span class="na">value=</span><span class="s">"AZ"</span><span class="nt">></span>Arizona<span class="nt"></option></span> |
| <span class="nt"><option</span> <span class="na">value=</span><span class="s">"CA"</span><span class="nt">></span>California<span class="nt"></option></span> |
| <span class="nt"><option</span> <span class="na">value=</span><span class="s">"FL"</span><span class="nt">></span>Florida<span class="nt"></option></span> |
| <span class="nt"><option</span> <span class="na">value=</span><span class="s">"KS"</span> <span class="na">selected=</span><span class="s">"selected"</span><span class="nt">></span>Kansas<span class="nt"></option></span> |
| <span class="nt"><option</span> <span class="na">value=</span><span class="s">"NY"</span><span class="nt">></span>New York<span class="nt"></option></span> |
| <span class="nt"></select></span> |
| <span class="nt"></td></span> |
| <span class="nt"></tr></span> |
| </code></pre></div></div> |
| |
| <p>The value returned by calling the personBean object’s getResidency method determines which of the select tag’s option |
| tags is marked as selected. In our example, since getResidency returns “KS”, the option tag whose value attribute |
| equals “KS” is marked as selected.</p> |
| |
| <h2 id="struts-2-checkbox-tag">Struts 2 Checkbox Tag</h2> |
| |
| <p>The Struts 2 checkbox tag is used to create the HTML input type equals checkbox tag. The value for the key attribute |
| tells the framework what method to call to determine if the checkbox is checked or not checked. The method called should |
| return a Boolean value (true or false). A return value of true will cause the checkbox to be checked and false |
| the checkbox will not be checked.</p> |
| |
| <p><strong>Struts 2 Checkbox Tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><s:checkbox</span> <span class="na">key=</span><span class="s">"personBean.over21"</span> <span class="nt">/></span> |
| </code></pre></div></div> |
| |
| <p>Since the method isOver21 returns true, the checkbox is checked.</p> |
| |
| <p><strong>HTML Created By Struts 2 Checkbox Tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><tr></span> |
| <span class="nt"><td</span> <span class="na">valign=</span><span class="s">"top"</span> <span class="na">align=</span><span class="s">"right"</span><span class="nt">></td></span> |
| <span class="nt"><td</span> <span class="na">valign=</span><span class="s">"top"</span> <span class="na">align=</span><span class="s">"left"</span><span class="nt">></span> |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"personBean.over21"</span> <span class="na">value=</span><span class="s">"true"</span> <span class="na">checked=</span><span class="s">"checked"</span> <span class="na">id=</span><span class="s">"save_personBean_over21"</span><span class="nt">/></span> |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">id=</span><span class="s">"__checkbox_save_personBean_over21"</span> <span class="na">name=</span><span class="s">"__checkbox_personBean.over21"</span> <span class="na">value=</span><span class="s">"true"</span> <span class="nt">/></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"save_personBean_over21"</span> <span class="na">class=</span><span class="s">"checkboxLabel"</span><span class="nt">></span>21 or older<span class="nt"></label></span> |
| <span class="nt"></td></span> |
| <span class="nt"></tr></span> |
| </code></pre></div></div> |
| |
| <p>When the form is submitted and the checkbox is not checked, no value will be posted for the checkbox (this is how HTML |
| forms work). Since the Struts 2 framework will need to update the value of the personBean’s over21 instance field |
| to false—given that the check box was not checked—the framework needs a way to determine if the checkbox was not checked |
| after form submission.</p> |
| |
| <p>If you examine the HTML code created by the Struts 2 checkbox tag, you’ll see that it created a hidden field associated |
| with the personBean.over21 checkbox. When the Struts 2 framework intercepts the submission of this form it will use this |
| hidden form field to check if the associated checkbox field exists in the posted form data. If that checkbox field |
| doesn’t exist then the Struts 2 framework will know to update the value of the personBean object’s over21 instance |
| variable to false.</p> |
| |
| <h2 id="struts-2-checkboxlist-tag">Struts 2 checkboxlist Tag</h2> |
| |
| <p>The Struts 2 framework provides a unique form field control that creates a series of associated check boxes, one or more |
| of which can be checked. In the example application, the Person class has an Array of Strings, which is used to store |
| car models owned by a person.</p> |
| |
| <p>Using the Struts 2 checkbox tag, we can create a series of checkboxes, one for each possible car model the user may own. |
| The value of each String in the personBean’s carModels Array will determine which checkboxes are checked.</p> |
| |
| <p><strong>Struts 2 Checkboxlist Tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><s:checkboxlist</span> <span class="na">key=</span><span class="s">"personBean.carModels"</span> <span class="na">list=</span><span class="s">"carModelsAvailable"</span> <span class="nt">/></span> |
| </code></pre></div></div> |
| |
| <p>The list attributes value in the checkboxlist tag tells the Struts 2 framework which method to call to get the possible |
| car models. In the example application, the framework will call the EditAction class’s getCarModelsAvailable method. |
| That method returns an Array of Strings. For each element of the Array, the Struts 2 framework creates a checkbox |
| (including the associated hidden field described above).</p> |
| |
| <p>The key attribute value in the checkboxlist tag tells the Struts 2 framework which method to call on the personBean |
| object to determine which checkboxes should be checked. In the example application, the framework will call |
| the personBean object’s getCarModels method. The getCarModels method returns an Array of Strings. For each String value |
| in that Array that matches a String value in the Array returned by the EditAction class’s getCarModelsAvailable, |
| the checkbox will be checked.</p> |
| |
| <p><strong>HTML Created By Struts 2 Checkboxlist Tag</strong></p> |
| |
| <div class="language-html highlighter-rouge"><div class="highlight"><pre class="highlight"><code><span class="nt"><tr></span> |
| <span class="nt"><td</span> <span class="na">class=</span><span class="s">"tdLabel"</span><span class="nt">></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"save_personBean_carModels"</span> <span class="na">class=</span><span class="s">"label"</span><span class="nt">></span>Car models owned:<span class="nt"></label></span> |
| <span class="nt"></td></span> |
| <span class="nt"><td></span> |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"personBean.carModels"</span> <span class="na">value=</span><span class="s">"Ford"</span> <span class="na">id=</span><span class="s">"personBean.carModels-1"</span> <span class="na">checked=</span><span class="s">"checked"</span><span class="nt">/></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"personBean.carModels-1"</span> <span class="na">class=</span><span class="s">"checkboxLabel"</span><span class="nt">></span>Ford<span class="nt"></label></span> |
| |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"personBean.carModels"</span> <span class="na">value=</span><span class="s">"Chrysler"</span> <span class="na">id=</span><span class="s">"personBean.carModels-2"</span><span class="nt">/></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"personBean.carModels-2"</span> <span class="na">class=</span><span class="s">"checkboxLabel"</span><span class="nt">></span>Chrysler<span class="nt"></label></span> |
| |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"personBean.carModels"</span> <span class="na">value=</span><span class="s">"Toyota"</span> <span class="na">id=</span><span class="s">"personBean.carModels-3"</span><span class="nt">/></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"personBean.carModels-3"</span> <span class="na">class=</span><span class="s">"checkboxLabel"</span><span class="nt">></span>Toyota<span class="nt"></label></span> |
| |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"checkbox"</span> <span class="na">name=</span><span class="s">"personBean.carModels"</span> <span class="na">value=</span><span class="s">"Nissan"</span> <span class="na">id=</span><span class="s">"personBean.carModels-4"</span> <span class="na">checked=</span><span class="s">"checked"</span><span class="nt">/></span> |
| <span class="nt"><label</span> <span class="na">for=</span><span class="s">"personBean.carModels-4"</span> <span class="na">class=</span><span class="s">"checkboxLabel"</span><span class="nt">></span>Nissan<span class="nt"></label></span> |
| <span class="nt"><input</span> <span class="na">type=</span><span class="s">"hidden"</span> <span class="na">id=</span><span class="s">"__multiselect_save_personBean_carModels"</span> <span class="na">name=</span><span class="s">"__multiselect_personBean.carModels"</span> <span class="na">value=</span><span class="s">""</span> <span class="nt">/></span> |
| <span class="nt"></td></span> |
| <span class="nt"></tr></span> |
| </code></pre></div></div> |
| |
| <h2 id="summary">Summary</h2> |
| |
| <p>There are several other Struts 2 form controls you should explore. If you need more information about the Struts 2 form |
| tags consult the Struts 2 documentation at <a href="http://struts.apache.org">http://struts.apache.org</a>.</p> |
| |
| <table> |
| <tbody> |
| <tr> |
| <td>Return to <a href="debugging-struts.html">Debugging Struts</a></td> |
| <td>or</td> |
| <td>onward to <a href="form-validation-using-xml.html">Form validation using XML</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> |