blob: f04e3d1e9faaafb6a56559d6f9dcec20eb0a71ca [file] [log] [blame]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="description" content="A brief guide to creating a web application
using the Struts framework in NetBeans IDE">
<meta name="keywords" content="NetBeans, IDE, integrated development environment,
Struts, web framework, open source">
<link rel="stylesheet" type="text/css" href="../../../netbeans.css">
<link rel="stylesheet" type="text/css" href="../../../lytebox.css" media="screen">
<script type="text/javascript" src="../../../images_www/js/lytebox-compressed.js"></script>
<title>Struts Web 框架简介 - NetBeans IDE 教程</title>
</head>
<body>
<!-- Copyright (c) 2009, 2010, 2011, Oracle and/or its affiliates. All rights reserved. -->
<h1>Struts Web 框架简介</h1>
<p>本文档介绍了使用 Struts Web 框架在 NetBeans IDE 中开发 Web 应用程序的基础知识。Struts 是一个开源框架,用于扩展 Java Servlet API 并使用模型、视图、控制器(MVC) 体系结构。通过此框架,您可以基于各种标准技术(如 JSP 页、JavaBeans、资源包和 XML)创建可维护、可扩展并且灵活的 Web 应用程序。</p>
<p>本教程介绍如何构建一个简单的 MVC 应用程序,该程序可以显示登录页并在提交的数据通过验证时返回成功页。您将了解 Struts 提供的一些基本功能,以及这些功能是如何使用 IDE 实现的。具体说来,您将在 JSP 页中使用 Struts 标记,使用 Struts <code>ActionForm</code> Bean 维护用户数据,以及使用 Struts <code>Action</code> 对象实现转发逻辑。您还将学到如何对应用程序执行简单验证,包括为失败的登录尝试设置警告消息。</p>
<p>有关 Struts 更加详细的介绍,请参见官方 <a href="http://struts.apache.org/">Struts</a> Web 站点上的 <a href="http://struts.apache.org/struts-action/faqs/works.html">How does Struts work?</a>(Struts 的工作原理)。还可以使用 IDE 的 Javadoc 索引搜索("Help"(帮助)> "Javadoc Index Search"(Javadoc 索引搜索))查看 <a href="http://struts.apache.org/release/1.3.x/apidocs/index.html">Struts 框架 API</a>(与 Struts 库打包在一起)。</p>
<p><strong>目录</strong></p>
<img alt="此页上的内容适用于 NetBeans IDE 7.2、7.3、7.4 和 8.0" class="stamp" src="../../../images_www/articles/73/netbeans-stamp-80-74-73.png" title="此页上的内容适用于 NetBeans IDE 7.2、7.3、7.4 和 8.0">
<ul class="toc">
<li><a href="#overview">应用程序概述</a></li>
<li><a href="#set">设置 Struts 应用程序</a></li>
<li><a href="#jsp">创建 JSP 页</a>
<ul>
<li><a href="#login">创建登录页</a></li>
<li><a href="#success">创建成功页</a></li>
</ul></li>
<li><a href="#actionForm">创建 <code>ActionForm</code> Bean</a></li>
<li><a href="#actionClass">创建 <code>Action</code></a></li>
<li><a href="#validate">实现验证</a>
<ul>
<li><a href="#beanData">访问 Bean 数据并准备转发条件</a></li>
<li><a href="#errorMsg">设置错误消息</a></li>
</ul></li>
<li><a href="#forward"><code>forward</code> 条目添加到 <code>struts-config.xml</code></a></li>
<li><a href="#configure">配置和运行应用程序</a>
<ul>
<li><a href="#welcome">设置欢迎页</a></li>
<li><a href="#style">附加样式表</a></li>
<li><a href="#run">运行应用程序</a></li>
</ul></li>
<li><a href="#seeAlso">另请参见</a></li>
</ul>
<p><strong>要学完本教程,您需要具备以下软件和资源。</strong></p>
<table>
<tbody>
<tr>
<th class="tblheader" scope="col">软件或资源</th>
<th class="tblheader" scope="col">要求的版本</th>
</tr>
<tr>
<td class="tbltd1"><a href="https://netbeans.org/downloads/index.html">NetBeans IDE</a></td>
<td class="tbltd1">7.2、7.3、7.4、8.0、Java EE</td>
</tr>
<tr>
<td class="tbltd1"><a href="http://www.oracle.com/technetwork/java/javase/downloads/index.html">Java 开发工具包 (JDK)</a></td>
<td class="tbltd1">7 或 8</td>
</tr>
<tr>
<td class="tbltd1">GlassFish Server <br><em class="indent margin-around"></em> <br>Tomcat Servlet 容器</td>
<td class="tbltd1">Open Source Edition 3.x 或 4.0 <br><em class="margin-around indent"> </em> <br>版本 7.x 或 8.x</td>
</tr>
</tbody>
</table>
<p><strong class="notes">注:</strong></p>
<ul>
<li>Java 安装使您能够选择性地安装 GlassFish Server 和 Apache Tomcat Servlet 容器。必须安装其中一个(或在 IDE 中注册其他服务器)才能学完本教程。</li>
<li>如果需要将项目与工作解决方案进行比较,可以<a href="https://netbeans.org/projects/samples/downloads/download/Samples%252FJava%2520Web%252FMyStrutsApp.zip">下载样例应用程序</a></li>
</ul>
<a name="overview"></a>
<h2>应用程序概述</h2>
<p>使用 Struts 时,此框架将为您提供一个控制器 Servlet (<code>ActionServlet</code>),它在 IDE 中包含的 Struts 库中定义,并自动在 <code>web.xml</code> 部署描述符中注册,<a href="#controllerServlet">如下所示</a>。该控制器 Servlet 使用 <code>struts-config.xml</code> 文件将传入请求映射到 Struts <code>Action</code> 对象,并实例化与暂时存储窗体数据的操作相关联的任何 <code>ActionForm</code> 对象。<code>Action</code> 对象在使用窗体 Bean 中存储的任何数据的同时,使用其 <code>execute</code> 方法处理请求。一旦 <code>Action</code> 对象处理了请求,它就将存储任何新数据(即,存储在窗体 Bean 或单独的结果 Bean 中),并将结果转发到相应的视图中。</p>
<div class="indent">
<img alt="Struts 工作流" class="margin-around" src="../../../images_www/articles/72/web/struts/workflow.png" title="Struts 工作流">
</div>
<p>在 NetBeans IDE 中开发 Struts 应用程序与开发任何其他类型的 Web 应用程序类似。但是,您要使用 IDE 提供的 Struts 支持来补充 Web 开发工具包。例如,在 IDE 中使用模板创建 Struts <code>Action</code> 对象和 <code>ActionForm</code> Bean。创建后,IDE 会自动在 <code>struts-config.xml</code> 文件中注册这些类,并让您使用源代码编辑器右键单击菜单中的菜单项非常轻松地扩展此文件。由于许多 Web 应用程序将 JSP 页用于视图,因此 Struts 还提供了定制标记库,以便于与 HTML 窗体进行交互。在 IDE 的源代码编辑器中,您可以调用代码完成功能和 Javadoc 支持,以帮助有效地使用这些库。</p>
<p>以下步骤说明了如何创建收集用户数据的简单窗体、执行简单验证以及在成功页上输出数据。</p>
<a name="set"></a>
<h2>设置 Struts 应用程序</h2>
<p>在 IDE 中,Struts 只是一个附带 Struts 库和配置文件的普通 Web 应用程序。在 IDE 中,您可以像创建其他任何 Web 应用程序那样,使用新建 Web 应用程序向导创建 Struts 应用程序,此外,还需要一个表明希望将 Struts 库和配置文件包含在应用程序中的步骤。</p>
<ol>
<li>从主菜单中,选择 "File"(文件)> "New Project"(新建项目)(Ctrl-Shift-N 组合键;在 Mac 上为 ⌘-Shift-N 组合键)。在 "Categories"(类别)列表中选择 "Java Web",然后在 "Projects"(项目)列表中选择 "Web Application"(Web 应用程序)。单击 "Next"(下一步)。</li>
<li>在 "Name and Location"(名称和位置)面板中,输入 <code>MyStrutsApp</code> 作为项目名称,然后单击 "Next"(下一步)。</li>
<li>在 "Server and Settings"(服务器和设置)面板中,选择您要将应用程序部署到的服务器。这里仅列出了已在 IDE 中注册的服务器。(要注册服务器,请单击 "Server"(服务器)下拉列表旁边的 "Add"(添加)。)另请注意,所部署应用程序的上下文路径是 <code>/MyStrutsApp</code>。单击 "Next"(下一步)。</li>
<li>在 "Frameworks"(框架)面板中选择 "Struts"。<br> <img alt="显示在新建 Web 应用程序向导的 &quot;Frameworks&quot;(框架)面板中的 Struts 选项" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/new-project-wizard.png" title="Struts 选项显示在新建 Web 应用程序向导的 &quot;Frameworks&quot;(框架)面板中">
<p>出于本教程的目的,请勿更改此面板下部区域中的任何配置值。向导会显示以下配置选项。</p>
<ul>
<li><strong>Action Servlet Name(操作 Servlet 名称)</strong>:应用程序中使用的 Struts 操作 Servlet 的名称。<code>web.xml</code> 部署描述符包含操作 Servlet 条目,并指定相应的特定于 Struts 的参数(如 Struts 库中 Servlet 类的路径,以及应用程序中 <code>struts-config.xml</code> 配置文件的路径)。</li>
<li><strong>Action URL Pattern(操作 URL 模式)</strong>:指定映射到 Struts 操作控制器的传入请求模式。这会在部署描述符中生成一个映射条目。默认情况下,只映射 <code>*.do</code> 模式。</li>
<li><strong>Application Resource(应用程序资源)</strong>:用于指定将在 <code>struts-config.xml</code> 文件中用来本地化消息的资源包。默认情况下,它是 <code>com.myapp.struts.ApplicationResource</code></li>
<li><strong>Add Struts TLDs(添加 Struts TLD)</strong>:用于生成 Struts 标记库的标记库描述符。标记库描述符是一个 XML 文档,其中包括有关整个标记库以及每个单独标记的附加信息。通常,此操作是不必要的,因为您可以参阅联机 URI,而不是本地 TLD 文件。</li>
</ul></li>
<li>单击 "Finish"(完成)。IDE 将在您的文件系统中创建项目文件夹。与 IDE 中的任何 Web 应用程序一样,此项目文件夹中包含所有的源代码和 IDE 的项目元数据(如 Ant 构建脚本)。但是除此之外,您的 Web 应用程序在其类路径上还具有所有的 Struts 库。这些 Struts 库不仅位于应用程序的类路径上,而且还包含在项目中,并且在稍后构建项目时将与项目打包在一起。</li>
</ol>
<p>此项目在 IDE 中打开。"Projects"(项目)窗口是项目源的主入口点。它显示重要项目内容的逻辑视图。例如,如果展开新项目中的若干节点,则项目可能显示如下:</p>
<div class="indent">
<img alt="包含新创建的项目的 &quot;Projects&quot;(项目)窗口" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/proj-window-init.png" title="&quot;Projects&quot;(项目)窗口将显示 MyStrutsApp 项目">
</div>
<p><strong class="notes">注:</strong>使用 "Files"(文件)窗口("Window"(窗口)> Files"(文件))可以通过基于目录的视图来查看所有项目内容。</p>
<p><a name="controllerServlet"></a>特定于 Struts 的配置文件以及应用程序的部署描述符就放置在 "Configuration Files"(配置文件)文件夹中。打开部署描述符(双击 <code>web.xml</code> 文件节点使其显示在源代码编辑器中)。为了进行 Struts 处理,为 Struts 控制器 Servlet 提供了一个映射。</p>
<pre class="examplecode">
&lt;servlet&gt;
&lt;servlet-name&gt;action&lt;/servlet-name&gt;
&lt;servlet-class&gt;org.apache.struts.action.ActionServlet&lt;/servlet-class&gt;
&lt;init-param&gt;
&lt;param-name&gt;config&lt;/param-name&gt;
&lt;param-value&gt;/WEB-INF/struts-config.xml&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;init-param&gt;
&lt;param-name&gt;debug&lt;/param-name&gt;
&lt;param-value&gt;2&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;init-param&gt;
&lt;param-name&gt;detail&lt;/param-name&gt;
&lt;param-value&gt;2&lt;/param-value&gt;
&lt;/init-param&gt;
&lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
&lt;/servlet&gt;
&lt;servlet-mapping&gt;
&lt;servlet-name&gt;action&lt;/servlet-name&gt;
&lt;url-pattern&gt;*.do&lt;/url-pattern&gt;
&lt;/servlet-mapping&gt;</pre>
<p>在以上代码中,Struts 控制器 Servlet 名为 <code>action</code>,并在 Struts 库 (<code>org.apache.struts.action.ActionServlet</code>) 中定义。将其设置为处理所有满足 <code>*.do</code> 映射的请求。此外,Servlet 的初始化参数通过 <code>struts-config.xml</code> 文件指定,同时包含在 <code>WEB-INF</code> 文件夹中。</p>
<a name="jsp"></a>
<h2>创建 JSP 页</h2>
<p>首先为应用程序创建两个 JSP 页。第一个页面中显示一个窗体。第二个页面是登录成功时返回的视图。</p>
<ul>
<li><a href="#login">创建登录页</a></li>
<li><a href="#success">创建成功页</a></li>
</ul>
<div class="indent">
<a name="login"></a>
<h3>创建登录页</h3>
<ol>
<li>右键单击 <code>MyStrutsApp</code> 项目节点,选择 "New"(新建)> "JSP",然后将新文件命名为 <code>login</code>。单击 "Finish"(完成)。此时将在源代码编辑器中打开 <code>login.jsp</code> 文件。</li>
<li>在源代码编辑器中,将 <code>&lt;title&gt;</code><code>&lt;h1&gt;</code> 标记(或 <code>&lt;h2&gt;</code> 标记,取决于您使用的 IDE 版本)的内容更改为 <code>Login Form</code></li>
<li>将以下两个 taglib 指令添加到文件顶部:
<pre class="examplecode">
&lt;%@ taglib uri=&quot;http://struts.apache.org/tags-bean&quot; prefix=&quot;bean&quot; %&gt;
&lt;%@ taglib uri=&quot;http://struts.apache.org/tags-html&quot; prefix=&quot;html&quot; %&gt;</pre>
<p>许多 Web 应用程序将 JSP 页用于 MVC 范例中的视图,因此 Struts 提供定制标记库,以便于与 HTML 窗体进行交互。通过使用 IDE 的代码完成支持,这些库可以轻松地应用到 JSP 文件。在源代码编辑器中键入代码时,IDE 会提供 Struts 标记的代码完成以及 Struts Javadoc。您还可以通过按 Ctrl-空格键,手动调用代码完成功能:</p>
<img alt="源代码编辑器中提供的 Struts 标记的代码完成和 Javadoc" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/code-completion.png" title="为 Struts 标记提供代码完成和 Javadoc">
<p><a href="http://struts.apache.org/release/1.3.x/struts-taglib/dev_bean.html">Bean 标记库</a>为您提供了大量标记,在将窗体 Bean(即 <code>ActionForm</code> Bean)与从窗体中收集的数据关联时,这些标记非常有用。<a href="http://struts.apache.org/release/1.3.x/struts-taglib/dev_html.html">html 标记库</a>在视图和 Web 应用程序所必需的其他组件之间提供了一个接口。例如,下面您将使用 Struts 的 <code>&lt;html:form&gt;</code> 标记替换常用的 html <code>form</code> 标记。这样做的一个好处是使服务器查找或创建一个 Bean 对象,该对象对应于为 <code>html:form</code><code>action</code> 元素提供的值。</p></li>
<li><code>&lt;h1&gt;</code>(或 <code>&lt;h2&gt;</code>)标记下,添加以下代码:
<pre class="examplecode">
&lt;html:form action=&quot;/login&quot;&gt;
&lt;html:submit value=&quot;Login&quot; /&gt;
&lt;/html:form&gt;</pre>
<p class="tips">无论何时在源代码编辑器中完成键入,都可以通过右键单击并选择 "Format"(格式化)(Alt-Shift-F 组合键)来整理代码。</p></li>
<li>在 IDE 右侧区域的组件面板("Window"(窗口)> "Palette"(组件面板))中,将 "Table"(表)项从 "HTML" 类别拖动至 <code>&lt;html:submit value="Login" /></code> 行上方的某个位置。将显示 "Insert Table"(插入表格)对话框。将行设置为 <code>3</code>,列设置为 <code>2</code>,其他所有设置都保留为 <code>0</code>。在本教程中,稍后您将<a href="#style">附加样式表</a>来影响表的显示。<br> <img alt="源代码编辑器中显示的 &quot;Create Table&quot;(创建表)对话框" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/insert-table.png" title="&quot;Palette&quot;(组件面板)为易于使用的代码模板提供对话框"> <br> 单击 "OK"(确定),然后可以选择重新设置代码格式(Alt-Shift-F 组合键)。现在,<code>login.jsp</code> 中的窗体显示如下:
<pre class="examplecode">
&lt;html:form action=&quot;/login&quot;&gt;
&lt;table border=&quot;0&quot;&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;html:submit value=&quot;Login&quot; /&gt;
&lt;/html:form&gt;</pre>
<p><strong class="notes">注:</strong>可以安全地删除 <code>&lt;thead&gt;</code> 表行,因为它不会在本教程中使用。</p></li>
<li>在第一个表行中,输入以下内容(更改的内容以<strong>粗体</strong>显示):
<pre class="examplecode">
&lt;tr&gt;
&lt;td&gt;<strong>Enter your name:</strong>&lt;/td&gt;
&lt;td&gt;<strong>&lt;html:text property=&quot;name&quot; /&gt;</strong>&lt;/td&gt;
&lt;/tr&gt;</pre></li>
<li>在第二个表行中,输入以下内容(更改的内容以<strong>粗体</strong>显示):
<pre class="examplecode">
&lt;tr&gt;
&lt;td&gt;<strong>Enter your email:</strong>&lt;/td&gt;
&lt;td&gt;<strong>&lt;html:text property=&quot;email&quot; /&gt;</strong>&lt;/td&gt;
&lt;/tr&gt;</pre>
通过 <code>html:text</code> 元素,您可以将此窗体中的输入字段与下一步中将创建的窗体 Bean 中的属性相匹配。例如,<code>property</code> 的值必须与此窗体关联的窗体 Bean 中声明的字段相匹配。</li>
<li>将 &lt;html:submit value="Login" /&gt; 元素移动到第三个表行的第二列,以便第三个表行如下所示(更改的内容以<strong>粗体</strong>显示):
<pre class="examplecode">
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;<strong>&lt;html:submit value=&quot;Login&quot; /&gt;</strong>&lt;/td&gt;
&lt;/tr&gt;</pre>
</li>
</ol>
<p>在此阶段,您的登录窗体会显示如下:</p>
<pre class="examplecode">
&lt;html:form action=&quot;/login&quot;&gt;
&lt;table border=&quot;0&quot;&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Enter your name:&lt;/td&gt;
&lt;td&gt;&lt;html:text property=&quot;name&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Enter your email:&lt;/td&gt;
&lt;td&gt;&lt;html:text property=&quot;email&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;html:submit value=&quot;Login&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;
&lt;/html:form&gt;</pre>
<a name="success"></a>
<h3>创建成功页</h3>
<ol>
<li>右键单击 <code>MyStrutsApp</code> 项目节点,选择 "New"(新建)> "JSP",然后将新文件命名为 <code>success</code>。在 "Folder"(文件夹)字段中,单击邻近的 "Browse"(浏览)按钮,然后从显示的对话框中选择 <code>WEB-INF</code>。单击 "Select Folder"(选择文件夹)以在 "Folder"(文件夹)字段中输入 WEB-INF。客户端请求不能直接访问 WEB-INF 文件夹中包含的任何文件。为了能够正常显示 <code>success.jsp</code>,它必须包含处理过的数据。单击 "Finish"(完成)。</li>
<li>在源代码编辑器中,将新创建页面中的内容更改为以下内容:
<pre class="examplecode">
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;Login Success&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;Congratulations!&lt;/h1&gt;
&lt;p&gt;You have successfully logged in.&lt;/p&gt;
&lt;p&gt;Your name is: .&lt;/p&gt;
&lt;p&gt;Your email address is: .&lt;/p&gt;
&lt;/body&gt;</pre></li>
<li><a href="http://struts.apache.org/release/1.3.x/struts-taglib/dev_bean.html">Bean 标记库</a>指令添加到文件顶部:
<pre class="examplecode">
&lt;%@ taglib uri=&quot;http://struts.apache.org/tags-bean&quot; prefix=&quot;bean&quot; %&gt;
</pre></li>
<li>添加以下 <code>&lt;bean:write&gt;</code> 标记(更改的内容以<strong>粗体</strong>显示):
<pre class="examplecode">
&lt;p&gt;Your name is: <strong>&lt;bean:write name=&quot;LoginForm&quot; property=&quot;name&quot; /&gt;</strong>.&lt;/p&gt;
&lt;p&gt;Your email address is: <strong>&lt;bean:write name=&quot;LoginForm&quot; property=&quot;email&quot; /&gt;</strong>.&lt;/p&gt;
</pre>
通过使用 <code>&lt;bean:write&gt;</code> 标记,您可以利用 Bean 标记库来定位要创建的 <code>ActionForm</code> Bean,并显示为 <code>name</code><code>email</code> 保存的用户数据。</li>
</ol>
</div>
<a name="actionForm"></a>
<h2>创建 <code>ActionForm</code> Bean</h2>
<p>Struts <code>ActionForm</code> Bean 用于保留请求之间的数据。例如,如果用户提交一个窗体,则数据会暂时存储在窗体 Bean 中,以便它可以重新显示在窗体页面中(如果数据的格式无效或登录失败),或者显示在登录成功页中(如果数据通过验证)。</p>
<ol>
<li>右键单击 <code>MyStrutsApp</code> 项目节点,然后选择 "New"(新建)> "Other"(其他)。在 "Categories"(类别)下选择 "Struts",然后在 "File Types"(文件类型)下选择 "Struts ActionForm Bean"。单击 "Next"(下一步)。</li>
<li>为 "Class Name"(类名)键入 <code>LoginForm</code>。然后在 "Package"(包)下拉列表中选择 <code>com.myapp.struts</code> 并单击 "Finish"(完成)。
<p>
IDE 将创建 <code>LoginForm</code> Bean,并在源代码编辑器中将其打开。默认情况下,IDE 为它提供了一个名为 <code>name</code><code>String</code>,以及名为 <code>number</code><code>int</code>。这两个字段都具有为其定义的存取方法。而且,IDE 会将一个 Bean 声明添加到 <code>struts-config.xml</code> 文件中。如果您在源代码编辑器中打开 <code>struts-config.xml</code> 文件,将看到以下声明(由向导添加):</p>
<pre class="examplecode">
&lt;form-beans&gt;
<strong>&lt;form-bean name=&quot;LoginForm&quot; type=&quot;com.myapp.struts.LoginForm&quot; /&gt;</strong>
&lt;/form-beans&gt;
</pre>
<p>IDE 在 <code>struts-config.xml</code> 文件中提供导航支持。按住 Ctrl 键并将鼠标悬停在 <code>LoginForm</code> Bean 的全限定类名上。此名称将变为一个链接,使您能够直接导航至源代码编辑器中的该类:</p>
<img alt="Struts 配置文件中显示的导航支持" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/navigation-support.png" title="在 struts-config.xml 中提供导航支持"></li>
<li>在源代码编辑器中的 <code>LoginForm</code> Bean 中,创建字段以及随附的存取方法,这些方法与 <code>login.jsp</code> 中创建的 <code>name</code><code>email</code> 文本输入字段相对应。由于已在 <code>LoginForm</code> 框架中创建 <code>name</code>,因此只需要实现 <code>email</code>
<p>
将以下声明添加到 <code>name</code> 下(更改的内容以<strong>粗体</strong>显示):</p>
<pre class="examplecode">
private String name;
<strong>private String email;</strong></pre>
<a name="accessors"></a>
<p>要创建存取方法,请将光标放在 <code>email</code> 上,然后按 Alt-Insert 组合键。</p>
<img alt="源代码编辑器中显示的 &quot;Insert Code&quot;(插入代码)菜单" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/create-accessors.png" title="在源代码编辑器中按 Ctrl-I 时显示 &quot;Insert Code&quot;(插入代码)菜单">
<p>
选择 "Getter and Setter"(getter 和 setter),然后在显示的对话框中选择 <code>email : String</code> 并单击 "Generate"(生成)。将生成 <code>email</code> 字段的存取方法。</p>
<p class="notes"><strong>注:</strong>可以删除 <code>number</code> 的声明和存取方法,因为它不会在本教程中使用。</p></li>
</ol>
<a name="actionClass"></a>
<h2>创建 <code>Action</code></h2>
<p><code>Action</code> 类包含应用程序中的业务逻辑。收到窗体数据后,将由 <code>Action</code> 对象的 <code>execute</code> 方法处理数据,并确定处理过的数据要转发到的视图。由于 <code>Action</code> 类是组成 Struts 框架的必要部分,因此 NetBeans IDE 为您提供了一个向导。</p>
<ol>
<li>在 "Projects"(项目)窗口中,右键单击 <code>MyStrutsApp</code> 项目节点,然后选择 "New"(新建)> "Other"(其他)。从 "Struts" 类别中,选择 "Struts Action"(Struts 操作),然后单击 "Next"(下一步)。</li>
<li>在 "Name and Location"(名称和位置)面板中,将名称更改为 <code>LoginAction</code></li>
<li>在 "Package"(包)下拉列表中选择 <code>com.myapp.struts</code></li>
<li>在 "Action Path"(操作路径)中键入 <code>/login</code>。该值必须与为 <code>login.jsp</code><code>&lt;html:form&gt;</code> 标记的 <code>action</code> 属性所设置的值相匹配。请确保设置与以下屏幕快照中显示的一样,然后单击 "Next"(下一步)。 <br> <img alt="新建 Struts 操作向导" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/new-struts-action.png" title="新建 Struts 操作向导"></li>
<li>在此向导的第三步中,可以选择将 <code>Action</code> 类与窗体 Bean 相关联。请注意,以前创建的 <code>LoginForm</code> Bean 作为“ActionForm Bean 名称”的一个选项列出。对面板进行以下调整:
<ul>
<li>删除 "Input Resource"(输入资源)字段的正斜杠</li>
<li>将 "Scope"(范围)设置为 "Request"(请求)(在 Struts 中,"Session"(会话)是默认范围设置。)</li>
<li>取消选择 "Validate ActionForm Bean"(验证 ActionForm Bean)选项</li>
</ul>
单击 "Finish"(完成)。将生成 <code>LoginAction</code> 类并在源代码编辑器中打开文件。另请注意,以下 <code>action</code> 条目将添加到 <code>struts-config.xml</code> 文件:
<pre class="examplecode">
&lt;action-mappings&gt;
<strong>&lt;action name=&quot;LoginForm&quot; path=&quot;/login&quot; scope=&quot;request&quot; type=&quot;com.myapp.struts.LoginAction&quot; validate=&quot;false&quot;/&gt;</strong>
&lt;action path=&quot;/Welcome&quot; forward=&quot;/welcomeStruts.jsp&quot;/&gt;
&lt;/action-mappings&gt;</pre>
<code>name</code><code>scope</code> 属性适用于与此操作关联的窗体 Bean。具体说来,当传入请求与 <code>/login</code> 相匹配时,Struts 框架就会自动实例化 <code>LoginForm</code> 对象,并使用请求中发送的窗体数据填充此对象。<code>validate</code> 的默认值设置为 <code>true</code>。此设置通知框架调用窗体 Bean 的 <code>validate</code> 方法。但是您在向导中取消选择了此选项,因为您将在下一步中手动编码简单验证,这不需要 <code>validate</code> 方法。</li>
</ol>
<a name="validate"></a>
<h2>实现验证</h2>
<p>在源代码编辑器中,浏览 <code>LoginAction</code> 类并查看 <code>execute</code> 方法:</p>
<pre class="examplecode">
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
return mapping.findForward(SUCCESS);
}</pre>
<p>注意 <code>SUCCESS</code> 的定义(列在 <code>LoginAction</code> 类声明的下方):</p>
<pre class="examplecode">private final static String SUCCESS = &quot;success&quot;;</pre>
<p>目前,<code>mapping.findForward</code> 方法设置为无条件地向名为 <code>success</code> 的输出视图转发任何请求。此设置并不是很令人满意;您希望首先对传入数据执行某种验证以确定是发送 <code>success</code> 视图,还是发送其他任何视图。</p>
<ul>
<li><a href="#beanData">访问 Bean 数据并准备转发条件</a></li>
<li><a href="#errorMsg">设置错误消息</a></li>
</ul>
<div class="indent">
<a name="beanData"></a>
<h3>访问 Bean 数据并准备转发条件</h3>
<ol>
<li><code>execute</code> 方法的主体中键入以下代码:
<pre class="examplecode">
// extract user data
LoginForm formBean = (LoginForm)form;
String name = formBean.getName();
String email = formBean.getEmail();</pre>
为了使用传入的窗体数据,需要使用 <code>execute</code><code>ActionForm</code> 参数,并将其强制转换为 <code>LoginForm</code>,然后应用先前创建的 getter 方法。</li>
<li>键入以下条件子句以对传入数据执行验证:
<pre class="examplecode">
// perform validation
if ((name == null) || // name parameter does not exist
email == null || // email parameter does not exist
name.equals(&quot;&quot;) || // name parameter is empty
email.indexOf(&quot;@&quot;) == -1) { // email lacks '@'
return mapping.findForward(FAILURE);
}</pre>
在此阶段,<code>execute</code> 方法会显示如下:
<pre class="examplecode">public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// extract user data
LoginForm formBean = (LoginForm) form;
String name = formBean.getName();
String email = formBean.getEmail();
// perform validation
if ((name == null) || // name parameter does not exist
email == null || // email parameter does not exist
name.equals(&quot;&quot;) || // name parameter is empty
email.indexOf(&quot;@&quot;) == -1) { // email lacks '@'
return mapping.findForward(FAILURE);
}
return mapping.findForward(SUCCESS);
}</pre></li>
<li><code>FAILURE</code> 的声明添加到 <code>LoginAction</code> 类中(更改以<strong>粗体</strong>显示):
<pre class="examplecode">
private final static String SUCCESS = &quot;success&quot;;
<strong>private final static String FAILURE = &quot;failure&quot;;</strong>
</pre>
</li>
</ol>
<p>如果用户同时提供了 <code>name</code><code>email</code> 字段的条目,并且输入的电子邮件地址中包含“@”符号,则 <code>execute</code> 方法将使用以上逻辑将请求转发到 <code>success</code> 视图。否则,将转发 <code>failure</code> 视图。您可以将 <code>failure</code> 视图设置为指回窗体页面,允许用户再次输入格式正确的数据,详细信息请参见下面的<a href="#forward"><code>forward</code> 条目添加到 <code>struts-config.xml</code></a></p>
<a name="errorMsg"></a>
<h3>设置错误消息</h3>
<p>如果返回登录窗体,则最好通知用户登录已失败。您可以实现此功能,方法是在窗体 bean 中添加 <code>error</code> 字段,并将相应的 <code>&lt;bean:write&gt;</code> 标记添加到 <code>login.jsp</code> 中的窗体。最后,在 <code>Action</code> 对象中,将错误消息设置为显示在选择了 <code>failure</code> 视图的事件中。</p>
<ol>
<li>打开 <code>LoginForm</code> 并将 <code>error</code> 字段添加到类中:
<pre class="examplecode">
// error message
private String error;</pre></li>
<li><code>error</code> 添加 getter 方法和 setter 方法,<a href="#accessors">如上所述</a></li>
<li>修改 setter 方法使它如下所示:
<pre class="examplecode">
public void setError() {
this.error =
&quot;&lt;span style='color:red'&gt;Please provide valid entries for both fields&lt;/span&gt;&quot;;
}
</pre></li>
<li>打开 <code>login.jsp</code> 并进行以下更改:
<pre class="examplecode">
&lt;html:form action=&quot;/login&quot;&gt;
&lt;table border=&quot;0&quot;&gt;
&lt;tbody&gt;
<strong>&lt;tr&gt;
&lt;td colspan=&quot;2&quot;&gt;
&lt;bean:write name=&quot;LoginForm&quot; property=&quot;error&quot; filter=&quot;false&quot;/&gt;
&amp;nbsp;&lt;/td&gt;
&lt;/tr&gt;</strong>
&lt;tr&gt;
&lt;td&gt;Enter your name:&lt;/td&gt;
&lt;td&gt;&lt;html:text property=&quot;name&quot; /&gt;&lt;/td&gt;
&lt;/tr&gt;
</pre></li>
<li><code>LoginAction</code> 中,在 <code>if</code> 条件子句中添加一条语句,以便在转发 <code>failure</code> 条件之前设置错误消息(更改的内容以<strong>粗体</strong>显示):
<pre class="examplecode">
if ((name == null) || // name parameter does not exist
email == null || // email parameter does not exist
name.equals(&quot;&quot;) || // name parameter is empty
email.indexOf(&quot;@&quot;) == -1) { // email lacks '@'
<strong>formBean.setError();</strong>
return mapping.findForward(FAILURE);
}
</pre></li>
</ol>
<p>完成的 <code>LoginAction</code> 类现在应该如下所示:</p>
<pre class="examplecode">
public class LoginAction extends org.apache.struts.action.Action {
private final static String SUCCESS = &quot;success&quot;;
private final static String FAILURE = &quot;failure&quot;;
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
// extract user data
LoginForm formBean = (LoginForm)form;
String name = formBean.getName();
String email = formBean.getEmail();
// perform validation
if ((name == null) || // name parameter does not exist
email == null || // email parameter does not exist
name.equals(&quot;&quot;) || // name parameter is empty
email.indexOf(&quot;@&quot;) == -1) { // email lacks '@'
formBean.setError();
return mapping.findForward(FAILURE);
}
return mapping.findForward(SUCCESS);
}
}
</pre>
</div>
<a name="forward"></a>
<h2><code>forward</code> 条目添加到 <code>struts-config.xml</code></h2>
<p>为了让应用程序能够使 JSP 页与 <code>LoginAction</code><code>execute</code> 方法所返回的转发条件相匹配,您需要将 <code>forward</code> 条目添加到 <code>struts-config.xml</code> 文件中。</p>
<ol>
<li>在源代码编辑器中打开 <code>struts-config.xml</code>,右键单击 <code>LoginForm</code><code>action</code> 条目中的任何位置,然后选择 "Struts" &gt; "Add Forward"(添加转发)。 <br> <img alt="显示在 struts-config.xml 的右键单击菜单中的 Struts 选项" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/add-forward.png" title="右键单击并选择 &quot;Struts&quot; &gt; &quot;Add Forward&quot;(添加转发)"></li>
<li>在 "Add Forward"(添加转发)对话框的 "Forward Name"(转发名称)中,键入 <code>success</code>。在 "Resource File"(资源文件)字段中输入 <code>success.jsp</code> 的路径(即 <code>/WEB-INF/success.jsp</code>)。此对话框现在应如下所示: <br> <img alt="&quot;Add Forward&quot;(添加转发)对话框" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/add-forward-dialog.png" title="&quot;Add Forward&quot;(添加转发)对话框在 struts-config.xml 中创建一个转发条目"> <br> 单击 "ADD"(添加)。请注意,以下 <code>forward</code> 条目已添加到 <code>struts-config.xml</code> 中(更改的内容以<strong>粗体</strong>显示):
<pre class="examplecode">
&lt;action name=&quot;LoginForm&quot; path=&quot;/login&quot; scope=&quot;request&quot; type=&quot;com.myapp.struts.LoginAction&quot; validate=&quot;false&quot;&gt;
<strong>&lt;forward name=&quot;success&quot; path=&quot;/WEB-INF/success.jsp&quot;/&gt;</strong>
&lt;/action&gt;
</pre></li>
<li>执行相同的操作以添加 <code>failure</code> 的转发条目。将 "Resource File"(资源文件)路径设置为 <code>/login.jsp</code>。以下 <code>forward</code> 条目将添加到 <code>struts-config.xml</code>(更改的内容以<strong>粗体</strong>显示):
<pre class="examplecode">
&lt;forward name=&quot;success&quot; path=&quot;/WEB-INF/success.jsp&quot;/&gt;
<strong>&lt;forward name=&quot;failure&quot; path=&quot;/login.jsp&quot;/&gt;</strong>
</pre>
</li>
</ol>
<a name="configure"></a>
<h2>配置和运行应用程序</h2>
<p>IDE 使用 Ant 构建脚本来构建和运行 Web 应用程序。当您基于在新建项目向导中输入的选项创建项目时,IDE 会生成构建脚本。在构建并运行应用程序之前,需要将应用程序的默认入口点设置为 <code>login.jsp</code>。(可选)您还可以向项目中添加简单的样式表。</p>
<ul>
<li><a href="#welcome">设置欢迎页</a></li>
<li><a href="#style">附加样式表</a></li>
<li><a href="#run">运行应用程序</a></li>
</ul>
<a name="welcome"></a>
<h3>设置欢迎页</h3>
<ol>
<li>在 "Projects"(项目)窗口中,双击 <code>web.xml</code> 部署描述符。源代码编辑器顶部列出的标签为您提供了 <code>web.xml</code> 文件的界面。单击 "PAges"(页面)标签。在 "Welcome File"(欢迎文件)字段中,输入 <code>login.jsp</code><br> <img alt="部署描述符界面" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/welcome-files.png" title="应用程序的部署描述符的图形编辑器"> <br> 现在单击 "Source"(源)标签可查看此文件。注意 <code>login.jsp</code> 现在已列在 <code>welcome-file</code> 条目中:
<pre class="examplecode">
&lt;welcome-file&gt;login.jsp&lt;/welcome-file&gt;
</pre></li>
</ol>
<a name="style"></a>
<h3>附加样式表</h3>
<ol>
<li>向项目中添加简单的样式表。执行此操作的一个简单方法是将<a href="https://netbeans.org/files/documents/4/2228/stylesheet.css">此样例样式表</a>保存到您的计算机上。复制此文件(Ctrl-C 组合键),然后在 IDE 中,选择 "Projects"(项目)窗口中的 "Web Pages"(Web 页)节点,然后按 Ctrl-V 组合键。此文件即添加到项目中。</li>
<li>将样式表链接到您的 JSP 页,方法是在 <code>login.jsp</code><code>success.jsp</code><code>&lt;head&gt;</code> 标记之间添加一个引用:
<pre class="examplecode">
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;stylesheet.css&quot;&gt;
</pre></li>
</ol>
<a name="run"></a>
<h3>运行应用程序</h3>
<ol>
<li>在 "Projects"(项目)窗口中,右键单击该项目节点并选择 "Run"(运行)。IDE 将构建 Web 应用程序并使用您在创建此项目时所指定的服务器对其进行部署。浏览器将打开并显示 <code>login.jsp</code> 页。键入一些会验证失败的数据,即将任一字段保留为空或输入缺少 "@" 符号的电子邮件地址: <br> <img alt="浏览器中显示的包含样例数据的 login.jsp" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/login-form.png" title="窗体包含将验证失败的数据"> <br><br> 单击 "Login" 之后,登录窗体页会重新显示,其中包含一条错误消息: <br> <img alt="显示的具有错误消息的 login.jsp" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/login-form-error.png" title="窗体重新显示,但具有错误消息"> <br><br> 尝试输入应该通过验证的数据。单击 "Login" 之后,您将会看到成功页: <br> <img alt="显示的包含输入数据的 success.jsp" class="margin-around b-all" src="../../../images_www/articles/72/web/struts/success-page.png" title="成功页显示,并显示输入数据">
</li>
</ol>
<div class="feedback-box">
<a href="/about/contact_form.html?to=3&amp;subject=Feedback:%20Introduction%20to%20Struts">请将您的反馈意见发送给我们</a></div>
<br style="clear:both;">
<a name="seeAlso"></a>
<h2>另请参见</h2>
<p>对 NetBeans IDE 中 Struts 框架的简介到此结束。本文档演示了如何使用 Struts 框架在 NetBeans IDE 中构造一个简单的 Web MVC 应用程序,并且向您介绍了用于开发 Web 应用程序的 IDE 的界面。您已了解了如何在 JSP 页中使用 Struts 标记、如何在 Struts <code>ActionForm</code> Bean 中暂时存储用户数据,以及如何使用 Struts <code>Action</code> 对象实现转发逻辑。您还将学到如何对应用程序执行简单验证,包括为失败的登录尝试设置警告消息。</p>
<p>有关相关教程,请参见以下资源:</p>
<ul>
<li><a href="framework-adding-support.html">添加对 Web 框架的支持</a>。此教程为一般性指南,介绍了如何使用插件管理器将 Web 框架支持添加到 NetBeans IDE 中。</li>
<li><a href="quickstart-webapps-spring.html">Spring Web 框架简介</a>。此教程介绍了使用 Spring Framework 在 NetBeans IDE 中开发 Web 应用程序的基础知识。</li>
<li><a href="jsf20-intro.html">JavaServer Faces 2.0 简介</a>。此文档介绍如何将托管 Bean 写入 Web 页,以及如何利用 Facelets 模板。</li>
</ul>
</body>
</html>