【正文】
The extends attribute in the definition name=x extends=y/ 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 mon elements like topBanner, topMenu, panel1, panel2, and other ponents, 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 ponents 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 . 7 More plex 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 firstlevel check with which your program can eliminate obvious errors before checking for adherence to more plex business logic rules. Struts provides a validator package that is configured via a descriptor in the WEBINF directory. I have not included the validator package in the two examples。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 the file and fill in appropriate values for and . 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 and properties were set appropriately in step 2. Point your Web browser to You should see a Hello, World page. You39。re ready to continue with the article. The Example 1 application doesn39。s developerWorks article on Struts covers functionality through Struts 。其中的意義在于: Web 應(yīng)用程序的開發(fā)將比過去任何時候都更容易,因為您可以通過使用定制標記的標準機制得到一個在此基礎(chǔ)上做進一步開發(fā)的預(yù)先編寫好的代碼庫。 Struts 讓您創(chuàng)建消息資源特性文件,這些文件指定標簽、標題和其它輸出的文本。在這兩個示例中我沒有包含該驗證器包;如果您想試一下,它包含在 Struts 包中。只要天氣代碼不需要用戶的任何輸入或與頁面上的其它組件交互,您就可以添加 panel4 而不必對應(yīng)用程序業(yè)務(wù)邏輯做任何更改,甚至不必更改除 以外的 JSP 頁面。 真正節(jié)省時間和適應(yīng)性方面最顯著的增強方面體現(xiàn)在布局的繼承,這是 Struts 的另一個特性。注:路徑是 /tileslayout/。象處理 Hashtable 對象一樣,用值的強類型對象來處 理動態(tài)表單 bean。對于只有少許頁面的小項目,這一額外開銷可能微不足道。請執(zhí)行下列步驟: 轉(zhuǎn)至 EX2_INSTALL 目錄。因此,必須更新每個對物理文件名的引用。 敏銳的讀者會 認識到 JSP 技術(shù)提供了包括來自其它 servlet 和頁面中的內(nèi)容的功能。 示例 1 Web 應(yīng)用程序非常簡單,它演示了常見的 Web 應(yīng)用程序功能。如果 Examples 不工作,則 Tomcat 發(fā)生故障;請參閱 Tomcat 文檔來解決問題。通過將它與示例 2 比較,您會看到 Struts 和 Tiles 將如何極大地提高您的 Web 開發(fā)的結(jié)構(gòu)化程度和可管理性。開發(fā)人員可以指定 Tiles 頁面定義作為 Struts 操作的目標視圖(按照 Struts 的說法是一個 forward)。目前的 Struts 發(fā)行版本是 ,但自 2020 年 3 月 19 日起有一個標記為 的 beta 測試版可供使用。您正在閱讀的這篇文章討論自 Malcolm 的文章發(fā)表以來對 Struts 所做的更改,包括 Tiles 庫。正如很多人所知,表單驗證的過程類似于這樣: 顯示表單;等待用戶填寫然后提交數(shù)據(jù)。結(jié)果,在特定解決方案中的應(yīng)用程序混合了 HTML 代碼與應(yīng)用程序邏輯。 JSP 技術(shù)和標記的引入稍微改善了這種更改問題,因為能夠?qū)⑦壿?和顯示分離。 在新頁面上向用戶顯示處理的結(jié)果或下一步(可能是另一個表單)。 Struts 和 Tiles 的背景知識 Craig McClanahan 是 Apache Tomcat 項目的技術(shù)主管,他創(chuàng)立 Struts 項目以滿足對這方面的渴望。 自版本 以來對 Struts 框架的有用添加包括經(jīng)改進的表單驗證功能、可以通過 XML 聲明來指定表單元素和可以動態(tài)地定義 bean 特性。如果您希望在自己安裝本文的示例前先了解這些示例的運行情況,可以看看它們在帶有嵌入式 Tomcat 的 JBoss 服務(wù)器 上是怎樣運行的。如果因這些軟件包的版本不同而遇到困難, 3 您可能需要使用上面所指定的版本,以便開始了解 Struts 和 Tiles 的設(shè)置和開發(fā)。盡管該值可以是絕對路徑,也可以是相對路徑;但如果您不了解 Ant 是如何工作的,或許最好使用絕對路徑。在示例 1 中,我有意對每一頁面中的公共項進行硬編碼,以便說明這一點。所有其它頁面只需使用 jsp:include/ 標記就可以得到菜單中的內(nèi)容,這樣這些頁面可以自動獲得對菜單的更改。錯誤處理代碼是在 中,其中我必須重復(fù)顯示代碼,并添加代碼以插入用戶在前一表單屏幕( )中輸入的值。這將把第二個示例應(yīng)用程序構(gòu)建到 WAR 文件中以備部署,然后將它部署至 Tomcat。 在 EX2_INSTALL/src/WEBINF/ 中,自先前有關(guān) Struts 的文章以來重要的更改有在 formbeans/ 節(jié)中 DynaActionForm 的使用及在 actionmappings/ 節(jié)中 tile 作為目標的使用。在 input 屬性和 path 屬性中指定 tile。還請注意:它不包含任何代碼,所以用戶界 面設(shè)計人員在進行更改時不必擔心破壞什么。這些名稱應(yīng)該與 中 forward name=x path=y/ 標記中的 path 屬性匹配。 6 您會在本文的兩個示例中注意到,我編碼了名、姓、喜歡的顏色和出生日期等基本驗證。這 些服務(wù)器通過處理安全性中所涉及的許多日常任務(wù)使認證和授權(quán)更便捷,讓您擺脫必須對它們進行的編碼。第一個更改是工作流管理系統(tǒng)。 wait for user to fill it out and submit data. Check for valid field values。t cover. The current release version of Struts is , but there is a beta version, labeled , that has been available since March 19, 2020. 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 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 code base, and that is what I will cover. Useful additions to the Struts framework since version 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 portletlike rectangles of content within a larger page of