You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@struts.apache.org by mr...@apache.org on 2005/10/28 08:32:00 UTC

svn commit: r329127 [3/3] - in /struts/bsf/trunk/src/struts-example: ./ WEB-INF/ WEB-INF/classes/ WEB-INF/lib/ WEB-INF/scripts/

Added: struts/bsf/trunk/src/struts-example/tour.html
URL: http://svn.apache.org/viewcvs/struts/bsf/trunk/src/struts-example/tour.html?rev=329127&view=auto
==============================================================================
--- struts/bsf/trunk/src/struts-example/tour.html (added)
+++ struts/bsf/trunk/src/struts-example/tour.html Thu Oct 27 23:29:07 2005
@@ -0,0 +1,986 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
+    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+
+<html xmlns="http://www.w3.org/1999/xhtml">
+<head>
+  <meta name="generator" content="HTML Tidy for Windows (vers 1st July 2003), see www.w3.org" />
+  <meta http-equiv="Content-Type" content="text/html; charset=us-ascii" />
+  <link rel="stylesheet" type="text/css" href="base.css" />
+
+  <title>A Walking Tour of the Struts MailReader Demonstration Application</title>
+</head>
+
+<body>
+  <blockquote>
+    <h2>A Walking Tour of the Struts MailReader Demonstration Application</h2>
+
+    <p><i>This article is meant to introduce a new user to Struts by "walking through" an application. See the <a href="http://jakarta.apache.org/struts/">Struts Users Guide and Strut's API</a> for more documentation.</i></p>
+
+    <p><i>The MailReader application is based on the 1.2.0 build of Struts. To follow along, you should install the MailReader application on your own development workstation (e.g. localhost).</i></p>
+
+    <p><i>The article assumes the reader has a basic understanding of the Java language, JavaBeans, web applications, and JavaServer Pages. For background on these technologies, see the <a href="http://jakarta.apache.org/struts/userGuide/preface.html">Preface to the Struts User Guide</a>.</i></p>
+    <hr />
+
+    <ul>
+      <li>
+        <a href="#index.jsp">index.jsp</a>
+
+        <ul>
+          <li><a href="#web.xml">web.xml and ApplicationResources.properties</a></li>
+
+          <li><a href="#DatabaseServlet.java">DatabaseServlet.java</a></li>
+        </ul>
+      </li>
+
+      <li>
+        <a href="#logon.jsp">logon.jsp</a>
+
+        <ul>
+          <li><a href="#struts-config.xml">struts-config.xml</a></li>
+
+          <li><a href="#struts-config.xml">LogonForm.java</a></li>
+
+          <li><a href="#LogonAction.java">LogonAction.java</a></li>
+
+          <li><a href="#struts-config.xml/2">struts-config.xml 2</a></li>
+        </ul>
+      </li>
+
+      <li>
+        <a href="#mainMenu.jsp">mainMenu.jsp</a>
+
+        <ul>
+          <li><a href="#CheckLoginTag.java">CheckLoginTag.java</a></li>
+
+          <li><a href="#editRegistrationAction.java">editRegistrationAction.java</a></li>
+        </ul>
+      </li>
+
+      <li>
+        <a href="#subscription.jsp">registration.jsp</a>
+
+        <ul>
+          <li><a href="#RegistrationForm.java">RegistrationForm.java</a></li>
+
+          <li><a href="#EditRegistrationAction.java">EditRegistrationAction.java</a></li>
+
+          <li><a href="#LinkSubscriptionTag.java">LinkSubscriptionTag.java</a></li>
+
+          <li><a href="#LinkUserTag.java">LinkUserTag.java</a></li>
+
+          <li><a href="#EditSubscriptionAction.java">EditSubscriptionAction.java</a></li>
+        </ul>
+      </li>
+
+      <li>
+        <a href="#subcription.jsp">subscription.jsp</a>
+
+        <ul>
+          <li><a href="#SubscriptionForm.java">SubscriptionForm.java</a></li>
+
+          <li><a href="#SaveSubscriptionAction.java">SaveSubscriptionAction.java</a></li>
+        </ul>
+      </li>
+
+      <li><a href="#Summary">Summary</a></li>
+    </ul>
+    <hr />
+
+    <p>The Struts distribution bundles four applications: struts-documentation, tiles-documentation, struts-example, and struts-examples. This document walks through the struts-example, also known as the "MailReader Demonstration Application".</p>
+
+    <p>The premise of the MailReader is that it is the first iteration of a portal application. This version allows users to register themselves and maintain a set of accounts with various mail servers. When completed, the application would let users read mail from their accounts.</p>
+
+    <p>The MailReader application demonstrates registering with an application, logging into an application, maintaining a master record, and maintaining child records. This document walks through the JSP pages, Struts Java classes, and Struts configuration elements needed to do these things.</p>
+
+    <blockquote>
+      <p>Note that for compatability and ease of deployment, the MailReader uses "application-based" authorization. However, use of the standard Java Authentication and Authorization Service (JAAS) is recommended for most applications. (See the <a href="http://jakarta.apache.org/struts/userGuide/preface.html">Preface to the Struts User Guide</a> for more about authentification technologies.)</p>
+    </blockquote>
+
+    <p>The walkthrough starts with how the initial welcome page is displayed, and then steps through logging in, adding and editing subscriptions, and creating a new registration.</p>
+
+    <h3><a name="index.jsp" id="index.jsp">index.jsp</a></h3>
+
+    <p>A web application, like any other web site, can specify a list of welcome pages. When you open a web application without specifying a particular page, a welcome page is used by default.</p>
+
+    <p>Struts allows developers to manage an application through "virtual pages" called <i>actions</i>. An accepted practice in Struts is to never link directly to server pages, but only to these actions. The actions are listed in a configuration file. By linking to actions, developers can "rewire" an application without editing the server pages.</p>
+
+    <blockquote>
+      <p><font class="hint">"Link actions not pages."</font></p>
+    </blockquote>
+
+    <p>Unfortunately, actions cannot be specified as a welcome page. Since there can be a list of pages, the web server looks for each page on the list before selecting one. The web server doesn't see actions as pages and will never select one as a welcome page. So, in the case of a welcome page, how do we follow the Struts best practice of navigating through actions rather than pages?</p>
+
+    <p>One solution is to use a server page to "bootstrap" a Struts action. A Java web application recognizes the idea of "forwarding" from one page to another page (or action). We can register the usual "index.jsp" as the welcome page and have it forward to a "Welcome" action. Here's the MailReader's index.jsp:</p>
+    <hr />
+    <pre>
+<code>&lt;%@ taglib uri="/tags/struts-logic" prefix="logic" %&gt;
+&lt;logic:redirect action="/Welcome"/&gt;</code>
+</pre>
+    <hr />
+
+    <p>At the top of the page, we import the "struts-logic" JSP tag library. (Again, see the <a href="http://jakarta.apache.org/struts/userGuide/preface.html">Preface to the Struts User Guide</a> for more about the technologies underlying Struts.) The page itself consists of a single tag that redirects to the "Welcome" action. The tag inserts the actual web address for the redirect when the page is rendered. But, where does the tag find the actual address to insert?</p>
+
+    <p>The list of actions, along with other Struts components, are registered through one or more Struts configuration files. The configuration files are written as XML documents and processed when the application starts. If we just wanted to forward to the welcome page, we could use a configuration element like this:</p>
+    <hr />
+    <pre>
+<code>&lt;!-- Display welcome page --&gt;
+&lt;action path="/Welcome" forward="/welcome.jsp" /&gt;</code>
+</pre>
+    <hr />
+
+    <p>If someone asked for the Welcome action ("/Welcome.do"), the welcome.jsp page would be displayed in return.</p>
+
+    <h4><a name="WelcomeAction.java" id="WelcomeAction.java">WelcomeAction.java</a></h4>
+
+    <p>But if we peek at the configuration file for the MailReader, we find a slightly more complicated XML element for the Welcome action:</p>
+    <hr />
+    <pre>
+<code>&lt;!-- Display welcome page --&gt;
+&lt;action path="/Welcome"
+  type="org.apache.struts.webapp.example.WelcomeAction"&gt;
+  &lt;forward name="failure" path="/Error.jsp" /&gt;
+  &lt;forward name="success" path="/welcome.jsp" /&gt;
+&lt;/action&gt;</code>
+</pre>
+    <hr />
+
+    <p>Here, the "WelcomeAction" Java class executes whenever someone asks for the Welcome action. As it completes, the Action class can select which page is displayed. Two pages the class can select here are "Error.jsp" and "welcome.jsp". But the Action class doesn't need to know the path to the pages. The class can select them just using the names "success" or "failure".</p>
+
+    <p>OK ... but why would a WelcomeAction want to choose between success and failure?</p>
+
+    <p>The MailReader application retains a list of users along with their email accounts. The application stores this information in a database. If the application can't connect to the database, the application can't do its job. So before displaying the welcome page, the class checks to see if the database is available. The MailReader is also an internationalized application. So, the WelcomeAction checks to see if the message resources are available too. If both resources are available, the class forwards to the "success" path. Otherwise, it forwards to the "failure" path so that the appropriate error messages can be displayed.</p>
+
+    <h4><a name="MemoryDatabasePlugIn.java" id="MemoryDatabasePlugIn.java">MemoryDatabasePlugIn.java</a></h4>
+
+    <p>The database is exposed to the application as an object stored in application scope. The database object is based on an interface. Different implementations of the database could be loaded without changing the rest of the application. But how is the database object loaded in the first place?</p>
+
+    <p>One section of the Struts configuration is devoted to "PlugIns". When a Struts application loads, it also loads whatever PlugIns are specified in its configuration. The PlugIn interface is quite simple, and you can use PlugIns to do anything that might need to be done when your application loads. The PlugIn is also notified when the application shuts down, so you can release any allocated resources.</p>
+    <hr />
+    <pre>
+<code>&lt;plug-in className="org.apache.struts.webapp.example.memory.MemoryDatabasePlugIn"&gt;
+  &lt;set-property property="pathname" value="/WEB-INF/database.xml"/&gt;
+&lt;/plug-in&gt;</code>
+</pre>
+    <hr />
+
+    <p>By default, the MailReader application loads a "MemoryDatabase" implementation of the UserDatabase. MemoryDatabase stores the database contents as a XML document, which is parsed by the Digester and loaded as a set of nested hashtables. The outer table is the list of user objects, each of which has its own inner hashtable of subscriptions. When you register, a user object is stored in this hashtable ... and when you login, the user object is stored within the session context.</p>
+
+    <p>The database comes seeded with a sample user. If you check the database.xml file under WEB-INF, you'll see the sample user described as:</p>
+    <hr />
+    <pre>
+<code>&lt;user username="user" fromAddress="John.User@somewhere.com" fullName="John Q. User" password="pass"&gt;
+  &lt;subscription host="mail.hotmail.com" autoConnect="false" password="bar" type="pop3" username="user1234"&gt;
+  &lt;/subscription&gt;
+  &lt;subscription host="mail.yahoo.com" autoConnect="false" password="foo" type="imap" username="jquser"&gt;
+  &lt;/subscription&gt;
+&lt;/user&gt;</code>
+</pre>
+    <hr />
+
+    <p>This creates a registration record for "John Q. User", with the detail for his hotmail account (or "subscription").</p>
+
+    <h4><a name="MessageResources.properties" id="MessageResources.properties">MessageResources.properties</a></h4>
+
+    <p>Another section of the Struts configuration loads the message resources for the application. If you change a message in the resource, and then reload the application, the change will appear throughout the application. If you provide message resources for additional locales, you can internationalize your application.</p>
+    <hr />
+    <pre>
+<code>&lt;message-resources parameter="org.apache.struts.webapp.example.MessageResources" /&gt;</code>
+</pre>
+    <hr />
+
+    <p>This is a standard properties text file. Here are the entries used by the welcome page:</p>
+    <hr />
+    <pre>
+<code>index.heading=MailReader Demonstration Application Options
+index.logon=Log on to the MailReader Demonstration Application
+index.registration=Register with the MailReader Demonstration Application
+index.title=MailReader Demonstration Application (Struts 1.2.1-dev)
+index.tour=A Walking Tour of the MailReader Demonstration Application</code>
+</pre>
+    <hr />
+
+    <p>The MailReader application uses a second set of message resources for non-text elements. The "key" element can be used to access this resource bundle rather than the default bundle.</p>
+    <hr />
+    <pre>
+<code>&lt;message-resources parameter="org.apache.struts.webapp.example.AlternateMessageResources" key="alternate" /&gt;</code>
+</pre>
+    <hr />
+
+    <h3><a name="welcome.jsp" id="welcome.jsp">welcome.jsp</a></h3>
+
+    <p>After confirming that the necessary resources exist, the WelcomeAction forwards to the welcome.jsp page.</p>
+    <hr />
+    <pre>
+<code>&lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt;
+&lt;%@ taglib uri="/tags/struts-bean" prefix="bean" %&gt;
+&lt;%@ taglib uri="/tags/struts-html" prefix="html" %&gt;
+
+&lt;html&gt;
+&lt;head&gt;
+&lt;title&gt;&lt;bean:message key="index.title"/&gt;&lt;/title&gt;
+&lt;link rel="stylesheet" type="text/css" href="base.css" /&gt;
+&lt;/head&gt;
+
+&lt;h3&gt;&lt;bean:message key="index.heading"/&gt;&lt;/h3&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;html:link action="/EditRegistration?action=Create"&gt;<br />
+&lt;bean:message key="index.registration"/&gt;&lt;/html:link&gt;&lt;/li&gt;
+&lt;li&gt;&lt;html:link action="/Logon"&gt;&lt;bean:message key="index.logon"/&gt;&lt;/html:link&gt;&lt;/li&gt;
+&lt;/ul&gt;
+
+&lt;h3&gt;Change Language&lt;/h3&gt;
+&lt;ul&gt;
+&lt;li&gt;&lt;html:link action="/Locale?language=en"&gt;English&lt;/html:link&gt;&lt;/li&gt;
+&lt;li&gt;&lt;html:link action="/Locale?language=ja" useLocalEncoding="true"&gt;Japanese&lt;/html:link&gt;&lt;/li&gt;
+&lt;li&gt;&lt;html:link action="/Locale?language=ru" useLocalEncoding="true"&gt;Russian&lt;/html:link&gt;&lt;/li&gt;
+&lt;/ul&gt;
+
+&lt;hr /&gt;
+
+&lt;p&gt;&lt;html:img bundle="alternate" pageKey="struts.logo.path" altKey="struts.logo.alt"/&gt;&lt;/p&gt;
+
+&lt;p&gt;&lt;html:link action="/Tour"&gt;&lt;bean:message key="index.tour"/&gt;&lt;/html:link&gt;&lt;/p&gt;
+
+&lt;/body&gt;
+&lt;/html&gt;</code>
+</pre>
+    <hr />
+
+    <p>At the top of the welcome.jsp page, there are several directives that load the Struts tag libraries. These are just the usual red tape that goes with any JSP file. The rest of the page demonstrates three Struts JSP tags: "bean:message", "html:link", and "html:img".</p>
+
+    <p>The bean:message tag inserts a message from the MessageResources file. The MailReader comes with support for three locales: English (the default), Russian, and Japanese. If the Struts locale setting is changed for a user, the bean:message tag will render messages from that locale's property bundle instead.</p>
+
+    <p>The html:link tag does double duty. First, you can refer to an action or forward stored in the Struts configuration, and the tag will insert the corresponding path when the page is rendered. This makes it easy to "rewire" an application without touching all the pages. Second, the link tag will "URL encode" the hyperlink to maintain the client session. Your application can maintain client state without requiring cookies.</p>
+
+    <blockquote>
+      <p><font class="hint">If you turn cookies off in your browser, and then reload your browser and this page, you will see the links with the Java session id information attached. (If you are using Internet Explorer and try this, be sure you reset cookies for the appropriate security zone, and that you disallow "per-session" cookies.)</font></p>
+    </blockquote>
+
+    <p>The html:img tag renders an img tag. When necessary, the src URI is encoded as it is with the link tag. In this case, the tag inserts the src path from the "alternate" MessageResource bundle, along with the text for the alt element.</p>
+
+    <p>In the span of a single request, Struts has done quite a bit already:</p>
+
+    <ul>
+      <li>Confirmed that required objects were created during initialization.</li>
+
+      <li>Written all the page headings and labels from internationalized message resources.</li>
+
+      <li>Automatically URL-encoded paths as needed.</li>
+    </ul>
+
+    <p>When rendered, the welcome page lists two menu options: one to register with the application and one to login in (if you have already registered). Let's follow the login link first.</p>
+
+    <h3><a name="logon.jsp" id="logon.jsp">logon.jsp</a></h3>
+
+    <p>If you choose the logon link, the Logon action forwards control to the logon.jsp page. The logon page displays a form that accepts a username and password. You can use the default username and password to logon (user and pass). Note that both the username and password are case sensitive. Better yet, try omitting or misspelling the username and password in various combinations and see how the application reacts.</p>
+
+    <p>Here's how the login.jsp is coded:</p>
+    <hr />
+    <pre>
+<code>&lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt;
+&lt;%@ taglib uri="/tags/struts-bean" prefix="bean" %&gt;
+&lt;%@ taglib uri="/tags/struts-html" prefix="html" %&gt;
+
+&lt;html:xhtml/&gt;
+&lt;html&gt;
+&lt;head&gt;
+&lt;title&gt;&lt;bean:message key="logon.title"/&gt;&lt;/title&gt;
+&lt;/head&gt;
+
+&lt;html:errors/&gt;
+
+&lt;html:form action="/SubmitLogon" focus="username"
+         onsubmit="return validateLogonForm(this);"&gt;
+&lt;table border="0" width="100%"&gt;
+
+  &lt;tr&gt;
+    &lt;th align="right"&gt;
+      &lt;bean:message key="prompt.username"/&gt;:
+    &lt;/th&gt;
+    &lt;td align="left"&gt;
+      &lt;html:text property="username" size="16" maxlength="18"/&gt;
+    &lt;/td&gt;
+  &lt;/tr&gt;
+
+  &lt;tr&gt;
+    &lt;th align="right"&gt;
+      &lt;bean:message key="prompt.password" bundle="alternate"/&gt;:
+    &lt;/th&gt;
+    &lt;td align="left"&gt;
+      &lt;html:password property="password" size="16" maxlength="18"
+                    redisplay="false"/&gt;
+    &lt;/td&gt;
+  &lt;/tr&gt;
+
+  &lt;tr&gt;
+    &lt;td align="right"&gt;
+      &lt;html:submit property="Submit" value="Submit"/&gt;
+    &lt;/td&gt;
+    &lt;td align="left"&gt;
+      &lt;html:reset/&gt;
+    &lt;/td&gt;
+  &lt;/tr&gt;
+
+&lt;/table&gt;
+
+&lt;/html:form&gt;
+
+&lt;html:javascript formName="LogonForm"
+        dynamicJavascript="true"
+         staticJavascript="false"/&gt;
+&lt;script language="Javascript1.1" src="staticJavascript.jsp"&gt;&lt;/script&gt;
+
+&lt;jsp:include page="footer.jsp" /&gt;
+&lt;/body&gt;
+&lt;/html&gt;</code>
+</pre>
+    <hr />
+
+    <p>We saw some of these tags on the welcome page. Let's focus on the new tags.</p>
+
+    <p>The first new tag on the logon page is "html:errors". The credentials you entered are processed by a "LogonAction" class. If the credentials are incorrect, the LogonAction posts an appropriate error message and forwards back to the input page. If the html:errors tag sees that one or more messages were posted, the tag ouputs the messages to the page. The text of the messages can be specified in the MessageResource bundle, making them easy to internationalize.</p>
+
+    <p>The second new tag is "html:form". This tag renders a html form tag. The "action" element tells the tag to use "SubmitLogon.do" for the form's action. The "focus" attribute tells the tag to generate a little Javascript after the form that sets its focus to the "username" field. The "onsubmit" attribute tells the form to run a Javascript when the form is submitted. (Just like the corresponding attribute on the standard form tag.)</p>
+
+    <p>Within the html:form tag, we see four other new tags: "html:text", "html:password", "html:submit", and "html:reset".</p>
+
+    <p>The html:text tag renders a "input type=text" tag. The "property" attribute becomes the input tag's "name" attribute.</p>
+
+    <p>The html:password tag renders a "input type=password" tag. The "redisplay" attribute tell the tag not to render the password back into the file, if the submit fails. The html:submit and html:reset tags render buttons of the corresponding types.</p>
+
+    <p>Following the form is a "html:javascript" tag. This tag works with the Struts Validator component to generate a JavaScript that can validate input before it is submitted to the LogonAction.</p>
+
+    <blockquote>
+      <p><font class="hint">Most of these tags have many more options than the ones we use in this application. For the complete documentation for each tag, see the Tag Developers Guides in the Struts documentation bundle.</font></p>
+    </blockquote>
+
+    <p>But, how do these tags know so much? How does the Javascript tag know what scripts to write? How do the text and password tags know what values to redisplay?</p>
+
+    <p>For the answers, we need to turn to the Struts configuration files, "struts-config.xml" and "validation.xml".</p>
+
+    <h4><a name="struts-config.xml" id="struts-config.xml">struts-config.xml</a></h4>
+
+    <p>In the struts-config.xml file, we find an element for the "/SubmitLogon" action</p>
+    <hr />
+    <pre>
+<code>&lt;!-- Process a user logon --&gt;
+&lt;action    path="/SubmitLogon"
+                  type="org.apache.struts.webapp.example.LogonAction"
+                  name="LogonForm"
+                 scope="request"
+                 input="logon"&gt;
+ &lt;exception
+                   key="expired.password"
+                  type="org.apache.struts.webapp.example.ExpiredPasswordException"
+                  path="/ExpiredPassword.do"/&gt;
+       &lt;/action&gt;</code>
+</pre>
+    <hr />
+
+    <p>We saw the path and type attributes in the Welcome action. Let's look at the new attributes.</p>
+
+    <p>The "name" attribute specifies something Struts calls an "ActionForm". The ActionForm buffers input from a form and delivers it to an Action class as an object. The ActionForm can also validate the input. If validation fails, the tags can rewrite the input values from the ActionForm.</p>
+
+    <p>The ActionForms are defined in the "formbeans" section of the configuration file. Here's the formbean element for the "LogonForm".</p>
+    <hr />
+    <pre>
+<code>&lt;form-bean       name="LogonForm"
+                 type="org.apache.struts.validator.DynaValidatorForm"&gt;
+                 &lt;form-property name="username" type="java.lang.String"/&gt;
+                 &lt;form-property name="password" type="java.lang.String"/&gt;
+&lt;/form-bean&gt;</code>
+</pre>
+    <hr />
+
+    <p>ActionForms can be "conventional" or "dynamic". Here, we are using a dynamic ActionForm. Rather than cobble up an actual JavaBean class, we specify the properties the ActionForm can accept in the configuration file. If the property is not specified here, it is not captured, validated, or passed up to the Action class.</p>
+
+    <p>Struts creates the ActionForms automatically. The "scope" attribute in the action element tells the controller wether to store the ActionForm in the request or in the user's session.</p>
+
+    <blockquote>
+      <p><font class="hint">The Struts best practice is to use request scope for single-page forms that contain all the properties needed by the Action. There is usually no need to maintain form data across requests.</font></p>
+    </blockquote>
+
+    <p>Struts can also validate the ActionForm automatically. If validation fails, Struts looks for the forward specified by the "input" attribute. In this case, the "logon" forward sends control back to the input.jsp page.</p>
+    <hr />
+    <pre>
+<code>&lt;forward name="logon" path="/Logon.do"/&gt;</code>
+</pre>
+    <hr />
+
+    <p>Within the logon action element is another new element, "exception". When a user logons on, it's possible that an "ExpiredPasswordException" will be thrown. Should this happen, Struts will capture the exception and send control to the "ExpiredPassword" action.</p>
+
+    <h4><a name="validations.xml" id="validations.xml">validations.xml</a></h4>
+
+    <p>In the logon.jsp, we mentioned that the html:javascript tag confers with the Struts Validator components. The Validator is configured through another XML document, the "validation.xml". Here's the element for our LogonForm:</p>
+    <hr />
+    <pre>
+<code>&lt;form name="LogonForm"&gt;
+
+        &lt;field property="username"
+                        depends="required"&gt;
+                &lt;arg0   key="prompt.username"/&gt;
+        &lt;/field&gt;
+
+        &lt;field property="password"
+                        depends="required, minlength,maxlength"&gt;
+                &lt;arg0   key="prompt.password"/&gt;
+                &lt;arg1   key="${var:minlength}" name="minlength"
+                   resource="false"/&gt;
+                &lt;arg2   key="${var:maxlength}" name="maxlength"
+                   resource="false"/&gt;
+                &lt;var&gt;
+                        &lt;var-name&gt;maxlength&lt;/var-name&gt;
+                        &lt;var-value&gt;16&lt;/var-value&gt;
+                &lt;/var&gt;
+                &lt;var&gt;
+                        &lt;var-name&gt;minlength&lt;/var-name&gt;
+                        &lt;var-value&gt;3&lt;/var-value&gt;
+                &lt;/var&gt;
+        &lt;/field&gt;
+
+&lt;/form&gt;</code>
+</pre>
+    <hr />
+
+    <p>The field elements correspond to the ActionForm properties. The "username" field element says it depends on the "required" validator. If the username is blank or absent, validation will fail and an error message is automatically generated.</p>
+
+    <p>The "password" field (or property) is also required. In addition, it must also pass the "maxlength" and "minlength" validations. Here, the minimum length is three characters and the maximum length is sixteen. If the length of the password doesn't meet these criteria, a corresponding error message is generated. Of course, the messages are generated from the MessageResource bundles and are easy to localize.</p>
+
+    <h4><a name="LogonAction.java" id="LogonAction.java">LogonAction.java</a></h4>
+
+    <p>If validation passes, the LogonForm object is forwarded to the LogonAction. The LogonAction interacts with the database to see if the credentials are valid. If so, the user is logged on, and control passes to the "success" forward. Otherwise, control is forwarded to the input page and the list of error messages displayed.</p>
+
+    <p>Here's the LogonAction (sans comments):</p>
+    <hr />
+    <pre>
+<code>package org.apache.struts.webapp.example;
+import ...
+
+public final class LogonAction extends BaseAction {
+
+    private static String USERNAME = "username";
+    private static String PASSWORD = "password";
+
+    User getUser(UserDatabase database, String username,
+                 String password, ActionMessages errors)
+                 throws ExpiredPasswordException {
+
+        User user = null;
+        if (database == null){
+            errors.add(
+                ActionMessages.GLOBAL_MESSAGE,
+                new ActionMessage("error.database.missing"));
+        }
+        else {
+            user = database.findUser(username);
+            if ((user != null) &amp;&amp; !user.getPassword().equals(password)) {
+                user = null;
+            }
+            if (user == null) {
+                errors.add(
+                    ActionMessages.GLOBAL_MESSAGE,
+                    new ActionMessage("error.password.mismatch"));
+            }
+        }
+
+        return user;
+
+    }
+
+    void SaveUser(HttpServletRequest request, User user) {
+
+        HttpSession session = request.getSession();
+
+        session.setAttribute(Constants.USER_KEY, user);
+        if (log.isDebugEnabled()) {
+            log.debug(
+                "LogonAction: User '"
+                    + user.getUsername()
+                    + "' logged on in session "
+                    + session.getId());
+        }
+
+    }
+
+    public ActionForward execute(
+        ActionMapping mapping,
+        ActionForm form,
+        HttpServletRequest request,
+        HttpServletResponse response)
+        throws Exception {
+
+        UserDatabase database = getUserDatabase(request);
+        String username = (String) PropertyUtils.getSimpleProperty(form,
+                USERNAME);
+        String password = (String) PropertyUtils.getSimpleProperty(form,
+                PASSWORD);
+        ActionMessages errors = new ActionMessages();
+
+        User user = getUser(database,username,password,errors);
+
+        SaveUser(request,user);
+
+        if (!errors.isEmpty()) {
+            this.saveErrors(request, errors);
+            return (mapping.getInputForward());
+        }
+
+        return (findSuccess(mapping));
+
+    }</code>
+</pre>
+    <hr />
+
+    <h4><a name="MainMenu.do" id="MainMenu.do">MainMenu.do and mainMenu.jsp</a></h4>
+
+    <p>On a successful logon, the Main Menu page displays. If you logged in using the default account, the page title should be "Main Menu Options for John Q. User". Below this legend should be two links:
+
+    <ul>
+      <li>Edit your user registration profile</li>
+
+      <li>Log off MailReader Demonstration Application</li>
+    </ul>
+
+    <p>If you check the address shown by your browser, you will see that it shows "/SubmitLogon.do" not "/MainMenu.do". The Java servlet platform supports the idea of server-side forwards. When control passed from the SubmitLogon action to the MainMenu action, everything occured server-side. All the browser knows is that we are looking at the result of submitting a form to "/LogonSubmit.do", so that's the address that shows. It doesn't know control passed from one action to another. The difference between server-side forwards and client-side redirects is subtle and often confuses new developers. </p>
+
+    <p>If you change the address to "/MainMenu.do" and press enter, the same page will display again.</p>
+
+    <p>Here's the JSP source for the "MainMenu" action and the "mainMenu.jsp".</p>
+
+    <hr />
+
+    <blockquote><pre><code>&lt;action path="/MainMenu" forward="/mainMenu.jsp"/></blockquote></code></pre>
+
+    <hr />
+
+    <blockquote><pre><code>&lt;%@ page contentType="text/html;charset=UTF-8" language="java" %>
+&lt;%@ taglib uri="/tags/app" prefix="app" %>
+&lt;%@ taglib uri="/tags/struts-bean" prefix="bean" %>
+&lt;%@ taglib uri="/tags/struts-html" prefix="html" %>
+&lt;app:checkLogon/>
+&lt;html>
+&lt;head>
+&lt;title>&lt;bean:message key="mainMenu.title"/>&lt;/title>
+&lt;link rel="stylesheet" type="text/css" href="base.css" />
+&lt;/head>
+&lt;h3>&lt;bean:message key="mainMenu.heading"/> &lt;bean:write name="user" property="fullName" />&lt;/h3>
+&lt;ul>
+&lt;li>&lt;html:link action="/EditRegistration?action=Edit">&lt;bean:message key="mainMenu.registration"/>&lt;/html:link>&lt;/li>
+&lt;li>&lt;html:link forward="logoff">&lt;bean:message key="mainMenu.logoff"/>&lt;/html:link>&lt;/li>
+&lt;/ul>
+&lt;/body>
+&lt;/html></blockquote></code></pre>
+
+    <hr />
+
+    <p>If you check the source for mainMenu.jsp, you will find two new tags: "app:checkLogon" and "bean:write". Let's look at the bean tag first.</p>
+
+
+    <p>When control passed through the LogonAction, it retrieved a "User" object from the database and stored a reference in "session" scope. This object is a JavaBean with several properties. One property is "fullName". The bean:write tag can find the User bean and print the fullName property for us.</p>
+
+    <p>But how can the page be sure that we are logged in and that the User object exists?</p>
+
+	<p>At the top of the page, above the "html" tag, is the other new tag, checkLogon. This tag, true to its name, checks to see of the user is logged on. If not, the tag forwards to the LogonAction. Let's have a look:
+
+    <hr />
+
+    <blockquote><pre><code>package org.apache.struts.webapp.example;
+import ...
+
+public final class CheckLogonTag extends TagSupport {
+
+    private String name = Constants.USER_KEY;
+    private static String LOGIN_PATH = "/Logon.do";
+    private String page = LOGIN_PATH;
+
+    public int doStartTag() throws JspException {
+	   return (SKIP_BODY);
+    }
+
+    public int doEndTag() throws JspException {
+    	boolean valid = false;
+    	HttpSession session = pageContext.getSession();
+    	if ((session != null) && (session.getAttribute(name) != null)) {
+    	    valid = true;
+        }
+        if (valid) {
+            return (EVAL_PAGE);
+        } else {
+            ModuleConfig config =
+                (ModuleConfig) pageContext.getServletContext().getAttribute(
+                    org.apache.struts.Globals.MODULE_KEY);
+
+                try {
+                    pageContext.forward(config.getPrefix() + page);
+                } catch (ServletException e) {
+                    throw new JspException(e.toString());
+                } catch (IOException e) {
+                    throw new JspException(e.toString());
+                }
+
+            return (SKIP_PAGE);
+        }
+    }
+
+    public void release() {
+        super.release();
+        this.name = Constants.USER_KEY;
+        this.page = LOGIN_PATH;
+    }
+}</blockquote></code></pre>
+
+	<hr />
+
+		<p>As tags go, CheckLogon is quite simple. It takes no attributes. It's just hardwired to check for a User object in session scope and forward to the LogonAction if the object is missing. </p>
+
+		<p>Simple but effective. Try following the logout link and then putting in the "/MainMenu.do" address by hand. If you do, the tag will automatically forward you to the logon page. For any page that requires a login, we simply need to add a checkLogon tag at the top (before any of the page is written).</p>
+
+		<blockquote>
+		<p><font class="hint">Many Struts developers would frown on the CheckLogon tag solution. They might argue that a page should not need to know about logons. We shouldn't rely on people remembering to put a CheckLogon tag on every protected page. <em>And "they" would be right!</em> We do recommend that most applications use standard JAAS. Perhaps in the next iteration, the MailReader developers will do just that. But for now, the CheckLogon tag is a simple solution to a simple problem.</font></p>
+		</blockquote>
+
+    <p>The other links we've seen have either gone directly to a JSP file, or to a Struts action path, like Login.do. The "Edit your user registration profile" link is a little different, since it also uses a parameter, as in EditRegistration.do?action=Edit. When the Struts ActionServlet processes this link, it will ignore the parameter for the purpose of matching the request, but still pass the parameter along to action's object.</p>
+
+    <blockquote>
+      <p><i>This means that in Struts, an action object must be able to handle every valid parameter for it's base path. (In the Example, EditRegistration <b>must</b> handle both Edit and Create.)</i></p>
+
+      <p><i>You may want to check for invalid parameters too. (And be careful of differences in case if your comparisons are not case insensitive!)</i></p>
+    </blockquote>
+
+    <p>If you check the struts-config.xml, you'll see that the EditRegistration action is mapped to the (surprise again!), the EditRegistrationAction; it uses a RegistrationForm bean, and registration.jsp for input.</p>
+
+    <blockquote>
+      <p><code>&lt;!-- Registration form bean --&gt;<br />
+      &lt;form-bean name="RegistrationForm"<br />
+      type="org.apache.struts.webapp.example.RegistrationForm"/&gt;</code></p>
+
+      <p><code>&lt;!-- Edit user registration --&gt;<br />
+      &lt;action path="/EditRegistration"<br />
+      type="org.apache.struts.webapp.example.EditRegistrationAction"<br />
+      name="RegistrationForm"<br />
+      scope="request"<br />
+      validate="false"<br />
+      input="/registration.jsp"&gt;<br />
+      &lt;forward name="success" path="/registration.jsp"/&gt;<br />
+      &lt;/action&gt;</code></p>
+
+      <p><i>Hint: Consistent naming conventions, like the ones used throughout the Example, make applications much easier to write and understand. Save your creativity for the things that matter, and follow an established standard for source code formatting, like the <a href="www.amazon.com/exec/obidos/ISBN=0521777682/">Elements of Java Style</a>.</i></p>
+    </blockquote>
+
+    <h4><a name="EditRegistrationAction.java" id="EditRegistrationAction.java">EditRegistrationAction.java</a></h4>
+
+    <p>Many objects in an application may do double-duty. For example, EditRegistrationAction not only lets you update a registration, but is also used to create a new one. Which task the object performs is determined by the action passed to it. In the case of EditRegistrationAction, it can either edit or create a registration, the default being create if a task is not specified. To select between tasks, simply add ?create or ?edit to the hyperlink or form action.</p>
+
+    <blockquote>
+      <p><i>Like most classes in the example application, editRegistration makes good use of the log to track it's progress. Note that ActionServlet has had a new log method added since the Example was written. You can now specify both the message and a minimum logging (or debug) level. For more, see the Javadoc in your struts-documentation application.</i></p>
+    </blockquote>
+
+    <h3><a name="registration.jsp" id="registration.jsp">registration.jsp</a> and <a name="RegistrationForm.java" id="RegistrationForm.java">RegistrationForm.java</a></h3>
+
+    <p>If you follow the "Edit your user registration profile" link from the mainMenu, we will finally reach the heart of the Example application, the registration page. This page displays everything the Example application knows about you (or at least your login), while demonstrating several interesting techniques.</p>
+
+    <p>You'll remember that mainMenu.jsp wanted to be sure that everyone was logged in, and used the CheckLogin tag to enforce that. The registration.jsp is a little different. First it uses a Struts logic tag to see if the task at hand is to register a new User. If not (e.g. action != "Create"), the logic tag exposes a CheckLoginTag to be sure we have a user (and therefore a registration) to edit.</p>
+
+    <blockquote>
+      <p><code>&lt;logic:equal<br />
+      name="RegistrationForm"<br />
+      property="action"<br />
+      scope="request"<br />
+      value="Edit"<br />
+      &gt;<br />
+      &lt;app:checkLogon/&gt;<br />
+      &lt;/logic:equal&gt;</code></p>
+
+      <p><i>Note that the Struts html:form tag will refer to properties set by struts-config.xml and automatically create a RegistrationForm bean if one is not present. However, that does not happen until the form tag is processed within the page. Since this block appears before the html:form tag, a runtime error is exposed if you try to access registration.jsp directly (rather then going through the EditRegistration.do action).</i></p>
+    </blockquote>
+
+    <p>registation.jsp continues to use logic tags throughout the page so that a single JSP can be used to perform more than one task. For example, if you are editing the form (action == "Edit"), the page inserts your username from the RegistrationForm bean. If you are new user (action == "Create"), the page creates an empty field, so you can pick your username.</p>
+
+    <blockquote>
+      <p><i>The Struts logic tags are a very convenient way to express application logic within your pages. This prevents user error and reduces the number of JSPs your application needs to maintain, among other benefits.</i></p>
+    </blockquote>
+
+    <p>The page also uses logic tags to display a list of subscriptions for the given user. If the user enters this page with an edit action in the request context, the lower part of the page listing the subscriptions is exposed by this logic tag:</p>
+
+    <blockquote>
+      <p><code>&lt;logic:equal<br />
+      name="RegistrationForm"<br />
+      property="action"<br />
+      scope="request"<br />
+      value="Edit"<br />
+      &gt;</code></p>
+    </blockquote>
+
+    <p>Otherwise, the page just contains the top portion -- a blank data-entry form for creating the user's registration.</p>
+
+    <h4><a name="logic:iterate" id="logic:iterate">logic:iterate</a></h4>
+
+    <p>Beside making the usual conditional tests, you can also use logic tags to forward control to other actions, to redirect control to another path, and to iterate over collections. The registration page includes a good example of using the logic:iterate tag to display the user's subscriptions.</p>
+
+    <p>The subscriptions are stored in a hashtable object, which is in turn stored in the user object. So to display each subscription, we have to reach into the user object, and loop through the members of the subscription collection. Using the iterate tag, this couldn't be easier.</p>
+
+    <blockquote>
+      <p>&lt;logic:iterate name="user" property="subscriptions" id="subscription"&gt;<br />
+      &lt;!-- block to repeat --&gt;<br />
+      &lt;/logic:iterate&gt;</p>
+    </blockquote>
+
+    <p>The three parameters to the iterate tag ( name, property, and id) tell it to</p>
+
+    <ol>
+      <li>Check this context for an attribute (e.g. object) named "user",</li>
+
+      <li>Snag the property of user named "subscriptions",</li>
+
+      <li>In the block to iterate, use "subscription" (singular) as the name for each member of the collection.</li>
+    </ol>
+
+    <p>So, to list the host for each subscription in a HTML unordered list, we could write:</p>
+
+    <blockquote>
+      <p><code>&lt;ul&gt;<br />
+      &lt;logic:iterate name="user" property="subscriptions" id="subscription"&gt;<br />
+      &lt;li&gt;<br />
+      &lt;bean:write name="subscription" property="host" filter="true" /&gt;<br />
+      &lt;/li&gt;<br />
+      &lt;/logic:iterate&gt;<br />
+      &lt;/ul&gt;</code></p>
+
+      <p><i>This is another good example of how Struts works with the standard JSP tags, like bean. The filter option says to use convert HTML commands to their character entity. So a &lt; would be output in the HTML as &amp;lt;.</i></p>
+    </blockquote>
+
+    <p>In registration.jsp, iterate is used to create a menu of subscriptions, each linked with an edit and delete action.</p>
+
+    <blockquote>
+      <p><code>&lt;logic:iterate id="subscription" name="user" property="subscriptions"&gt;<br />
+      &lt;tr&gt;<br />
+      &lt;td align="left"&gt;<br />
+      &lt;bean:write name="subscription" property="host" filter="true"/&gt;<br />
+      &lt;/td&gt;<br />
+      &lt;td align="left"&gt;<br />
+      &lt;bean:write name="subscription" property="username" filter="true"/&gt;<br />
+      &lt;/td&gt;<br />
+      &lt;td align="center"&gt;<br />
+      &lt;bean:write name="subscription" property="type" filter="true"/&gt;<br />
+      &lt;/td&gt;<br />
+      &lt;td align="center"&gt;<br />
+      &lt;bean:write name="subscription" property="autoConnect"/&gt;<br />
+      &lt;/td&gt;<br />
+      &lt;td align="center"&gt;<br />
+      &lt;app:linkSubscription page="/EditSubscription.do?action=Delete"&gt;<br />
+      &lt;bean:message key="registration.deleteSubscription"/&gt;<br />
+      &lt;/app:linkSubscription&gt;<br />
+      &lt;app:linkSubscription page="/EditSubscription.do?action=Edit"&gt;<br />
+      &lt;bean:message key="registration.EditSubscription"/&gt;<br />
+      &lt;/app:linkSubscription&gt;<br />
+      &lt;/td&gt;<br />
+      &lt;/tr&gt;<br />
+      &lt;/logic:iterate&gt;</code></p>
+
+      <p><i>The collection in an iterate tag can be any of the following: an array of objects, an Iterator, a Collection (which includes Lists, Sets and Vectors), or a Map (which includes Hashtables) whose elements will be iterated over.</i></p>
+    </blockquote>
+
+    <p>You'll note that the hyperlinks to the edit and delete action for each subscription are written with another custom tag, app:linkSubscription. Writing a hyperlink to an action is not difficult, but it can be ugly, and makes an excellent case for encapsulation. If you trace through the app.tld, you will find that the source code for the linkSubscription tag lives in (<i>come on, take a guess</i>) LinkSubscriptionTag.java.</p>
+
+    <h4><a name="LinkSubscriptionTag.java" id="LinkSubscriptionTag.java">LinkSubscriptionTag.java</a></h4>
+
+    <p>The Example application uses a subscription's host name (e.g. yahoo.com) as a primary key, which means you can only have one subscription for each host. It also means that to edit a subscription, all you need to know is the user and host. In fact, the EditSubscription action is designed to create, edit, or delete a subscription if provided a user and host names in the request. The goal of LinkSubscriptionTag is then to output a block like:</p>
+
+    <blockquote>
+      <p><code>&lt;A HREF=[path]EditSubscription.do?action=[action]&amp;username=[user]&amp;host=[host]"&gt;[action]<br />
+      &lt;/A&gt;</code></p>
+    </blockquote>
+
+    <p>based on input block like:</p>
+
+    <blockquote>
+      <p><code>&lt;app:linkSubscription<br />
+      page="/EditSubscription.do?action=Delete"&gt;Delete<br />
+      &lt;/app:linkSubscription&gt;</code></p>
+    </blockquote>
+
+    <p>To reduce overhead, LinkSubscriptionTag uses "subscription" as the default name (which the iterator refers to as "ID"), so that can be omitted from the tag properties. The "action" portion of the will differ, and so that is given as the page property to the tag</p>
+
+    <p>Here are a few annotated highlights from LinkSubscriptionTag.java:</p>
+
+    <ol>
+      <li><i>Create a string buffer, and ask the request for the relative path to the application</i><br />
+      <code>StringBuffer url = new StringBuffer(request.getContextPath());</code></li>
+
+      <li><i>Snag a reference to the subscription bean (for this iteration)<br /></i> <code>subscription = (Subscription) pageContext.findAttribute(name);</code></li>
+
+      <li><i>Append the username and host from the bean to the path request.<br /></i> <code>url.append("&amp;username="); url.append(BeanUtils.filter(subscription.getUser().getUsername()));<br />
+      url.append("&amp;host=");<br />
+      url.append(BeanUtils.filter(subscription.getHost()));</code></li>
+    </ol>
+
+    <p>These are the essentials, but be sure to see the full source in LinkSubscriptionTag.java for the rest of the error and logic checking that a working application needs to succeed.</p>
+
+    <p>Meanwhile, back on registration.jsp, there is one more link on the page. This uses yet another custom tag, the app:linkUser tag.</p>
+
+    <blockquote>
+      <p><code>&lt;app:linkUser page="/EditSubscription.do?action=Create"&gt;<br />
+      &lt;bean:message key="registration.addSubscription"/&gt;<br />
+      &lt;/app:linkUser&gt;</code></p>
+    </blockquote>
+
+    <p>By this time, you must be ready to flip directly to LinkUserTag.java with nary a glance at the configuration file ...</p>
+
+    <h4><a name="LinkUserTag.java" id="LinkUserTag.java">LinkUserTag.java</a></h4>
+
+    <p>Since they solve the same general problem, LinkUserTag and LinkSubscriptionTag are quite a bit a like, except that LinkUserTag grabs the user bean from the session context, instead of a subscription bean from the iteration. Like the LinkSubscriptionTag, the name for the user bean (e.g. "user") is defaulted, and can be omitted from the tag; all that's needed is the page property -- the rest is automatic!</p>
+
+    <blockquote>
+      <p><code>&lt;app:linkUser page="/EditSubscription.do?action=Create"&gt;<br />
+      &lt;bean:message key="registration.addSubscription"/&gt;<br />
+      &lt;/app:linkUser&gt;</code></p>
+    </blockquote>
+
+    <p>When rendered, this displays a HTML hypertext link like:</p>
+
+    <blockquote>
+      <p><code>&lt;a href="/struts-example/EditSubscription.do?action=Create&amp;amp;username=user"&gt;<br />
+      Add<br />
+      &lt;/a&gt;</code></p>
+
+      <p><i>Note that anchor links with ampersands should use the character entity &amp;amp; as the LinkUserTag has done here (<a href="http://www.w3.org/TR/html401/appendix/notes.html#h-B.2.2">http://www.w3.org/TR/html401/appendix/notes.html#h-B.2.2</a>).</i></p>
+    </blockquote>
+
+    <p>Let's follow that "Add" link now and see what's up with the EditSubcription action anyway.</p>
+
+    <h4><a name="EditSubscriptionAction.java" id="EditSubscriptionAction.java">EditSubscriptionAction.java</a></h4>
+
+    <p>Predictably, we find a some now-familiar mappings in struts-config.xml</p>
+
+    <blockquote>
+      <p><code>&lt;!-- Subscription form bean --&gt;<br />
+      &lt;form-bean<br />
+      name="SubscriptionForm"<br />
+      type="org.apache.struts.webapp.example.SubscriptionForm"<br />
+      /&gt;</code></p>
+
+      <p><code>&lt;!-- Edit mail subscription --&gt;<br />
+      &lt;action path="/EditSubscription"<br />
+      type="org.apache.struts.webapp.example.EditSubscriptionAction"<br />
+      name="SubscriptionForm"<br />
+      scope="request"<br />
+      validate="false"<br />
+      &gt;<br />
+      &lt;forward name="failure" path="/mainMenu.jsp"/&gt;<br />
+      &lt;forward name="success" path="/subscription.jsp"/&gt;<br />
+      &lt;/action&gt;</code></p>
+
+      <p><i>When we've introduced these type of mappings before, and mentioned that the struts-config.xml was parsed when the ActionServlet was initialized. But we should make it clear that when the Struts digester parsed this file, it actually created standard Java objects, linked as properties to the controller. This means you don't have to edit Java source files just to add a bunch of "new" statements. (How cool is that?)</i></p>
+    </blockquote>
+
+    <p>Following what was specified by struts-config.xml, the controller makes sure that a SubscriptionForm bean exists, along with the SubscriptionAction object, and then calls the action object's perform method. The perform method first checks to see that the user is logged-in. If not, control is forwarded to the Login action. EditSubscriptionAction.perform then either creates a new subscription object (if the task is Create), or searches the user's subscription hashtable for a matching hostname (if the task is Edit).</p>
+
+    <p>Finally, EditSubscriptionAction conforms the ActionForm bean with the database bean. There may be several subscriptions in the database, but in EditSubscriptionAction we expose the one selected (or just created) for this request to use. Once the Action form (called "subform" in the code) is created and populated from the database, the bean's action is set to either Create or Edit, and control is forwarded to our "success" form, subscription.jsp .</p>
+
+    <blockquote>
+      <p><i>Note that the servlet only creates one object for each action. Each request is handled as a separate thread, and passed to the single action object instance. This means your action objects must be multi-thread safe.</i></p>
+    </blockquote>
+
+    <p>But before turning to our final JSP, a word about our database model ...</p>
+
+    <h4><a name="User.java" id="User.java">User.java</a> and <a name="Subscription.java" id="Subscription.java">Subscription.java</a></h4>
+
+    <p>If you're used to working with relational databases, the links between the user and subscription objects may be confusing. A conventional relational database would create two distinct tables, one for the users and another for the subscriptions, and link them together with a user ID. The Example application implements a different model, a hierarchical database. Here a "table" of subscriptions is stored within each user object, something like the way a filing system stores documents within folders.</p>
+
+    <p>In addition to the usual getters and setters, the user object also has two methods for working with subscription objects. findSubscription takes a hostname and returns the subscription object for that host. getSubscriptions returns an array of all the subscriptions for the user (ready-made for the iterate tag!). Besides the fields needed to manage the SubscriptionForm data, the object also maintains a runtime link to its user object.</p>
+
+    <p>To create a new subscription, EditSubscriptionAction.java simply creates a new subscription object, and sets its user to the object found in the request, and then forwards control to its input form, subscription.jsp.</p>
+
+    <h3><a name="subcription.jsp" id="subcription.jsp">subscription.jsp</a></h3>Saving the best for last, subscription.jsp demonstrates use of some interesting Struts custom form tags, html:options and html:checkbox.
+
+    <p>In registration.jsp, the Struts iteration tag was used to write a list of subscriptions. Another place where iterations and collections are handy is the option list for a HTML select tag. Since this is such a common situation, Struts offers a html:options (plural) tag can take an array of objects as a parameter. The tag then iterates over the members of the array (beans) to place each one inside an standard option tag. So given a block like</p>
+
+    <blockquote>
+      <p><code>&lt;html:select property="type"&gt;<br />
+      &lt;html:options<br />
+      collection="serverTypes"<br />
+      property="value"<br />
+      labelProperty="label"<br />
+      /&gt;<br />
+      &lt;/html:select&gt;</code></p>
+    </blockquote>
+
+    <p>Struts outputs a block like</p>
+
+    <blockquote>
+      <p><code>&lt;select name="type"&gt;<br />
+      &lt;option value="imap" selected&gt;IMAP Protocol&lt;/option&gt;<br />
+      &lt;option value="pop3"&gt;POP3 Protocol&lt;/option&gt;<br />
+      &lt;/select&gt;</code></p>
+    </blockquote>
+
+    <p>Here, one collection contained both the labels and the values, from properties of the same name. Options can also use a second array for the labels, if they do not match the values. Options can use a Collection, Iterator, or Map for the source of the list.</p>
+
+    <p>For demonstrations purposes, the serverTypes array is created at the top of this page. Usually, the html:options tag would be used to list valid items from a database maintained elsewhere. For example, if the application needed you to select a default subscription, a form might list the subscriptions in an options tag.</p>
+
+    <blockquote>
+      <p><i>The LabelValueBean used to create the demonstration array is also a good example of simple but useful bean object.</i></p>
+    </blockquote>
+
+    <p>A particularly tricky HTML control is the checkbox. A problem with a checkbox is that it is only sent in the request if it is checked. If it is not checked, it is not sent (i.e. null). This can be problematic when trying to validate the form's data after it has been translated to a bean. The autoconnect property for a subscription demonstrates how to handle validation of a checkbox.</p>
+
+    <h4><a name="SubscriptionForm.java" id="SubscriptionForm.java">SubscriptionForm.java</a></h4>
+
+    <p>Struts validation is handled by the reset and validate methods of the ActionForm bean. When creating your own form beans, you should subclass ActionForm, add your own fields and their getters/setters, and implement the reset and validate methods.</p>
+
+    <p>Struts calls reset before populating the form, and calls validate after populating it but before the perform method of the action. Reset should assign default values to each of your form fields, usually null. But in the case of checkboxes, the default value should usually be false instead of null.</p>
+
+    <blockquote>
+      <p><i>For more examples of validating forms, take another look at LoginForm.java and RegistrationForm.java.</i></p>
+    </blockquote>
+
+    <p>Back in subscription.jsp, we have one more block to cover. Although the same basic form can be used to created, edit, or delete a subscription, people might expect the buttons to be labeled differently in each case. subscription.jsp accommodates by using a logic tag to output a different set of buttons for each case. This doesn't really change the way subscription.jsp works, but it does make things less confusing for the user.</p>
+
+    <blockquote>
+      <p><code>&lt;logic:equal<br />
+      name="SubscriptionForm"<br />
+      property="action"<br />
+      scope="request"<br />
+      value="Create"&gt;<br />
+      &lt;html:submit&gt;<br />
+      <b>&lt;bean:message key="button.save"/&gt;<br /></b> &lt;/html:submit&gt;<br />
+      &lt;/logic:equal&gt;</code></p>
+    </blockquote>
+
+    <p>In the case of a request to delete a subscription, the submit button is labeled "Confirm", since this view is meant to give the user a last chance to cancel, before sending that task along to SaveSubscriptionAction.java.</p>
+
+    <p>The actual action property is placed into the form as a hidden field, and SaveSubscriptionAction checks that property to execute the appropriate task.</p>
+
+    <h4><a name="SaveSubscriptionAction.java" id="SaveSubscriptionAction.java">SaveSubscriptionAction.java</a></h4>
+
+    <p>Our final stop has the job of finishing what EditSubscriptionAction.java and subscription.jsp started. After the usual logic and error checking, SaveSubscriptionAction either deletes or updates the subscription object being handled by this request, and cleans up the bean, just to be tidy. By now, you should be very comfortable reading through the source on your own, to pickup the finer points.</p>
+
+    <p>This concludes our tour. To review, you may wish to trace the path a new user takes when they register with the application for the first time. You should also read over each of the .java and JSP files carefully, since we only covered the high points here.</p>
+
+    <h3><a name="Summary" id="Summary">Summary</a></h3>
+
+    <ul>
+      <li>Struts uses a single controller servlet to route HTTP requests.</li>
+
+      <li>The requests are routed to action objects according to path (or URI).</li>
+
+      <li>Each request is handled as a separate thread</li>
+
+      <li>There is only one object for each action (URI), so your action objects must be multi-thread safe.</li>
+
+      <li>The configuration of action objects are loaded from a XML resource file, rather than hardcoded.</li>
+
+      <li>Action objects can respond to the request, or ask the controller to forward the request to another object or to another page, such as an input form.</li>
+
+      <li>A library of custom tags works with the rest of the framework to enhance use of JavaServer Pages.</li>
+
+      <li>The Struts form tag can work closely with an action objects via a Struts ActionFormBean to retain the state of a data-entry form, and validate the data entered.</li>
+
+      <li>ActionForm beans can be automatically created by the JSP form or controller servlet.</li>
+
+      <li>Struts supports a message resource for loading constants strings. Alternate message resources can be provided to internationalize an application.</li>
+    </ul>
+    <hr />
+  </blockquote>
+</body>
+</html>

Added: struts/bsf/trunk/src/struts-example/welcome.jsp
URL: http://svn.apache.org/viewcvs/struts/bsf/trunk/src/struts-example/welcome.jsp?rev=329127&view=auto
==============================================================================
--- struts/bsf/trunk/src/struts-example/welcome.jsp (added)
+++ struts/bsf/trunk/src/struts-example/welcome.jsp Thu Oct 27 23:29:07 2005
@@ -0,0 +1,31 @@
+<%@ page contentType="text/html;charset=UTF-8" language="java" %>
+<%@ taglib uri="/tags/struts-bean" prefix="bean" %>
+<%@ taglib uri="/tags/struts-html" prefix="html" %>
+
+<html>
+<head>
+<title><bean:message key="index.title"/></title>
+<link rel="stylesheet" type="text/css" href="base.css" />
+</head>
+
+<h3><bean:message key="index.heading"/></h3>
+<ul>
+<li><html:link action="/EditRegistration?action=Create"><bean:message key="index.registration"/></html:link></li>
+<li><html:link action="/Logon"><bean:message key="index.logon"/></html:link></li>
+</ul>
+
+<h3>Language Options</h3>
+<ul>
+<li><html:link action="/Locale?language=en">English</html:link></li>
+<li><html:link action="/Locale?language=ja" useLocalEncoding="true">Japanese</html:link></li>
+<li><html:link action="/Locale?language=ru" useLocalEncoding="true">Russian</html:link></li>
+</ul>
+
+<hr />
+
+<p><html:img bundle="alternate" pageKey="struts.logo.path" altKey="struts.logo.alt"/></p>
+
+<p><html:link action="/Tour"><bean:message key="index.tour"/></html:link></p>
+
+</body>
+</html>



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org