教学管理系统设计与实现外文资料翻译

发布时间:2012-04-21 18:51:39   来源:文档文库   
字号:

题目名称Struts And Tiles Aid Component-based

Development


译文题目:Struts Tiles 辅助基于组件的开发

原文题目:Struts and Tiles aid component-based development

原文出处:Thinking in Java, 3rd ed. Revision 4.0


Struts Tiles 辅助基于组件的开发

1994 年,当时主流的采用 Web 应用程序的开发才刚开始。由于 Web 的不成熟,只有较少的工具能帮助开发人员构建 Web 软件。结果,在特定解决方案中的应用程序混合了 HTML 代码与应用程序逻辑。很显然,UI 设计的更改和业务逻辑的更新在大型应用程序中既困难又昂贵,因为紧耦合的表示和逻辑将这两种元素搅和在一起,进而导致错误和缓慢的进展。而且,混合的代码要求部分开发人员具备 UI 设计知识,或者要求开发人员与图形设计人员之间有紧密的工作关系,这常常会造成时间上的浪费。

JSP 技术和标记的引入稍微改善了这种更改问题,因为能够将逻辑和显示分离。UI 设计人员能够对显示进行卓有成效的工作,同时开发人员能够专注于逻辑。然而,这种方法仍存在一些缺陷。尤其是某些操作(还有公共操作)的开发仍很困难。验证表单就是典型的例子。正如很多人所知,表单验证的过程类似于这样:

显示表单;等待用户填写然后提交数据。

检查各个域值是否有效;如果有错误,则重新显示表单。

处理用户输入的数据,可能将其存储在一个数据库中。

在新页面上向用户显示处理的结果或下一步(可能是另一个表单)。

如果在这一过程中只使用 JSP 页面,那么在需要再次更改代码时,您会发现,按照可管理性这条思路,将控制从一个页面路由至另一个页面很难。您想把第 4 步和第 3 步置于同一个页面吗?如果使用多个单独的 JSP 页面,那么如何跟踪哪个页面链接至其它页面,以及在要更改一个页面的文件名或位置时该怎么做呢?而且,在第 2 步检测到某个域中的错误时,如何重新显示带有一条错误消息的原始表单,但还要保留用户已填入的值呢?Struts,一种开放源码模型-视图-控制器框架,通过帮助解决所有这些问题,从而使开发人员的工作更为轻松。

本文并不深入讨论 MVC 平台。有关这方面的信息,请参阅 Malcolm Davis 所写的标题为”Struts,an open-source MVC implementation” developerWorks文章。您正在阅读的这篇文章讨论自 Malcolm 的文章发表以来对 Struts 所做的更改,包括 Tiles 库。至于代码的安装过程,本文仅涉及 Jakarta Tomcat 4.0Catalina)最小安装所需的步骤。如果您没有使用 Tomcat,请查阅手册以了解您的应用程序服务器。

Struts Tiles 的背景知识

Craig McClanahan Apache Tomcat项目的技术主管,他创立 Struts 项目以满足对这方面的渴望。它作为模型-视图-控制器框架首选的并经过正式认可的开放源码实现,已经越来越流行了。它以与交付产品一起分发的形式受到来自 Sun IBM 的支持。因为 Craig 积极参与 Tomcat Struts 的开发工作,所以 Struts 将继续与 JSP Servlet规范的参考实现高度兼容,进而与所有 J2EE 应用程序服务器高度兼容。

Malcolm Davis 的有关StrutsdeveloperWorks文章涵盖了整个 Struts 0.9 的功能;为了简短起见,我将只讨论对 Struts 0.9 的更改以及他未涉及的 Struts 主题。目前的 Struts 发行版本是 1.0.2,但自 2002 3 19 日起有一个标记为 1.1-b1 beta 测试版可供使用。因为 beta 测试版表示编码工作已经完成,在这种情况下只进行错误修正,所以极有可能 1.1 的最终版本不久就将面市,而在 Struts 邮件列表上已经有这种呼声。因此,任何利用 Struts 的新项目很可能都将 1.1 代码作为基础,而这就是我将讨论的内容。

自版本0.9以来对Struts框架的有用添加包括经改进的表单验证功能、可以通过 XML 声明来指定表单元素和可以动态地定义 bean 特性。然而,最重要的添加可能是将 Tiles 模板库合并到 Struts 分发版中。

您是否曾希望用一种更简便的方法创建一组页面(或可能是整个应用程序),并且每个页面上的用户界面保持一致,有相同的导航栏、页眉和页脚等等?在含有较多内容的页面内显示多个类 portlet 的矩形内容的方法又如何?在 Tiles 框架的帮助下,您可以完成这两项任务和其它更多任务。通过定义屏幕和一组可嵌入在 JSP 页面中的标记的核心 XML 文件来插入静态和动态内容,Tiles框架允许您构建组件化的视图,并按您的希望来组装它们,从而有助于提高灵活性、可重用性、一致性和可维护性。

Struts Tiles 之间交互良好,因为这两个项目的开发人员已经认识到这两者具有互补性,所以决定让这两者共同协作。开发人员可以指定 Tiles 页面定义作为Struts操作的目标视图(按照Struts的说法是一个forward)。因为 Struts Tiles 都遵循 JSP 标记库规范,所以可以在 JSP 页面中将 Struts 标记和 Tiles 标记相互混合在一起。

您可能渴望尝试 Tiles 框架,并确切地了解它可以做些什么。如果您希望在自己安装本文的示例前先了解这些示例的运行情况,可以看看它们在带有嵌入式 Tomcat JBoss 服务器上是怎样运行的。

Struts Tiles 是用于 Web 开发的辅助工具,所以您需要设置一个 Web 容器对它们进行实验;将 Tomcat 设置为您的容器,然后设置 Struts Tiles 包,我会在下一节中循序渐进地讲述这一过程。这些指导信息还向您展示了如何安装本文的样本代码。一旦完成了这一切,您就准备好继续本文。示例 1 应用程序没有利用 Struts Tiles;它演示了以页面为中心的方法。通过将它与示例 2 比较,您会看到 Struts Tiles 将如何极大地提高您的 Web 开发的结构化程度和可管理性。最后,示例 3 演示了将功能添加到一个使用 Struts Tiles 的、并且已经启动且正在运行的 Web 应用程序中是多么地简单。

安装 Struts Tiles

在带有 J2SE 1.4 SDKAnt 1.4.1Tomcat 4.0.3 Struts 1.1-b1 Linux 机器上,下列指导信息已经经过了测试。如果因这些软件包的版本不同而遇到困难,您可能需要使用上面所指定的版本,以便开始了解 Struts Tiles 的设置和开发。

转至 TOMCAT_HOME/bin 目录。

通过输入 ./startup.sh (如果在运行 UNIX)或 ./startup.bat (如果在运行 Windows)来启动 Tomcat 服务器。

Web 浏览器指向 http://localhost:8080/examples 来验证 Tomcat 是否已启动并正确运行。缺省情况下,Tomcat 附带了 Examples 应用程序。如果 Examples 不工作,则 Tomcat 发生故障;请参阅 Tomcat 文档来解决问题。

Hello, World:首次尝试

要研究我们第一个示例,请遵循下列步骤:

转至 EX1_INSTALL 目录。

编辑 build.xml 文件,为 tomcat.install.dir 填写适当值。尽管该值可以是绝对路径,也可以是相对路径;但如果您不了解 Ant 是如何工作的,或许最好使用绝对路径。

输入 ant deploy 。这将把第一个示例应用程序构建到 WAR 文件中,以备部署,然后将它部署至 Tomcat。如果得到一个指出无法找到 Ant 的错误,请参阅安装 Struts Tiles”一节中的第 3 步,并确保您的路径环境变量包含 Ant

Web 浏览器指向 http://localhost:8080/ex1。您应该会看到“Hello, World”页面。

示例 1 Web 应用程序非常简单,它演示了常见的 Web 应用程序功能。几乎所有应用程序(也包括这个最简单的应用程序)都要求所有页面具有一致的用户界面。通常,这意味着所有页面都有公共的徽标、顶部栏、上部或左侧导航栏、主体和页脚。在示例 1 中,我有意对每一页面中的公共项进行硬编码,以便说明这一点。Web 应用程序开发的新手一般会通过将现有代码复制粘贴到新文档中来添加新的功能页面。很容易预见这种方法难以应付将来的变化。随着每一次增加新内容,更改诸如菜单、徽标等公共页面元素的过程花的时间会更长,更容易出错。很明显,复制粘贴方法对于任何具有大量页面的应用程序是一个糟糕的模型。

敏锐的读者会认识到 JSP 技术提供了包括来自其它 servlet 和页面中的内容的功能。我们为什么不可以仅仅使用 标记来合并公共元素呢?这肯定会使那些元素更易于更改。如果您需要更改菜单,只要更改包含菜单的文件。所有其它页面只需使用 标记就可以得到菜单中的内容,这样这些页面可以自动获得对菜单的更改。但是,当需要更改实际布局或需要重新组织文件和目录时,这种方法有不足之处。当决定更改以页面为中心模型下的布局时,必须对每个单个页面进行更改,因为即使对公共元素的访问已做了集中化处理,但仍然是由每个页面中的 HTML 代码来描述布局本身(有哪些是元素及它们的位置)。同样地,当决定更改包含了某个公共元素内容的文件的文件名或位置时,必须逐个更改使用该元素的文件。什么原因呢?因为每个文件根据固定的物理文件名,而不是逻辑对象名来查找每个公共元素。因此,必须更新每个对物理文件名的引用。Tiles 视图组件可以解决这些问题。

如果更进一步地研究 index.jsp form1.jsp(这两个 JSP 文件构成该应用程序),会发现另一个缺点:错误处理相当笨拙。错误处理代码是在 form1.jsp 中,其中我必须重复显示代码,并添加代码以插入用户在前一表单屏幕(index.jsp)中输入的值。如果用户概要信息域曾更改过,或者如果输入表单的显示曾更改过,就必须更新这两个地方中的代码。我可以将 form1.jsp 的错误处理部分与 index.jsp 中的初始表单显示结合在一起,但在初始表单装入时,我仍将需要做额外的工作以把域值设置成空字符串,并且我仍将需要有一个物理文件名来表示用户概要信息的最终静态显示,这意味着发生更改时,该应用程序结构仍是很脆弱的。Struts 表单自动化可以解决这种笨拙的表单处理缺陷。

Hello, World:经改进的新的应用程序

现在,让我们研究刚才看到的 Web 应用程序示例中的 Struts Tiles 版本。请执行下列步骤:

转至 EX2_INSTALL 目录。

编辑 build.xml 文件,为 struts.install.dir tomcat.install.dir 填写适当的值。

输入 ant deploy 。这将把第二个示例应用程序构建到 WAR 文件中以备部署,然后将它部署至 Tomcat。如果看到关于无法复制文件的错误,请检查第 2 步以确保正确设置了 struts.install.dir tomcat.install.dir

Web 浏览器指向 http://localhost:8080/ex2。您应该会看到“Hello, World”页面。

如果您已看过 EX2_INSTALL 目录,您很可能会说:这里要做些什么呢?有好多文件。与大多数强调更有序和结构化程度更高的技术一样,对于 Struts Tiles,在一开始需要在管理文件上花些工夫。对于只有少许页面的小项目,这一额外开销可能微不足道。然而,随着项目变大,Struts Tiles 方法逐渐会显示其优越性。让我们一点点地体会吧!这里我不想讨论 EX2_INSTALL/src/WEB-INF/web.xml;尽管这个文件实质上与其示例 1 中相对应的文件不同,而且大多数行都是样板,但理解这些设置对于着手开发并不太重要。

EX2_INSTALL/src/WEB-INF/struts-config.xml 中,自先前有关 Struts 的文章以来重要的更改有在 节中 DynaActionForm 的使用及在 节中 tile 作为目标的使用。在 Struts 的以前版本中,您必须为每个所使用的表单 bean 定义一个 Java 类。仅当不同的 HTML 表单共享域时,才可以在这些表单之间共享表单 bean。总之,每个表单 bean 需要有一个 Java 类是一个非常麻烦的要求。现在,您可以在 struts-config.xml 文件中指定表单 bean 的特性,而且是迅速地!不必有保存 Java 类的单独文件,就能自动创建这种 bean。象处理 Hashtable 对象一样,用值的强类型对象来处理动态表单 bean。至于操作映射,一旦确定将 Tiles 库合并到 Struts 分发版之后,那么指定一个 tile 作为目标,就完全是增加一项逻辑而已。您会在概要信息表单的操作映射中看到 tile 目标(tile.profileInput tile.profileOutput)。在 input 属性和 path 属性中指定 tile。注:可以指定 tile 目标弥补了我在分析以页面为中心的模型中提到的更改文件名和位置中的缺陷:tile 目标是虚拟名称或逻辑名称,而不是物理名称。

标记中指定 tile。您可以将定义命名为任何希望的名称,而且 name 属性不必是与 path 属性匹配的子字符串。我为第一个定义选择了名称 rootLayout ,以表明它是应用程序中的页面要遵循的基本布局。注:路径是 /tiles-layout/rootLayout.jsp。如果查看 EX2_INSTALL/src/web 下的 /tiles-layout/rootLayout.jsp,您会看到这种布局是多么的简单整齐。用户界面的设计人员会爱上它。还请注意:它不包含任何代码,所以用户界面设计人员在进行更改时不必担心破坏什么。

rootLayout.jsp 中的 标记对应于 tiles-defs.xml rootLayout 定义内的 标记。注:每个 标记都有一个表示逻辑名称的属性。每个逻辑名称映射至通过在 tiles-defs.xml 内的 中使用 标记指定的名称和值。通过在 rootLayout.jsp 页面中使用逻辑名称,而非物理名称,并通过在 tiles-defs.xml 中统一物理名称,我们就可以更改文件名,并使项目文件系统的组织易于管理。

真正节省时间和适应性方面最显著的增强方面体现在布局的继承,这是 Struts 的另一个特性。在 tiles-defs.xml 中,“Page definitions”栏下面的节有两个页面:tile.profileInput tile.profileOutput。这些名称是任意的,如果您不喜欢 tile. 前缀,可以不使用它(但是您使用的名称必须与 struts-config.xml 文件中指定的目标相匹配)。这些名称应该与 struts-config.xml 标记中的 path 属性匹配。这些名称还应该与 struts-config.xml 标记中的 input 属性匹配。在开发用户界面时, 标记中的 extends 属性是体现开发灵活性的地方。通过指定主布局并扩展它,您不仅能灵活更改象 topBanner topMenu panel1 panel2 这样的公共元素及其它组件, 还能随意地将不同元素放在页面上及更改它们的位置。例如,您可以添加 panel4 (一个 tile)以在页面左侧的 panel2 下显示本地天气。只要天气代码不需要用户的任何输入或与页面上的其它组件交互,您就可以添加 panel4 而不必对应用程序业务逻辑做任何更改,甚至不必更改除 rootLayout.jsp 以外的 JSP 页面。

Struts Tiles 更复杂的使用

篇幅所限,不允许我再深入讨论 Struts Tiles 包其它一些实用的方面,但我真的想简要地谈一下,这样您可以对 Struts Tiles 可以做些什么有所了解。如果有足够的需求,可能这些内容会是将来文章的主题。

您会在本文的两个示例中注意到,我编码了名、姓、喜欢的颜色和出生日期等基本验证。我所采用的这类简单验证实际上可由 Struts 通过使用它的格式验证来执行,这样节省了花在编码 Java 语句的时间。例如,检查是否是空字符串、检查字符串是否与日期相匹配或检查是否与其它某些正则表达式匹配等,这都是可行的。您会发现将格式验证用作第一级检查会是十分方便的,在检查是否符合更复杂的业务逻辑规则之前,程序可用第一级检查来消除一些明显错误。Struts 提供了一个验证器包,可以通过 WEB-INF 目录中的 validator.xml 描述符来配置该包。在这两个示例中我没有包含该验证器包;如果您想试一下,它包含在 Struts 1.1-b1 包中。

Struts Tiles 包的另一个有用特性是与容器管理的安全性相集成。很多人都在使用诸如 IBM WebSphere JBoss 之类的应用程序服务器。这些服务器通过处理安全性中所涉及的许多日常任务使认证和授权更便捷,让您摆脱必须对它们进行的编码。Struts 允许您根据用户角色有条件地向不同用户显示 bean 数据的不同位,这可以通过自动查询容器来决定。同样地,Tiles 框架允许您根据用户角色有条件地向不同用户显示不同的视图组件,这也是通过自动查询容器来决定的。我在自己的应用程序中使用这些特性向管理员、常规用户或 guest 用户显示不同的菜单。

最后要说明,我在示例中没有谈到国际化,但对于那些构建将来最终要以多种语言发布的大型应用程序的人来说,这实际是一个重要方面。Struts 让您创建消息资源特性文件,这些文件指定标签、标题和其它输出的文本。如果您用几种不同语言的输出填入消息资源特性文件的话,只要简单更改语言设置就能将所有窗口构件和硬编码的文本更改成适当语言,甚至在应用程序运行中也可以更改。

未来的方向

Struts Tiles 将走向何方呢?刚出现的两个更改必定会使 Struts Tiles 更加有用。第一个更改是工作流管理系统。您很可能处理过许多多步骤的业务过程;目前,在 Web 应用程序中对它们进行编码可以是一个主要的麻烦,因为您必须协调不同的步骤,而每一步骤都是 Java 类中的一个单独方法或是一个单独的 servlet JSP 页面。商业市场中的工具通过使开发人员能对多步骤的业务过程进行建模,然后自动生成 Java 代码作为进一步开发的基础,来帮助管理这些过程。Struts 工作流管理系统将提供类似的功能,使开发人员能够编写业务过程的脚本,这些过程在 Web 应用程序中跨多个页面,并指定这些页面如何通过核心的基于规则的系统进行交互。

另一个同样有望即将出现的更改是将 Struts 标记合并到 JSP 标准标记库(JSP Standard Tag Library)中,这个库是 Jakarta 项目,它试图产生一个有用的定制标记集合,从而简化并加快用 JSP 页面编写 Web 应用程序的开发。其中的意义在于:Web 应用程序的开发将比过去任何时候都更容易,因为您可以通过使用定制标记的标准机制得到一个在此基础上做进一步开发的预先编写好的代码库。而且,JSP STL 标准中这个项目的合并甚至确保了 Struts Tiles 功能更为广泛的分发和可用,这意味具有这种技能的开发人员在众多项目中大有作为,而且公司更加容易获得开发 Web 应用程序的熟练人才。


Struts and Tiles aid component-based development

In 1994, when mainstream adoption of Web application development had only just begun. Because of the immaturity of the Web, developers had few tools to help them build Web software. As a result, applications mixed HTML code with application logic in ad hoc solutions. Understandably, UI design changes and business logic updates were both difficult and expensive in large applications because the tightly coupled presentation and logic obfuscated both elements, leading to errors and slow progress. Also, the mixed code necessitated knowledge of UI design on the part of developers or a close working relationship between developer and graphic designer that often made for inefficient use of time.

The introduction of JSP technology and tags ameliorated the change problem somewhat because logic and display could be separated. UI designers could productively work on display while developers could focus on logic. However, this approach still suffered shortcomings. Notably, certain operations -- common ones, too -- were still hard to develop. The validating form is the classic example. As many of you know, the process of form validation goes something like this:

Display form; wait for user to fill it out and submit data.

Check for valid field values; redisplay form if there are errors.

Process data entered by user, perhaps storing it in a database.

Display new page with results of processing or next step (probably another form) for user.

If you're only using JSP pages for this process, you'll find it difficult to route control from one page to another in a way that preserves manageability down the road when the code needs to be changed again. Do you place step 4 in the same page as step 3? If you use separate JSP pages, how do you keep track of which pages link to others, and what do you do when you want to change a page's filename or location? Furthermore, when step 2 detects an error in a field, how do you redisplay the original form with an error message but preserve the values the user has already filled in? Struts, an open-source Model-View-Controller framework, makes life easier for developers by helping with all of these issues.

Background on Struts and Tiles

Craig McClanahan, the technical lead on the Apache Tomcat project, started the Struts project as a way to scratch an itch. It has grown quite popular as the preferred and officially sanctioned open-source implementation of a Model-View-Controller framework. It enjoys support from both Sun and IBM in the form of distribution with shipping products. Because Craig is active in development of both Tomcat and Struts, Struts will continue to be highly compatible with the reference implementation for the JSP and Servlet specifications, and thus highly compatible with all J2EE application servers.

Malcolm Davis's developerWorks article on Struts covers functionality through Struts 0.9; for brevity's sake, I will discuss only changes and Struts topics that he didn't cover. The current release version of Struts is 1.0.2, but there is a beta version, labeled 1.1-b1, that has been available since March 19, 2002. Because the beta version represents a code freeze in which only bug fixes are being worked on, there is an extremely good chance that the final version of 1.1 will be out shortly, a sentiment that has been echoed on the Struts mailing lists. Therefore, any new projects making use of Struts should probably begin on the 1.1 code base, and that is what I will cover.

Useful additions to the Struts framework since version 0.9 include improved form validation functionality, the ability to specify form elements via XML declarations, and the ability to dynamically define bean properties. Probably the most prominent addition, however, has been the incorporation of the Tiles templating library into the Struts distribution.

Have you ever wanted an easier way to create a set of pages (or perhaps a whole application) with a consistent user interface -- the same navigation bar, header, footer, and so on -- on every page? What about a way to display portlet-like rectangles of content within a larger page of content? The Tiles framework lets you accomplish both tasks and more. Through a central XML file defining screens and a set of tags that can be embedded in JSP pages to insert static and dynamic content, the Tiles framework allows you to build componentized views and assemble them as you like, thus aiding flexibility, reusability, consistency, and maintainability.

Struts and Tiles interact well because the developers of the two projects have recognized their complementary nature and decided to cooperate. A developer can specify a Tiles page definition as the target view (a forward in Struts parlance) of a Struts action. Because both Struts and Tiles conform to the JSP tag library specifications, Struts tags and Tiles tags can be intermixed in JSP pages.

You are likely itching to try out the Tiles framework and see exactly what it can do.

Struts and Tiles are aids for Web development, so you'll need to set up a Web container to experiment with them; the process for setting up Tomcat as your container and then the Struts and Tiles packages is described in the next section, in step-by-step fashion. These instructions also show you how to install this article's sample code. Once you've finished with that, you're ready to continue with the article. The Example 1 application doesn't take advantage of Struts and Tiles; it demonstrates the page-centric approach. By comparing it with Example 2, you'll see how much more structured and manageable Struts and Tiles can make your Web development. Finally, Example 3 demonstrates how straightforward it is to add functionality to a Struts and Tiles Web application that's already up and running.

Installing Struts and Tiles

The following instructions have been tested on Linux with the J2SE 1.4 SDK, Ant 1.4.1, Tomcat 4.0.3, and Struts 1.1-b1. If you encounter difficulties with different versions of these packages, you may want to change to the versions specified here to get acquainted with Struts and Tiles setup and development.

Start up the Tomcat server by typing ./startup.sh (if you are running UNIX) or ./startup.bat (if you are running Windows).

Verify that Tomcat is up and running properly by pointing your Web browser to http://localhost:8080/examples. The Examples application is shipped with Tomcat by default. If it doesn't work, then something went wrong with Tomcat; see the Tomcat documentation to resolve the issue.

Hello, World: A first attempt

To take a look at our first example, follow these steps:

Change to the EX1_INSTALL directory.

Edit the build.xml file and fill in an appropriate value for tomcat.install.dir. The value can either be an absolute or a relative path, although if you are inexperienced with how Ant works, it is probably best to use an absolute path.

Type ant deploy. This will build the first example application into a WAR file ready for deployment, then deploy it to Tomcat. If you get an error indicating that Ant could not be found, see step 3 in the Installing Struts and Tiles section and make certain that Ant is in your path.

Point your Web browser to http://localhost:8080/ex1. You should see a "Hello, World" page.

The Example 1 Web application is a very simple illustration of common Web application functionality. All but the simplest of applications require a consistent user interface from page to page. Usually this means that all pages have a common logo, top banner, top or left navigation bar, body, and footer. In Example 1, I have purposely hard-coded the common items in each page to demonstrate a point. Beginners at Web application development typically add new pages of functionality by copying and pasting existing code into new documents. It is easy to see how this approach can quickly become unwieldy. With every addition, the process of changing common page elements, such as a menu or a logo, takes longer and is more prone to error. Clearly, the copy-and-paste approach is a terrible model for any application that will grow beyond a few pages.

Astute readers will realize that JSP technology provides functionality to include content from other servlets and pages. Why don't we just use the tag to incorporate common elements? That would certainly make those elements easier to change. If you need to change the menu, just change the file containing the menu. All other pages use the tag to pull in the content from the menu, so they automatically pick up the changes. But this approach falls short when the actual layout needs to change or when files and directories need to be reorganized. When you decide to change the layout under a page-centric model, you must make a change to every single page, because even though access to the common elements has been centralized, the HTML code describing the layout itself (what the elements are and where they are positioned) is still present in each page. Similarly, when you decide to change the filename or the location of a file that contains content for one of the common elements, you must change every single file that uses the element in question. The reason? Every file addresses each common element by a fixed physical filename rather than a logical object name. Thus, you must update every reference to the physical filenames. These problems are addressed by Tiles view components.

If you look closely at index.jsp and form1.jsp, the two JSP files comprising the application, another drawback will be apparent: the error handling is quite awkward. The error handling code is in form1.jsp, where I must repeat the display code and add code to insert the values the user entered in the previous form screen (index.jsp). If the user profile fields ever change, or if the display of the input form ever changes, I must update code in two places. I could combine the error handling portion of form1.jsp with the initial form display in index.jsp, but I would still need to do extra work to set the field values to empty strings upon the initial form load, and I would still need to have a physical filename for the final static display of the user profile, which means that the application structure would remain fragile when change occurred. This drawback of awkward form handling is addressed by Struts form automation.

Hello, World: New and improved

Now, let's look at the Struts and Tiles version of the example Web application we just saw. Perform the following steps:

Change to the EX2_INSTALL directory.Edit the build.xml file and fill in appropriate values for struts.install.dir and tomcat.install.dir.

Type ant deploy. This will build the second example application into a WAR file ready for deployment, then deploy it to Tomcat. If you see an error about not being able to copy files, check to make sure that the struts.install.dir and tomcat.install.dir properties were set appropriately in step 2.

Point your Web browser to http://localhost:8080/ex2. You should see a "Hello, World" page.

You're probably saying,"What's going on here? There are a lot more files." As with mosttechnologies that impose more order and structure, with Struts and Tiles there is a startup cost in the form of administrative files. For a small project of a few pages, this additional overhead may not make sense. As the project grows, however, the Struts and Tiles approach really begins to shine. Let's take it piece by piece. I won't cover EX2_INSTALL/src/WEB-INF/web.xml; although this file is substantially different from its Example 1 counterpart, most of the lines are boilerplate,and understanding the important changes since the earlier article on Struts are the use of DynaActionForm in the section and the use of tiles as targets in the section. In previous versions of Struts, you had to define a Java class for every form bean that you used. You could share form beans between different HTML forms only if they shared fields. Overall, the requirement of a Java class for every formbean was a big pain. Now you can specify the form bean's properties

in the struts-config.xml file, and presto! The bean is automatically created without need for a separate file housing a Java class. The dynamic form beans are treated like Hashtable objects with strongly

typed objects for values. As for action mappings, the ability to specify a tile as a target was a completely logical addition once the decision was made to incorporate the Tiles library into the Struts distribution. You can see the tile targets (tile.profileInput and tile.profileOutput) in the action mapping for the profile form. The tiles are specified in the input attribute and the path attribute. Note that the ability to specify a tile target remedies the problem of changing filenames and locations mentioned in my analysis of the page-centric model: tile targets are virtual or logical names rather than physical names.

Tiles are specified in tags. You can name the definitions anything you want, and the name attribute does not have to be a substring match of the path attribute. I have chosen the name rootLayout for the first definition to indicate that it is the base layout that pages in the application will follow. Note the path: /tiles-layout/rootLayout.jsp. If you look at /tiles-layout/rootLayout.jsp under EX2_INSTALL/src/web, you will see how simple and clean the layout is. A user interface designer would love it. Note also that it contains no code, so the user interface designer can make changes without worrying about breaking anything.

The tags in rootLayout.jsp correspond to the tags within the rootLayout definition in tiles-defs.xml. Note that the tags each have an attribute representing a logical name. Each logical name maps to a name and value specified using a tag in the tag within tiles-defs.xml. By using logical rather than physical names in the rootLayout.jsp page and by consolidating physical names in tiles-defs.xml, we make changes to filenames and project filesystem organization manageable.

The real savings in time and the most significant increase in adaptability comes with inheritance of layouts, another Struts feature. In tiles-defs.xml, the section below the "Page definitions" banner has two pages: tile.profileInput and tile.profileOutput. The names are arbitrary and you don't need the tile. prefix if you don't like it (though the name you use must match targets specified in the struts-config.xml file). The names should match the path attribute in the tags in struts-config.xml. The names should also match the input attribute in the tags in struts-config.xml. The extends attribute in the tag is where the magic that delivers agility to user interface development happens. By specifying a master layout and extending it, you have the flexibility of not only changing common elements like topBanner, topMenu, panel1, panel2, and other components, but also the freedom to put different elements on the page and change where they are located. For instance, you could add panel4, a tile to display the local weather, below panel2 on the left side of the page. As long as the weather code does not require any input from the user or interaction with other components on the page, you could make the addition of panel4 without any changes whatsoever to the application business logic, or even any changes to JSP pages other than rootLayout.jsp.

More complex uses of Struts and Tiles

Space does not permit me to discuss other useful aspects of the Struts and Tiles package in depth, but I do want to touch on them briefly so you can get a sense of what Struts and Tiles can do. Perhaps these can be the topics of future articles if there is sufficient demand.

You will note that in both the examples in this article, I coded some basic validation of the first name, last name, favorite color, and birthdate. Simple validation of the sort I employed could actually be performed by Struts using its format validation, thus saving time spent coding Java statements. It is possible, for instance, to check for empty strings, to check that a string matches a date, or to check for a match with some other regular expression. You will find it very handy to use format validation as a first-level check with which your program can eliminate obvious errors before checking for adherence to more complex business logic rules. Struts provides a validator package that is configured via a validator.xml descriptor in the WEB-INF directory. I have not included the validator package in the two examples; if you would like to try it out, it's included with the Struts 1.1-b1 bundle.

Another useful feature of the Struts and Tiles package is integration with container-managed security. Many of you are using application servers such as IBM WebSphere or JBoss. These servers facilitate authentication and authorization by handling many of the routine tasks involved in security, freeing you from having to code them. Struts allows you to conditionally display different bits of bean data to different users based on user role, which is determined by automatically querying the container. Similarly, the Tiles framework allows you to conditionally display different view components to different users based on user role, again determined by automatically querying the container. I use these features in my own applications to display different menus to administrators, regular users, or guests.

Finally, I have not touched on internationalization in my examples, but it is a real concern for people building large applications that will ultimately be published in multiple languages. Struts lets you create message resource properties files that specify text for labels, titles, and other output. If you have populated a message resource properties file with all outputs in several different languages, you can change all widgets and hard-coded text to the appropriate language with a simple change of the language setting, even on the fly.

Future directions

Where do Struts and Tiles go from here? Two changes on the horizon promise to make Struts and Tiles even more useful. The first is a workflow management system. You have likely dealt with many multistep business processes; coding them in a Web application today can be a major pain because you have to coordinate the different steps, each of which is a separate method within a Java class or a separate servlet or JSP page. Tools in the commercial marketplace help with managing multistep business processes by enabling developers to model them, then automatically generating Java code that serves as scaffolding. The Struts workflow management system will provide similar functionality, allowing developers to script business processes that span multiple pages in a Web application and specify how the pages interact through a central, rules-based system.

The other, equally promising upcoming change is the incorporation of Struts tags in the JSP Standard Tag Library, a Jakarta project that seeks to produce a collection of useful custom tags that simplify and speed development of Web applications written with JSP pages. What this means is that Web applications will be easier than ever to develop because you will have a prewritten library of code to draw upon using the standard mechanism of custom tags. Also, the project's incorporation within the JSP STL standard ensures even wider distribution and availability of the Struts and Tiles functionality, meaning portability of skills for developers moving between projects and easy access to skilled talent for companies looking to develop Web applications.

What can you take away from this article?After reading this article, the following key points should stay with you:

MVC is a good architecture for developing robust Web software. A page-oriented architecture is simple, but fails as an application grows in size and scope. MVC is a proven model for larger applications.

Struts and Tiles provide a solid base for complex applications. Struts and Tiles adhere to the MVC paradigm and allow developers to create an application that can manageably grow with the needs of the business.

Struts and Tiles enjoy strong industry support, as well as popularity and momentum among developers. This ensures broad adoption and protects the investment of time and money made by developers and companies.

With Struts and Tiles, you'll find it easier to unleash your own Web applications on the world!

本文来源:https://www.2haoxitong.net/k/doc/b50a192fbd64783e09122bab.html

《教学管理系统设计与实现外文资料翻译.doc》
将本文的Word文档下载到电脑,方便收藏和打印
推荐度:
点击下载文档

文档为doc格式