Author: lukaszlenart Date: Mon Apr 3 10:51:24 2017 New Revision: 1009674 Log: Updates production
Modified: websites/production/struts/content/getting-started/annotations.html websites/production/struts/content/getting-started/exclude-parameters.html websites/production/struts/content/getting-started/http-session.html websites/production/struts/content/getting-started/introducing-interceptors.html websites/production/struts/content/getting-started/preperable-interface.html websites/production/struts/content/getting-started/unit-testing.html Modified: websites/production/struts/content/getting-started/annotations.html ============================================================================== --- websites/production/struts/content/getting-started/annotations.html (original) +++ websites/production/struts/content/getting-started/annotations.html Mon Apr 3 10:51:24 2017 @@ -125,98 +125,44 @@ <p>The example code for this tutorial, annotations, is available for checkout at <a href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a></p> -<blockquote> +<p><strong>Introduction</strong></p> -</blockquote> +<p>In our previous tutorials weâve been using an XML file (<code class="highlighter-rouge">struts.xml</code>) to configure our applications. The XML file wires up the action names (register), with ActionSupport classes (<code class="highlighter-rouge">RegisterAction.java</code>), and with the result to render back to the browser (<code class="highlighter-rouge">register.jsp</code>). Struts 2 provides an alternative to using XML to configure your application by using standard naming conventions and annotations for your action names, <code class="highlighter-rouge">ActionSupport</code> classes, and results.</p> -<p>#####Introduction#####</p> +<p>This tutorial assumes you understand how to apply annotations to Java classes and methods. If youâre not familiar with annotations, consult the <a href="http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html">Java online tutorial</a>.</p> -<p>In our previous tutorials weâve been using an XML file (struts.xml) to configure our applications. The XML file wires up the action names (register), with ActionSupport classes (RegisterAction.java), and with the result to render back to the browser (register.jsp). Struts 2 provides an alternative to using XML to configure your application by using standard naming conventions and annotations for your action names, ActionSupport classes, and results.</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> -<blockquote> - -</blockquote> - -<blockquote> - -</blockquote> - -<blockquote> - <p>This tutorial assumes you understand how to apply annotations to Java classes and methods. If youâre not familiar with annotations, consult the <a href="http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html">Java online tutorial</a>^[http://download.oracle.com/javase/tutorial/java/javaOO/annotations.html].</p> -</blockquote> - -<blockquote> - -</blockquote> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> - -<table> - <tbody> - <tr> - <td>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a>^[http://struts.apache.org/mail.html] 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.</td> - </tr> - </tbody> -</table> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> - -<p>#####Struts 2 Convention Plugin#####</p> +<p><strong>Struts 2 Convention Plugin</strong></p> <p>Struts 2 enables the use of standard naming conventions and annotations when you include the Convention plugin in your applicationâs class path. If youâre using Maven youâll need to add a dependency:</p> <p><strong>Convention Plugin Dependency</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code><dependency> - <groupId>org.apache.struts</groupId> - <artifactId>struts2-convention-plugin</artifactId> - <version>2.2.1</version> -</dependency> - - +<div class="highlighter-rouge"><pre class="highlight"><code><span class="nt"><dependency></span> + <span class="nt"><groupId></span>org.apache.struts<span class="nt"></groupId></span> + <span class="nt"><artifactId></span>struts2-convention-plugin<span class="nt"></artifactId></span> + <span class="nt"><version></span>2.5.10.1<span class="nt"></version></span> +<span class="nt"></dependency></span> </code></pre> </div> - <p>If youâre using Ant then copy the struts2-convention-plugin jar file from the Struts 2 download to your WEB-INF/lib folder.</p> -<table> - <tbody> - <tr> - <td>The convention plugin provide several different ways you can configure your Struts 2 application without using XML. Consult the <em>Convention Plugin</em> documentation for complete details. This tutorial only examines one simple way of following the conventions provided by the Convention plugin.</td> - </tr> - </tbody> -</table> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> - -<p>When you run the example application youâll see on the index.jsp page a link to Get your hello. This URL for the link is hello.action. When you click on this link, the execute method of class HelloAction.java (which is a Struts 2 ActionSupport class) is run. The view page rendered back to the browser after the execute method returns success is hello-success.jsp.</p> +<p>The convention plugin provide several different ways you can configure your Struts 2 application without using XML. Consult the <a href="//struts.apache.org/docs/convention-plugin.html">Convention Plugin</a> documentation for complete details. This tutorial only examines one simple way of following the conventions provided by the Convention plugin.</p> -<p>None of the above is wired up using XML but rather happens because the application follows the standard naming conventions expected by the Convention plugin. The first convention is that the ActionSupport class, HelloAction.java, is in package org.apache.struts.struts2annotations.action. One of the Convention pluginâs defaults is to look for ActionSupport classes that are in package structure that ends in action. The next convention the application follows is that HelloAction.java extends the ActionSupport class and defines an execute method. The link is hello.action. When the Struts 2 filter sees a request for hello.action it will map that request to the HelloAction classâs execute method due to the Convention plugin being used.</p> +<p>When you run the example application youâll see on the <code class="highlighter-rouge">index.jsp</code> page a link to Get your hello. This URL for the link is hello.action. When you click on this link, the execute method of class <code class="highlighter-rouge">HelloAction.java</code> (which is a Struts 2 <code class="highlighter-rouge">ActionSupport</code> class) is run. The view page rendered back to the browser after the execute method returns success is <code class="highlighter-rouge">hello-success.jsp</code>.</p> -<p>So a link of hello.action causes the execute method of class HelloAction to be run. That method returns âsuccess.â Because the application is using the Convention plugin, Struts 2 will render back to the browser a view page named hello-success.jsp that is located in WEB-INF/content (by default the Convention plugin expects all view pages to be in this location). If the execute method returns âinputâ or âerrorâ then the view page rendered would have been hello-input.jsp or hello-error.jsp.</p> +<p>None of the above is wired up using XML but rather happens because the application follows the standard naming conventions expected by the Convention plugin. The first convention is that the ActionSupport class, <code class="highlighter-rouge">HelloAction.java</code>, is in package org.apache.struts.struts2annotations.action. One of the Convention pluginâs defaults is to look for ActionSupport classes that are in package structure that ends in action. The next convention the application follows is that HelloAction.java extends the ActionSupport class and defines an execute method. The link is hello.action. When the Struts 2 filter sees a request for hello.action it will map that request to the HelloAction classâs execute method due to the Convention plugin being used.</p> -<p>#####Struts 2 Configuration Plugin#####</p> +<p>So a link of hello.action causes the execute method of class HelloAction to be run. That method returns âsuccess.â Because the application is using the Convention plugin, Struts 2 will render back to the browser a view page named <code class="highlighter-rouge">hello-success.jsp</code> that is located in WEB-INF/content (by default the Convention plugin expects all view pages to be in this location). If the execute method returns âinputâ or âerrorâ then the view page rendered would have been <code class="highlighter-rouge">hello-input.jsp</code> or <code class="highlighter-rouge">hello-error.jsp</code>.</p> -<p>In a <a href="#PAGE_16941310">previous tutorial</a> we reviewed how to use the Struts 2 Configuration plugin to view the details of how Struts 2 has configured your application. When using the Convention plugin, itâs very handy to also use the Configuration plugin during development. On the example applicationâs home page is a link to the applicationâs configuration. Click on that link and then the hello link on the left menu (under Actions in default). Youâll see the configuration for the hello action including itâs Action class, result, and view page.</p> +<p><strong>Struts 2 Configuration Plugin</strong></p> + +<p>In a <a href="debugging-struts.html">previous tutorial</a> we reviewed how to use the Struts 2 Configuration plugin to view the details of how Struts 2 has configured your application. When using the Convention plugin, itâs very handy to also use the Configuration plugin during development. On the example applicationâs home page is a link to the applicationâs configuration. Click on that link and then the hello link on the left menu (under Actions in default). Youâll see the configuration for the hello action including itâs Action class, result, and view page.</p> <p><img src="attachments/att24346643_Screen shot 2010-10-24 at 10.51.45 AM.png" alt="Screen shot 2010-10-24 at 10.51.45 AM.png" /></p> -<p>#####Annotations#####</p> +<p><strong>Annotations</strong></p> <p>If you want to go beyond the simple naming conventions provided by the Convention plugin, you can use the Struts 2 annotations also provided by the plugin. For example, a common work-flow for a Struts 2 application is to first execute the ActionSupport classâs input method to setup form field default values and then to run the execute method of the same ActionSupport class when the form is submitted (to validate and save the userâs input).</p> @@ -224,299 +170,47 @@ <p><strong>Action Annotation</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code>@Action("register-input") -public String input() throws Exception { - - logger.info("In input method of class Register"); - - return INPUT; -} - +<div class="highlighter-rouge"><pre class="highlight"><code><span class="nd">@Action</span><span class="o">(</span><span class="s">"register-input"</span><span class="o">)</span> +<span class="kd">public</span> <span class="n">String</span> <span class="nf">input</span><span class="p">(</span><span class="o">)</span> <span class="kd">throws</span> <span class="n">Exception</span> <span class="o">{</span> + <span class="n">logger</span><span class="o">.</span><span class="na">info</span><span class="o">(</span><span class="s">"In input method of class Register"</span><span class="o">);</span> + <span class="k">return</span> <span class="n">INPUT</span><span class="o">;</span> +<span class="o">}</span> </code></pre> </div> -<p>The Action annotation tells Struts 2 to execute the annotated method when the action link value equals the Action annotationâs value (âregister-inputâ). So a link of register-input.action will call the input method of class RegisterAction. On the example applicationâs home page is a link to Register for the drawing with a URL of register-input.action.</p> +<p>The Action annotation tells Struts 2 to execute the annotated method when the action link value equals the Action annotationâs value (âregister-inputâ). So a link of register-input.action will call the input method of class <code class="highlighter-rouge">RegisterAction</code>. On the example applicationâs home page is a link to Register for the drawing with a URL of register-input.action.</p> -<p>The input method above returns âinputâ. By the standards of the Convention plugin, the view page rendered will be register-input.jsp (from WEB-INF/content). On that view page is a Struts 2 form tag with an action attribute value of register. When submitting the form, the execute method of class RegisterAction will be run. Since the execute method returns success, the view page rendered is register-success.jsp.</p> +<p>The input method above returns âinputâ. By the standards of the Convention plugin, the view page rendered will be register-input.jsp (from WEB-INF/content). On that view page is a Struts 2 form tag with an action attribute value of register. When submitting the form, the execute method of class RegisterAction will be run. Since the execute method returns success, the view page rendered is <code class="highlighter-rouge">register-success.jsp</code>.</p> -<p>#####Struts 2 Configuration Values#####</p> +<p><strong>Struts 2 Configuration Values</strong></p> <p>In previous examples, we included in struts.xml values for some of the Struts 2 configuration parameters.</p> <p><strong>struts.xml parameter configuration</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code><constant name="struts.devMode" value="true" /> - - +<div class="highlighter-rouge"><pre class="highlight"><code><span class="nt"><constant</span> <span class="na">name=</span><span class="s">"struts.devMode"</span> <span class="na">value=</span><span class="s">"true"</span> <span class="nt">/></span> </code></pre> </div> <p>When we donât use a struts.xml file, we can set the value of these Struts 2 parameters by using filter parameters in web.xml:</p> -<p><strong>Struts 2 Parameter Configurate web.xml</strong></p> - -<div class="highlighter-rouge"><pre class="highlight"><code><filter> - <filter-name>struts2</filter-name> - <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> - <init-param> - <param-name>struts.devMode</param-name> - <param-value>true</param-value> - </init-param> -</filter> - - -</code></pre> -</div> - -<p>#####Summary#####</p> - -<p>Weâve just scratched the surface of what the Struts 2 convention plugin provides to reduce or eliminate the need to use an XML file to configure your Struts 2 application. The Struts 2 Convention plugin provides ways to map multiple actions to the same method, map results to different view pages, map errors to view pages, and much more. Be sure to read through the <em>Convention Plugin</em> documentation for alternative ways to configure your Struts 2 application.</p> - -<p>##Struts 2 Form Tags## {#PAGE_19300595}</p> - -<p>The example code for this tutorial, form_tags, can be checked out from <a href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a>.</p> - -<blockquote> - -</blockquote> - -<p>#####Introduction#####</p> - -<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 (<em>Processing Forms</em> , <em>Form Validation</em> , and <em>Message Resource Files</em> ) 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> - -<table> - <tbody> - <tr> - <td>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a>^[http://struts.apache.org/mail.html] 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.</td> - </tr> - </tbody> -</table> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> - -<p>#####Example Application#####</p> - -<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> - -<p>#####Struts 2 Select Tag#####</p> - -<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="highlighter-rouge"><pre class="highlight"><code><s:select key="personBean.sport" list="sports" /> - - -</code></pre> -</div> - -<p>In these form tags, we are using the key attribute as discussed in the <em>Message Resource Files</em> 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 <em>Message Resource Files</em> tutorial for information on using Struts 2 property files).</p> - -<blockquote> - -</blockquote> - -<blockquote> - -</blockquote> - -<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> - -<blockquote> - -</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="highlighter-rouge"><pre class="highlight"><code><tr> -<td class="tdLabel"> -<label for="save_personBean_sport" class="label">Favorite sport:</label> -</td> -<td> -<select name="personBean.sport" id="save_personBean_sport"> - <option value="football">football</option> - <option value="baseball">baseball</option> - <option value="basketball" selected="selected">basketball</option> -</select> -</td> -</tr> - - -</code></pre> -</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> - -<p>#####Struts 2 Radio Tag#####</p> - -<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="highlighter-rouge"><pre class="highlight"><code><s:radio key="personBean.gender" list="genders" /> - - -</code></pre> -</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="highlighter-rouge"><pre class="highlight"><code><tr> -<td class="tdLabel"> -<label for="save_personBean_gender" class="label">Gender:</label></td> -<td> -<input type="radio" name="personBean.gender" id="save_personBean_gendermale" value="male"/><label for="save_personBean_gendermale">male</label> -<input type="radio" name="personBean.gender" id="save_personBean_genderfemale" value="female"/><label for="save_personBean_genderfemale">female</label> -<input type="radio" name="personBean.gender" id="save_personBean_gendernot sure" checked="checked" value="not sure"/><label for="save_personBean_gendernot sure">not sure</label> -</td> -</tr> - - -</code></pre> -</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> - -<p>#####Struts 2 Select Tag - Object Backed#####</p> - -<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 then 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="highlighter-rouge"><pre class="highlight"><code><s:select key="personBean.residency" list="states" listKey="stateAbbr" listValue="stateName" /> - - -</code></pre> -</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="highlighter-rouge"><pre class="highlight"><code><tr> -<td class="tdLabel"> -<label for="save_personBean_residency" class="label">State resident:</label></td> -<td> -<select name="personBean.residency" id="save_personBean_residency"> - <option value="AZ">Arizona</option> - <option value="CA">California</option> - <option value="FL">Florida</option> - <option value="KS" selected="selected">Kansas</option> - <option value="NY">New York</option> -</select> -</td> -</tr> - - -</code></pre> -</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> - -<p>#####Struts 2 Checkbox Tag#####</p> - -<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="highlighter-rouge"><pre class="highlight"><code><s:checkbox key="personBean.over21" /> - - -</code></pre> -</div> - -<p>Since the method getOver21 returns true, the checkbox is checked.</p> - -<p><strong>HTML Created By Struts 2 Checkbox Tag</strong></p> - -<div class="highlighter-rouge"><pre class="highlight"><code><tr> -<td valign="top" align="right"> -</td> -<td valign="top" align="left"> -<input type="checkbox" name="personBean.over21" value="true" checked="checked" id="save_personBean_over21"/> -<input type="hidden" id="__checkbox_save_personBean_over21" name="__checkbox_personBean.over21" value="true" /> <label for="save_personBean_over21" class="checkboxLabel">21 or older</label> -</td> -</tr> - - -</code></pre> -</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> - -<p>#####Struts 2 checkboxlist Tag#####</p> - -<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="highlighter-rouge"><pre class="highlight"><code><s:checkboxlist key="personBean.carModels" list="carModelsAvailable" /> - - -</code></pre> -</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="highlighter-rouge"><pre class="highlight"><code><tr> -<td class="tdLabel"> -<label for="save_personBean_carModels" class="label">Car models owned:</label></td> -<td> -<input type="checkbox" name="personBean.carModels" value="Ford" id="personBean.carModels-1" checked="checked"/> -<label for="personBean.carModels-1" class="checkboxLabel">Ford</label> -<input type="checkbox" name="personBean.carModels" value="Chrysler" id="personBean.carModels-2"/> -<label for="personBean.carModels-2" class="checkboxLabel">Chrysler</label> -<input type="checkbox" name="personBean.carModels" value="Toyota" id="personBean.carModels-3"/> -<label for="personBean.carModels-3" class="checkboxLabel">Toyota</label> -<input type="checkbox" name="personBean.carModels" value="Nissan" id="personBean.carModels-4" checked="checked"/> -<label for="personBean.carModels-4" class="checkboxLabel">Nissan</label> -<input type="hidden" id="__multiselect_save_personBean_carModels" name="__multiselect_personBean.carModels" value="" /> -</td> -</tr> - +<p><strong>Struts 2 Parameter Configuration web.xml</strong></p> +<div class="highlighter-rouge"><pre class="highlight"><code><span class="nt"><filter></span> + <span class="nt"><filter-name></span>struts2<span class="nt"></filter-name></span> + <span class="nt"><filter-class></span>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter<span class="nt"></filter-class></span> + <span class="nt"><init-param></span> + <span class="nt"><param-name></span>struts.devMode<span class="nt"></param-name></span> + <span class="nt"><param-value></span>true<span class="nt"></param-value></span> + <span class="nt"></init-param></span> +<span class="nt"></filter></span> </code></pre> </div> -<p>Summary - 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> +<p><strong>Summary</strong></p> +<p>Weâve just scratched the surface of what the Struts 2 convention plugin provides to reduce or eliminate the need to use an XML file to configure your Struts 2 application. The Struts 2 Convention plugin provides ways to map multiple actions to the same method, map results to different view pages, map errors to view pages, and much more. Be sure to read through the <a href="//struts.apache.org/docs/convention-plugin.html">Convention Plugin</a> documentation for alternative ways to configure your Struts 2 application.</p> </section> </article> Modified: websites/production/struts/content/getting-started/exclude-parameters.html ============================================================================== --- websites/production/struts/content/getting-started/exclude-parameters.html (original) +++ websites/production/struts/content/getting-started/exclude-parameters.html Mon Apr 3 10:51:24 2017 @@ -125,26 +125,13 @@ <p>The example code for this tutorial, exclude_parameters, is available at <a href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a>.</p> -<p>#####Introduction#####</p> +<p><strong>Introduction</strong></p> -<p>When <a href="http://struts.apache.org/2.3.8/docs/strutsproperties.html">Struts development mode is set to true</a>^[http://struts.apache.org/2.3.8/docs/strutsproperties.html] (also see <a href="#PAGE_16941310">Debugging Struts</a>) the framework writes many informative messages to the log file. These messages include ones that indicate whether or not a specific parameter will be handled by the parameter interceptor and made available to the Action class. These log messages can be helpful in clearly identifying parameters that you do not want the parameter interceptor to process for security or other reasons. This article discusses how to exclude parameters from being handled by the parameter interceptor.</p> +<p>When <a href="//struts.apache.org/docs/strutsproperties.html">Struts development mode is set to true</a> (also see <a href="debugging-struts.html">Debugging Struts</a>) the framework writes many informative messages to the log file. These messages include ones that indicate whether or not a specific parameter will be handled by the parameter interceptor and made available to the Action class. These log messages can be helpful in clearly identifying parameters that you do not want the parameter interceptor to process for security or other reasons. This article discusses how to exclude parameters from being handled by the parameter interceptor.</p> -<table> - <tbody> - <tr> - <td>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a>^[http://struts.apache.org/mail.html] 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.</td> - </tr> - </tbody> -</table> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> +<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> -<p>#####Processing Request Parameters#####</p> +<p><strong>Processing Request Parameters</strong></p> <p>Most request parameters are by default processed by the parameter interceptor and Struts 2 will attempt to modify the state of those Action class fields that match up to a parameter name by calling a corresponding public set method. For example if the request includes a parameter of lastName with a value of Phillips, Struts 2 will try to call a public method with a signature of setLastName(String lastName). However, there may be request parameters that you do not want Struts 2 to try to set the value of in the Action class.</p> @@ -152,20 +139,18 @@ <p><strong>Struts 2 Form Tags</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code><s:form action="save" method="post"> -<s:textfield key="personBean.firstName" /> -<s:textfield key="personBean.lastName" /> -<s:textfield key="personBean.email" /> -<s:textfield key="personBean.phoneNumber" /> -<s:select key="personBean.sport" list="sports" /> -<s:radio key="personBean.gender" list="genders" /> -<s:select key="personBean.residency" list="states" listKey="stateAbbr" listValue="stateName" /> -<s:checkbox key="personBean.over21" /> -<s:checkboxlist key="personBean.carModels" list="carModelsAvailable" /> -<s:submit key="submit" /> -</s:form> - - +<div class="highlighter-rouge"><pre class="highlight"><code><span class="nt"><s:form</span> <span class="na">action=</span><span class="s">"save"</span> <span class="na">method=</span><span class="s">"post"</span><span class="nt">></span> + <span class="nt"><s:textfield</span> <span class="na">key=</span><span class="s">"personBean.firstName"</span> <span class="nt">/></span> + <span class="nt"><s:textfield</span> <span class="na">key=</span><span class="s">"personBean.lastName"</span> <span class="nt">/></span> + <span class="nt"><s:textfield</span> <span class="na">key=</span><span class="s">"personBean.email"</span> <span class="nt">/></span> + <span class="nt"><s:textfield</span> <span class="na">key=</span><span class="s">"personBean.phoneNumber"</span> <span class="nt">/></span> + <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> + <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> + <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> + <span class="nt"><s:checkbox</span> <span class="na">key=</span><span class="s">"personBean.over21"</span> <span class="nt">/></span> + <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> + <span class="nt"><s:submit</span> <span class="na">key=</span><span class="s">"submit"</span> <span class="nt">/></span> +<span class="nt"></s:form></span> </code></pre> </div> @@ -180,55 +165,44 @@ WARNING: Parameter [submit] is not on th Dec 31, 2012 3:43:53 PM com.opensymphony.xwork2.util.logging.commons.CommonsLogger error SEVERE: Developer Notification (set struts.devMode to false to disable this message): Unexpected Exception caught setting 'submit' on 'class org.apache.struts.edit.action.EditAction: Error setting expression 'submit' with value ['Save Changes', ] - - </code></pre> </div> -<p>#####Excluding Request Parameters From Struts 2 Processing#####</p> +<p><strong>Excluding Request Parameters From Struts 2 Processing</strong></p> -<p>If youâre not familiar with setting up a custom interceptor stack for your Struts 2 application review <a href="#PAGE_24187261">Introducing Interceptors</a>.</p> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> +<p>If youâre not familiar with setting up a custom interceptor stack for your Struts 2 application review <a href="introducing-interceptors.html">Introducing Interceptors</a>.</p> <p>To exclude specific parameters from being processed by the Struts 2 framework you need to add those parameter names to the list of excluded parameters. One way to do this is by adding those parameter names to the collection of excludedParams for the Parameters interceptor. You can do this by modifying the Parameters interceptor in setting up the stack of interceptors used by your Struts 2 application. For example:</p> <p><strong>Setup Interceptor Stack To Exclude submit Parameter</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code><interceptors> - <interceptor-stack name="appDefault"> - <interceptor-ref name="defaultStack"> - <param name="exception.logEnabled">true</param> - <param name="exception.logLevel">ERROR</param> - <param name="params.excludeParams">dojo..*,^struts..*,^session..*,^request..*,^application..*,^servlet(Request|Response)..*,parameters...*,submit</param> - </interceptor-ref> - </interceptor-stack> -</interceptors> +<div class="highlighter-rouge"><pre class="highlight"><code><span class="nt"><interceptors></span> + <span class="nt"><interceptor-stack</span> <span class="na">name=</span><span class="s">"appDefault"</span><span class="nt">></span> + <span class="nt"><interceptor-ref</span> <span class="na">name=</span><span class="s">"defaultStack"</span><span class="nt">></span> + <span class="nt"><param</span> <span class="na">name=</span><span class="s">"exception.logEnabled"</span><span class="nt">></span>true<span class="nt"></param></span> + <span class="nt"><param</span> <span class="na">name=</span><span class="s">"exception.logLevel"</span><span class="nt">></span>ERROR<span class="nt"></param></span> + <span class="nt"><param</span> <span class="na">name=</span><span class="s">"params.excludeParams"</span><span class="nt">></span>dojo..*,^struts..*,^session..*,^request..*,^application..*,^servlet(Request|Response)..*,parameters...*,submit<span class="nt"></param></span> + <span class="nt"></interceptor-ref></span> + <span class="nt"></interceptor-stack></span> +<span class="nt"></interceptors></span> -<default-interceptor-ref name="appDefault" /> - - +<span class="nt"><default-interceptor-ref</span> <span class="na">name=</span><span class="s">"appDefault"</span> <span class="nt">/></span> </code></pre> </div> -<p>The value of node <param name=âparams.excludeParamsâ> is a comma-delimited list of regular expressions or simple Strings that identify request parameters that should NOT be processed by the Parameters interceptor. To exclude the submit parameter (which is the name of the submit button in the form code above), I just added submit to the list.</p> +<p>The value of node <code class="highlighter-rouge"><param name="params.excludeParams"></code> is a comma-delimited list of regular expressions or simple Strings that identify request parameters that should NOT be processed by the Parameters interceptor. To exclude the submit parameter (which is the name of the submit button in the form code above), I just added submit to the list.</p> -<p>See the <a href="http://struts.apache.org/2.3.8/docs/struts-defaultxml.html">Basic Stack of Interceptors described here</a>^[http://struts.apache.org/2.3.8/docs/struts-defaultxml.html] to view the initial set of parameter names/regular expressions to exclude. Be sure to copy over the list of parameters already being excluded and then add your own parameters to the end separated by commas.</p> +<p>See the <a href="//struts.apache.org/docs/struts-defaultxml.html">Basic Stack of Interceptors described here</a> to view the initial set of parameter names/regular expressions to exclude. Be sure to copy over the list of parameters already being excluded and then add your own parameters to the end separated by commas.</p> -<p>#####Example Application#####</p> +<p><strong>Example Application</strong></p> -<p>Download the example application, <a href="http://code.google.com/p/struts2-examples/downloads/list">Exclude_Params_Struts2_Mvn</a>^[http://code.google.com/p/struts2-examples/downloads/list] that demonstrates excluding a request parameter. The download is a zipped Maven project. You should be able to unzip it and import the project into any Maven-aware Java IDE. See the projectâs README.txt file for how to build and run the application.</p> +<p>Download the example application, <a href="http://code.google.com/p/struts2-examples/downloads/list">Exclude_Params_Struts2_Mvn</a> that demonstrates excluding a request parameter. The download is a zipped Maven project. You should be able to unzip it and import the project into any Maven-aware Java IDE. See the projectâs README.txt file for how to build and run the application.</p> <p>To see the log messages written when not excluding the submit parameter remove the â,submitâ from the list of excluded parameter values in the struts.xml file. Then rebuild and redeploy the application and view the console when running the application.</p> -<p>#####Summary#####</p> +<p><strong>Summary</strong></p> -<p>Itâs a nice feature of the Struts 2 framework that it logs during development which request parameters will and will not be processed. During development of a Struts 2 web application itâs a good practice to review these log messages to determine if there are any parameters that the framework should not process. For those parameters the Struts 2 framework should not process add the parameter name (or a regular expression that can be used to identify multiple parameter names) to the comma-delimited list that is the value for the <param name=âparams.excludeParamsâ> node.</p> +<p>Itâs a nice feature of the Struts 2 framework that it logs during development which request parameters will and will not be processed. During development of a Struts 2 web application itâs a good practice to review these log messages to determine if there are any parameters that the framework should not process. For those parameters the Struts 2 framework should not process add the parameter name (or a regular expression that can be used to identify multiple parameter names) to the comma-delimited list that is the value for the <code class="highlighter-rouge"><param name="params.excludeParams"></code> node.</p> </section> </article> Modified: websites/production/struts/content/getting-started/http-session.html ============================================================================== --- websites/production/struts/content/getting-started/http-session.html (original) +++ websites/production/struts/content/getting-started/http-session.html Mon Apr 3 10:51:24 2017 @@ -125,95 +125,63 @@ <p>The example code for this tutorial, http_session, is available at <a href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a></p> -<blockquote> +<p><strong>Introduction</strong></p> -</blockquote> +<p>Your Struts 2 application may need to access the HTTP session object. Struts 2 provides an interface, <a href="https://struts.apache.org/maven/struts2-core/apidocs/org/apache/struts2/interceptor/SessionAware.html">SessionAware</a>, that your Action class should implement to obtain a reference to the HTTP session object.</p> -<p>#####Introduction#####</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> -<p>Your Struts 2 application may need to access the HTTP session object. Struts 2 provides an interface, <a href="http://struts.apache.org/2.3.1.2/struts2-core/apidocs/org/apache/struts2/interceptor/SessionAware.html">SessionAware</a>^[http://struts.apache.org/2.3.1.2/struts2-core/apidocs/org/apache/struts2/interceptor/SessionAware.html], that your Action class should implement to obtain a reference to the HTTP session object.</p> - -<table> - <tbody> - <tr> - <td>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a>^[http://struts.apache.org/mail.html] 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.</td> - </tr> - </tbody> -</table> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> - -<p>#####SessionAware Interface#####</p> +<p><strong>SessionAware Interface</strong></p> <p>The SessionAware interface has one method, setSession, that your Action class will need to override. In the example application (see above), the HelloWorldAction class implements the SessionAware interface and includes this code:</p> <p><strong>HelloWorldAction.java setSession Method</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code>private Map<String, Object> userSession ; - -public void setSession(Map<String, Object) session) { - - userSession = session ; - -} - +<div class="highlighter-rouge"><pre class="highlight"><code><span class="kd">private</span> <span class="n">Map</span><span class="o"><</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">></span> <span class="n">userSession</span> <span class="o">;</span> +<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setSession</span><span class="p">(</span><span class="n">Map</span><span class="o"><</span><span class="n">String</span><span class="o">,</span> <span class="n">Object</span><span class="o">)</span> <span class="n">session</span><span class="o">)</span> <span class="o">{</span> + <span class="n">userSession</span> <span class="o">=</span> <span class="n">session</span> <span class="o">;</span> +<span class="o">}</span> </code></pre> </div> <p>The Struts 2 framework has an interceptor that will inject the HTTP session object into the Action class by calling the setSession method.</p> -<p>#####Using the HTTP Session Object In The Action Class#####</p> +<p><strong>Using the HTTP Session Object In The Action Class</strong></p> <p>The example application keeps track of how many times the user clicks on a Hello link or submits the hello form. It stores this count in the HTTP session object in the increaseHelloCount method.</p> <p><strong>HelloWorldAction.java increaseHelloCount Method</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code>private void increaseHelloCount() { - - Integer helloCount = (Integer) userSession.get(HELLO_COUNT); - - if (helloCount == null ) { - - helloCount = 1; - - } else { - - helloCount++; - - } +<div class="highlighter-rouge"><pre class="highlight"><code><span class="kd">private</span> <span class="kt">void</span> <span class="nf">increaseHelloCount</span><span class="p">(</span><span class="o">)</span> <span class="o">{</span> + <span class="n">Integer</span> <span class="n">helloCount</span> <span class="o">=</span> <span class="o">(</span><span class="n">Integer</span><span class="o">)</span> <span class="n">userSession</span><span class="o">.</span><span class="na">get</span><span class="o">(</span><span class="n">HELLO_COUNT</span><span class="o">);</span> - userSession.put(HELLO_COUNT, helloCount); - -} - - + <span class="k">if</span> <span class="o">(</span><span class="n">helloCount</span> <span class="o">==</span> <span class="kc">null</span> <span class="o">)</span> <span class="o">{</span> + <span class="n">helloCount</span> <span class="o">=</span> <span class="mi">1</span><span class="o">;</span> + <span class="o">}</span> <span class="k">else</span> <span class="o">{</span> + <span class="n">helloCount</span><span class="o">++;</span> + <span class="o">}</span> + <span class="n">userSession</span><span class="o">.</span><span class="na">put</span><span class="o">(</span><span class="n">HELLO_COUNT</span><span class="o">,</span> <span class="n">helloCount</span><span class="o">);</span> +<span class="o">}</span> </code></pre> </div> <p>When the increaseHelloCount method is called from within the execute method, the userSession object is a reference to the HTTP session object injected by the Struts 2 framework. So any objects stored in the HTTP session can be retrieved using the userSession object and any objects stored in the userSession object will be stored in the HTTP session object.</p> -<p>#####Accessing HTTP Session Objects In The View#####</p> +<p><strong>Accessing HTTP Session Objects In The View</strong></p> <p>Struts 2 provides an easy way to get an object stored in the HTTP session from within the view page. In the example application is HelloWorld.jsp with this markup:</p> <p><strong>HelloWorld.jsp Get helloCount Value From HTTP Session</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code> <p>I've said hello to you <s:property value="#session.helloCount" /> times!</p> - - +<div class="highlighter-rouge"><pre class="highlight"><code> <span class="nt"><p></span>I've said hello to you <span class="nt"><s:property</span> <span class="na">value=</span><span class="s">"#session.helloCount"</span> <span class="nt">/></span> times!<span class="nt"></p></span> </code></pre> </div> <p>The s:property tagâs value attribute has a value of #session.helloCount. The â#â before the word session tells the Struts framework to look in the session scope for a key of âhelloCountâ (which is the value of the String constant HELLO_COUNT referenced in method increaseHelloCount). Struts will get the object mapped to helloCount key and then call that objectâs toString method to determine what to display in the view page.</p> -<p>#####Best Practices When Using SessionAware#####</p> +<p><strong>Best Practices When Using SessionAware</strong></p> <p>Using SessionAware does introduce a potential security vulnerability that you should mitigate by also following these practices in the Action class that implements the SessionAware interface.</p> @@ -222,26 +190,21 @@ public void setSession(Map<String, Ob <p>Do not have a public Map<String, Object) getSession method in the Action class. You only need a public void setSession method to implement the SessionAware interface.</p> </li> <li> - <p>Also have the Action class implement the <a href="http://struts.apache.org/2.3.1.2/xwork-core/apidocs/com/opensymphony/xwork2/interceptor/ParameterNameAware.html">ParameterNameAware interface</a>^[http://struts.apache.org/2.3.1.2/xwork-core/apidocs/com/opensymphony/xwork2/interceptor/ParameterNameAware.html] and override its acceptableParameterName method:</p> + <p>Also have the Action class implement the <a href="https://struts.apache.org/maven/struts2-core/apidocs/com/opensymphony/xwork2/interceptor/ParameterNameAware.html">ParameterNameAware interface</a> and override its acceptableParameterName method:</p> </li> </ol> <p><strong>HelloWorldAction.java acceptableParameterName Method</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code> public boolean acceptableParameterName(String parameterName) { - - boolean allowedParameterName = true ; - - if ( parameterName.contains("session") || parameterName.contains("request") ) { - - allowedParameterName = false ; - - } - - return allowedParameterName; - } - +<div class="highlighter-rouge"><pre class="highlight"><code> <span class="kd">public</span> <span class="kt">boolean</span> <span class="nf">acceptableParameterName</span><span class="p">(</span><span class="n">String</span> <span class="n">parameterName</span><span class="o">)</span> <span class="o">{</span> + <span class="kt">boolean</span> <span class="n">allowedParameterName</span> <span class="o">=</span> <span class="kc">true</span> <span class="o">;</span> + <span class="k">if</span> <span class="o">(</span> <span class="n">parameterName</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="s">"session"</span><span class="o">)</span> <span class="o">||</span> <span class="n">parameterName</span><span class="o">.</span><span class="na">contains</span><span class="o">(</span><span class="s">"request"</span><span class="o">)</span> <span class="o">)</span> <span class="o">{</span> + <span class="n">allowedParameterName</span> <span class="o">=</span> <span class="kc">false</span> <span class="o">;</span> + <span class="o">}</span> + + <span class="k">return</span> <span class="n">allowedParameterName</span><span class="o">;</span> + <span class="o">}</span> </code></pre> </div> @@ -251,21 +214,19 @@ public void setSession(Map<String, Ob <p><strong>struts.xml configure params interceptor</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code> <package name="basicstruts2" extends="struts-default"> - - <interceptors> - <interceptor-stack name="appDefault"> - <interceptor-ref name="defaultStack"> - <param name="exception.logEnabled">true</param> - <param name="exception.logLevel">ERROR</param> - <param name="params.excludeParams">dojo..*,^struts..*,^session..*,^request..*,^application..*,^servlet(Request|Response)..*,parameters...*</param> - </interceptor-ref> - </interceptor-stack> - </interceptors> +<div class="highlighter-rouge"><pre class="highlight"><code> <span class="nt"><package</span> <span class="na">name=</span><span class="s">"basicstruts2"</span> <span class="na">extends=</span><span class="s">"struts-default"</span><span class="nt">></span> + <span class="nt"><interceptors></span> + <span class="nt"><interceptor-stack</span> <span class="na">name=</span><span class="s">"appDefault"</span><span class="nt">></span> + <span class="nt"><interceptor-ref</span> <span class="na">name=</span><span class="s">"defaultStack"</span><span class="nt">></span> + <span class="nt"><param</span> <span class="na">name=</span><span class="s">"exception.logEnabled"</span><span class="nt">></span>true<span class="nt"></param></span> + <span class="nt"><param</span> <span class="na">name=</span><span class="s">"exception.logLevel"</span><span class="nt">></span>ERROR<span class="nt"></param></span> + <span class="nt"><param</span> <span class="na">name=</span><span class="s">"params.excludeParams"</span><span class="nt">></span>dojo..*,^struts..*,^session..*,^request..*,^application..*,^servlet(Request|Response)..*,parameters...*<span class="nt"></param></span> + <span class="nt"></interceptor-ref></span> + <span class="nt"></interceptor-stack></span> + <span class="nt"></interceptors></span> - <default-interceptor-ref name="appDefault" /> - - + <span class="nt"><default-interceptor-ref</span> <span class="na">name=</span><span class="s">"appDefault"</span> <span class="nt">/></span> +... </code></pre> </div> @@ -281,14 +242,7 @@ public void setSession(Map<String, Ob </tbody> </table> -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> - -<p>#####Summary#####</p> +<p><strong>Summary</strong></p> <p>When your Action class needs to access the HTTP session object implement the SessionAware interface and override the setSession method. Be sure to also implement the ParameterNameAware interface and override the acceptableParameterName method to mitigate a potential security vulnerability. If you have multiple actions that implement SessionAware then consider modifying the params interceptorâs excludeParams value as part of your Struts 2 package setup.</p> Modified: websites/production/struts/content/getting-started/introducing-interceptors.html ============================================================================== --- websites/production/struts/content/getting-started/introducing-interceptors.html (original) +++ websites/production/struts/content/getting-started/introducing-interceptors.html Mon Apr 3 10:51:24 2017 @@ -125,11 +125,7 @@ <p>The example code for this tutorial, interceptors, is available at <a href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a></p> -<blockquote> - -</blockquote> - -<p>#####Introduction#####</p> +<p><strong>Introduction</strong></p> <p>So far our tutorials have not delved into the inner workings of the Struts 2 framework. But in this tutorial weâll introduce a key set of classes the Struts 2 framework relies upon to do most of the work whenever an Action is executed. In this tutorialâs example project there is a register link that is mapped in the Struts XML configuration file (struts.xml) to the execute method of class Register. Before that execute method is called much work is done behind the scenes by the Struts 2 framework. For example:</p> @@ -152,7 +148,7 @@ <p>Handling any exceptions generated</p> </li> <li> - <p>Converting the Register classâs instance fields to String values for display in the view page</p> + <p>Converting the <code class="highlighter-rouge">Register</code> classâs instance fields to String values for display in the view page</p> </li> <li> <p>Forwarding to the correct view page depending on the result String returned by the execute method</p> @@ -161,54 +157,32 @@ <p>The above list of tasks are not complete - several other tasks are done before and after the execution of the Action.</p> -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> - <p>The benefit of using Struts 2 is all this work happens automatically. You can focus on the logic of the controller (the Struts 2 ActionSupport class), the Service layer, the data access layer, your domain models, etc.</p> -<table> - <tbody> - <tr> - <td>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a>^[http://struts.apache.org/mail.html] 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.</td> - </tr> - </tbody> -</table> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> +<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> -<p>#####Introducing Interceptors#####</p> +<p><strong>Introducing Interceptors</strong></p> <p>The tasks that are done by the Struts 2 framework before and after an Action is executed are done by Struts 2 interceptors. Interceptors are standard Java classes included in the Struts 2 core jar which are executed in a specific order.</p> <p>In our example application there is a package node in struts.xml. The package node has an attribute of extends with a value of âstruts-default.â The value âstruts-defaultâ identifies to the framework the specific stack of interceptors that will be executed before and after the Actions in that package.</p> -<p>If you want to learn more about the inner workings of interceptors, what interceptors belong to the struts default stack, and what are all the interceptors included with Struts 2, visit <em>Understanding Interceptors</em> .</p> +<p>If you want to learn more about the inner workings of interceptors, what interceptors belong to the struts default stack, and what are all the interceptors included with Struts 2, visit <a href="//struts.apache.org/docs/interceptors.html">Understanding Interceptors</a> .</p> -<p>Sometime the Struts 2 default stack of interceptors are not exactly what you need for a particular action. You may want to use interceptors that are not part of the Struts 2 default stack. For an individual Action or for the entire package of Actions, you can specify a different stack of interceptors that the Action or package should use. Below is how you would specify that the register Action should use both the <em>logger</em> and <em>timer</em> interceptors in addition to the interceptors provided by the default stack.</p> +<p>Sometime the Struts 2 default stack of interceptors are not exactly what you need for a particular action. You may want to use interceptors that are not part of the Struts 2 default stack. For an individual Action or for the entire package of Actions, you can specify a different stack of interceptors that the Action or package should use. Below is how you would specify that the register Action should use both the <a href="//struts.apache.org/docs/logger-interceptor.html">logger</a> and <a href="//struts.apache.org/docs/logger-interceptor.html">timer</a> interceptors in addition to the interceptors provided by the default stack.</p> <p><strong>Specify Specific Interceptors For An Action</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code><action name="register" class="org.apache.struts.register.action.Register" method="execute"> - <interceptor-ref name="timer" /> - <interceptor-ref name="logger" /> - <interceptor-ref name="defaultStack"> - <param name="exception.logEnabled">true</param> - <param name="exception.logLevel">ERROR</param> - </interceptor-ref> - <result name="success">thankyou.jsp</result> - <result name="input">register.jsp</result> -</action> - - +<div class="highlighter-rouge"><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"><interceptor-ref</span> <span class="na">name=</span><span class="s">"timer"</span> <span class="nt">/></span> + <span class="nt"><interceptor-ref</span> <span class="na">name=</span><span class="s">"logger"</span> <span class="nt">/></span> + <span class="nt"><interceptor-ref</span> <span class="na">name=</span><span class="s">"defaultStack"</span><span class="nt">></span> + <span class="nt"><param</span> <span class="na">name=</span><span class="s">"exception.logEnabled"</span><span class="nt">></span>true<span class="nt"></param></span> + <span class="nt"><param</span> <span class="na">name=</span><span class="s">"exception.logLevel"</span><span class="nt">></span>ERROR<span class="nt"></param></span> + <span class="nt"></interceptor-ref></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"><result</span> <span class="na">name=</span><span class="s">"input"</span><span class="nt">></span>register.jsp<span class="nt"></result></span> +<span class="nt"></action></span> </code></pre> </div> @@ -216,53 +190,42 @@ <p>In the code example above note the three interceptor-ref nodes. Each one has a value for the name attribute. For the register Action we are instructing the framework to use the timer, logger, and defaultStack interceptors. The defaultStack are all the interceptors normally executed for an Action.</p> -<p>How did I know to use the value of timer for the name attribute and even that there is a timer interceptor? On the <em>Interceptors</em> web page in the Struts 2 documentation are a list of interceptors that come with the Struts 2 framework and what the name value is for each interceptor.</p> +<p>How did I know to use the value of timer for the name attribute and even that there is a timer interceptor? On the <a href="//struts.apache.org/docs/interceptors.html">Interceptors</a> web page in the Struts 2 documentation are a list of interceptors that come with the Struts 2 framework and what the name value is for each interceptor.</p> <p>How did I know that the timer interceptor isnât part of the defaultStack of interceptors already? Again on the Interceptors documentation web page is a list of which interceptors belong to the defaultStack.</p> -<p>Note the param nodes. These nodes are used to provide a value to the setLogEnabled and setLogLevel methods of the <em>Exception Interceptor</em> . Providing the values of true and ERROR will cause the Struts 2 framework to log any exceptions not caught by the applicationâs code and to log those exceptions at the ERROR level.</p> +<p>Note the param nodes. These nodes are used to provide a value to the setLogEnabled and setLogLevel methods of the <a href="//struts.apache.org/docs/exception-interceptor.html">Exception Interceptor</a> . Providing the values of true and ERROR will cause the Struts 2 framework to log any exceptions not caught by the applicationâs code and to log those exceptions at the ERROR level.</p> -<p>#####Run The Example#####</p> +<p><strong>Run The Example</strong></p> <p>In the example application follow the README instructions to build, deploy, and run the application. View the output sent to the JVM console to see the log messages generated by the logger and timer interceptors. You should see log messages similar to the following:</p> -<p>INFO: Starting execution stack for action //register - Nov 20, 2010 9:55:48 AM com.opensymphony.xwork2.util.logging.jdk.JdkLogger info</p> - -<p>INFO: Finishing execution stack for action //register</p> - -<p>Nov 20, 2010 9:55:48 AM com.opensymphony.xwork2.util.logging.jdk.JdkLogger info</p> - -<p>INFO: Executed action <a href="https://cwiki.apache.org/register\!execute">//register!execute</a>^[https://cwiki.apache.org/register!execute] took 177 ms.</p> +<div class="highlighter-rouge"><pre class="highlight"><code>INFO: Starting execution stack for action //register +Nov 20, 2010 9:55:48 AM com.opensymphony.xwork2.util.logging.jdk.JdkLogger info +INFO: Finishing execution stack for action //register +Nov 20, 2010 9:55:48 AM com.opensymphony.xwork2.util.logging.jdk.JdkLogger info +INFO: Executed action /register!execute took 177 ms. +</code></pre> +</div> -<p>If you wanted to have the logger and timer interceptors executed for all Actions in a package you would use the following in struts.xml:</p> +<p>If you wanted to have the logger and timer interceptors executed for all Actions in a package you would use the following in <code class="highlighter-rouge">struts.xml</code>:</p> <p><strong>Specify Specific Interceptors For A Package</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code><package name="basicstruts2" extends="struts-default" > - - - <interceptors> +<div class="highlighter-rouge"><pre class="highlight"><code><span class="nt"><package</span> <span class="na">name=</span><span class="s">"basicstruts2"</span> <span class="na">extends=</span><span class="s">"struts-default"</span> <span class="nt">></span> + <span class="nt"><interceptors></span> + <span class="nt"><interceptor-stack</span> <span class="na">name=</span><span class="s">"appDefault"</span><span class="nt">></span> + <span class="nt"><interceptor-ref</span> <span class="na">name=</span><span class="s">"timer"</span> <span class="nt">/></span> + <span class="nt"><interceptor-ref</span> <span class="na">name=</span><span class="s">"logger"</span> <span class="nt">/></span> + <span class="nt"><interceptor-ref</span> <span class="na">name=</span><span class="s">"defaultStack"</span> <span class="nt">/></span> + <span class="nt"></interceptor-stack></span> + <span class="nt"></interceptors></span> - <interceptor-stack name="appDefault"> - - <interceptor-ref name="timer" /> - - <interceptor-ref name="logger" /> - - <interceptor-ref name="defaultStack" /> - - </interceptor-stack> - - </interceptors> - - <default-interceptor-ref name="appDefault" /> - - <!-- rest of package omitted --> - -</package> + <span class="nt"><default-interceptor-ref</span> <span class="na">name=</span><span class="s">"appDefault"</span> <span class="nt">/></span> + <span class="c"><!-- rest of package omitted --></span> +<span class="nt"></package></span> </code></pre> </div> @@ -270,76 +233,14 @@ <p>Note that in both examples we are still executing all the other interceptors by including the defaultStack as one of the interceptor-ref nodes. When you specify what interceptors you want to use for an Action or a package then only those interceptors are executed. So if in the example we had left out the interceptor-ref for defaultStack only the logger and timer interceptors would have executed.</p> -<p>#####Create Your Own Interceptor#####</p> +<p><strong>Create Your Own Interceptor</strong></p> -<p>In addition to specifying your own stack of interceptors, you can also write your own new interceptor and add it to the stack that is executed. The Struts <em>Writing Interceptors</em> guide explains how to do this. For example, you could create your own interceptor to handle authentication and authorization.</p> +<p>In addition to specifying your own stack of interceptors, you can also write your own new interceptor and add it to the stack that is executed. The Struts <a href="//struts.apache.org/docs/writing-interceptors.html">Writing Interceptors</a> guide explains how to do this. For example, you could create your own interceptor to handle authentication and authorization.</p> -<p>#####Summary#####</p> +<p><strong>Summary</strong></p> <p>Interceptors provide the Struts 2 framework with both power and flexibility. Developers may add additional interceptors (either ones provided by Struts 2 or ones they create) to the stack of interceptors executed when an Action class is called.</p> -<p>For more information about interceptors consult the Struts 2 <em>Interceptor</em> documentation.</p> - -<p>##Preparable Interface## {#PAGE_27839279}</p> - -<p>The example code for this tutorial, preparable_interface, is available at <a href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a>.</p> - -<blockquote> - -</blockquote> - -<p>#####Introduction#####</p> - -<p>Often the data used to populate a form control is dynamically generated, perhaps from a database. When the user submits the form, the Struts 2 validation interceptor attempts to validate the userâs form input. If validation fails the Struts 2 framework returns the value âinputâ but the âinputâ action is not re-executed. Rather the view associated with the âinputâ result is rendered to the user. Usually this view is the page that displayed the original form.</p> - -<p>This work-flow can cause a problem if one or more of the form fields or some other data displayed depends on a dynamic look-up that that is accomplished in the Action classâs input method. Since the Action classâs input method is not re-executed when validation fails, the view page may no longer have access to the correct information to create the form or other display information.</p> - -<table> - <tbody> - <tr> - <td>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a>^[http://struts.apache.org/mail.html] 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.</td> - </tr> - </tbody> -</table> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> - -<p>#####Preparable Interface#####</p> - -<p>Struts 2 provides the <a href="http://struts.apache.org/2.3.1/xwork-core/apidocs/com/opensymphony/xwork2/Preparable.html">Preparable interface</a>^[http://struts.apache.org/2.3.1/xwork-core/apidocs/com/opensymphony/xwork2/Preparable.html] to overcome this problem. An Action class that implements this interface must override the prepare method. The prepare method will always be called by the Struts 2 frameworkâs <a href="http://struts.apache.org/2.3.1.2/docs/prepare-interceptor.html">prepare interceptor</a>^[http://struts.apache.org/2.3.1.2/docs/prepare-interceptor.html] whenever any method is called for the Action class and also when validation fails before the view is rendered.</p> - -<p>In the prepare method you should put any statements that must be executed no matter what other Action class method will be called and also statements that should be executed if validation fails. Usually statements in the prepare method set the value for Action class instance fields that will be used to populate form controls and get the values that will be used to set the initial form field values.</p> - -<p>In addition to automatically running the prepare method the <a href="http://struts.apache.org/2.3.1.2/docs/prepare-interceptor.html">prepare interceptor</a>^[http://struts.apache.org/2.3.1.2/docs/prepare-interceptor.html] will also call a method named prepare[ActionMethodName]. For example, define a prepare method and a prepareInput method in the Action class that implements preparable. When the Struts 2 framework calls the input method, the prepare interceptor will call the prepareInput and the prepare methods before calling the input method.</p> - -<p>#####Example Application#####</p> - -<p>If you examine class EditAction in the example application (see above) youâll see that it implements the Preparable Interface. In the prepare method is this code:</p> - -<p><strong>EditAction.java prepare Method</strong></p> - -<div class="highlighter-rouge"><pre class="highlight"><code> - carModelsAvailable = carModelsService.getCarModels() ; - - setPersonBean( editService.getPerson() ); - - -</code></pre> -</div> - -<p>The above statements get the car model values used to populate the car model check boxes displayed in the form and also get the information about the Person object being edited.</p> - -<p>When you run the example application, look in the log to see when the prepare method is called in relation to the input and execute methods. Running the example application and examining the log should help you understand the impact of implementing the Preparable Interface and the prepare method.</p> - -<p>#####Summary#####</p> - -<p>When your application requires specific statements to be executed no matter which method of the Action class is called or when validation fails, you should implement the Preparable interface and override the prepare method.</p> - </section> </article> Modified: websites/production/struts/content/getting-started/preperable-interface.html ============================================================================== --- websites/production/struts/content/getting-started/preperable-interface.html (original) +++ websites/production/struts/content/getting-started/preperable-interface.html Mon Apr 3 10:51:24 2017 @@ -125,51 +125,31 @@ <p>The example code for this tutorial, preparable_interface, is available at <a href="https://github.com/apache/struts-examples">https://github.com/apache/struts-examples</a>.</p> -<blockquote> - -</blockquote> - -<p>#####Introduction#####</p> +<p><strong>Introduction</strong></p> <p>Often the data used to populate a form control is dynamically generated, perhaps from a database. When the user submits the form, the Struts 2 validation interceptor attempts to validate the userâs form input. If validation fails the Struts 2 framework returns the value âinputâ but the âinputâ action is not re-executed. Rather the view associated with the âinputâ result is rendered to the user. Usually this view is the page that displayed the original form.</p> <p>This work-flow can cause a problem if one or more of the form fields or some other data displayed depends on a dynamic look-up that that is accomplished in the Action classâs input method. Since the Action classâs input method is not re-executed when validation fails, the view page may no longer have access to the correct information to create the form or other display information.</p> -<table> - <tbody> - <tr> - <td>The <a href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a>^[http://struts.apache.org/mail.html] 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.</td> - </tr> - </tbody> -</table> - -<table> - <tbody> - <tr> - </tr> - </tbody> -</table> +<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> -<p>#####Preparable Interface#####</p> +<p><strong>Preparable Interface</strong></p> -<p>Struts 2 provides the <a href="http://struts.apache.org/2.3.1/xwork-core/apidocs/com/opensymphony/xwork2/Preparable.html">Preparable interface</a>^[http://struts.apache.org/2.3.1/xwork-core/apidocs/com/opensymphony/xwork2/Preparable.html] to overcome this problem. An Action class that implements this interface must override the prepare method. The prepare method will always be called by the Struts 2 frameworkâs <a href="http://struts.apache.org/2.3.1.2/docs/prepare-interceptor.html">prepare interceptor</a>^[http://struts.apache.org/2.3.1.2/docs/prepare-interceptor.html] whenever any method is called for the Action class and also when validation fails before the view is rendered.</p> +<p>Struts 2 provides the <a href="http://struts.apache.org/2.3.1/xwork-core/apidocs/com/opensymphony/xwork2/Preparable.html">Preparable interface</a>^[http://struts.apache.org/2.3.1/xwork-core/apidocs/com/opensymphony/xwork2/Preparable.html] to overcome this problem. An Action class that implements this interface must override the prepare method. The prepare method will always be called by the Struts 2 frameworkâs <a href="//struts.apache.org/docs/prepare-interceptor.html">prepare interceptor</a> whenever any method is called for the Action class and also when validation fails before the view is rendered.</p> <p>In the prepare method you should put any statements that must be executed no matter what other Action class method will be called and also statements that should be executed if validation fails. Usually statements in the prepare method set the value for Action class instance fields that will be used to populate form controls and get the values that will be used to set the initial form field values.</p> -<p>In addition to automatically running the prepare method the <a href="http://struts.apache.org/2.3.1.2/docs/prepare-interceptor.html">prepare interceptor</a>^[http://struts.apache.org/2.3.1.2/docs/prepare-interceptor.html] will also call a method named prepare[ActionMethodName]. For example, define a prepare method and a prepareInput method in the Action class that implements preparable. When the Struts 2 framework calls the input method, the prepare interceptor will call the prepareInput and the prepare methods before calling the input method.</p> +<p>In addition to automatically running the prepare method the <a href="//struts.apache.org/docs/prepare-interceptor.html">prepare interceptor</a> will also call a method named prepare[ActionMethodName]. For example, define a prepare method and a prepareInput method in the Action class that implements preparable. When the Struts 2 framework calls the input method, the prepare interceptor will call the prepareInput and the prepare methods before calling the input method.</p> -<p>#####Example Application#####</p> +<p><strong>Example Application</strong></p> <p>If you examine class EditAction in the example application (see above) youâll see that it implements the Preparable Interface. In the prepare method is this code:</p> <p><strong>EditAction.java prepare Method</strong></p> -<div class="highlighter-rouge"><pre class="highlight"><code> - carModelsAvailable = carModelsService.getCarModels() ; +<div class="highlighter-rouge"><pre class="highlight"><code> <span class="n">carModelsAvailable</span> <span class="o">=</span> <span class="n">carModelsService</span><span class="o">.</span><span class="na">getCarModels</span><span class="o">()</span> <span class="o">;</span> - setPersonBean( editService.getPerson() ); - - + <span class="n">setPersonBean</span><span class="o">(</span><span class="n">editService</span><span class="o">.</span><span class="na">getPerson</span><span class="o">());</span> </code></pre> </div> @@ -177,7 +157,7 @@ <p>When you run the example application, look in the log to see when the prepare method is called in relation to the input and execute methods. Running the example application and examining the log should help you understand the impact of implementing the Preparable Interface and the prepare method.</p> -<p>#####Summary#####</p> +<p><strong>Summary</strong></p> <p>When your application requires specific statements to be executed no matter which method of the Action class is called or when validation fails, you should implement the Preparable interface and override the prepare method.</p>