【正文】
旦確定將 Tiles 庫(kù)合并到 Struts 分發(fā)版之后,那么指定一個(gè) tile 作為目標(biāo),就完全是增加一項(xiàng)邏輯而已。您會(huì)在概要信息表單的操作映射中看到 tile 目標(biāo)( 和 )。在 input 屬性和 path 屬性中指定 tile。注:可以指定 tile 目標(biāo)彌補(bǔ)了我在分析以頁(yè)面為中心的模型中提到的更改文件名和位置中的缺陷: tile 目標(biāo)是虛擬名稱(chēng)或邏輯名稱(chēng),而不是物理名稱(chēng)。 在 definition/ 標(biāo)記中指定 tile。您可以將定義命名為任何希望的名稱(chēng),而且 name 屬性不必是與 path 屬性匹配的子字符串。我為第一個(gè)定義選擇了名稱(chēng) rootLayout ,以表明它是應(yīng)用程序中的頁(yè)面要遵循的基本布局。注:路徑是 /tileslayout/。如果查看 EX2_INSTALL/src/web 下的 /tileslayout/,您會(huì)看到這種布局是多么的簡(jiǎn)單整齊。用戶(hù)界面的設(shè)計(jì)人員會(huì) “愛(ài)上它 ”。還請(qǐng)注意:它不包含任何代碼,所以用戶(hù)界 面設(shè)計(jì)人員在進(jìn)行更改時(shí)不必?fù)?dān)心破壞什么。 中的 tiles:insert/ 標(biāo)記對(duì)應(yīng)于 中 rootLayout 定義內(nèi)的 put/ 標(biāo)記。注:每個(gè) tiles:insert attribute=x/ 標(biāo)記都有一個(gè)表示邏輯名稱(chēng)的屬性。每個(gè)邏輯名稱(chēng)映射至通過(guò)在 內(nèi)的 definition/ 中使用 put name=x value=y/ 標(biāo)記指定的名稱(chēng)和值。通過(guò)在 頁(yè)面中使用邏輯名稱(chēng),而非物理名稱(chēng),并通過(guò)在 中統(tǒng)一物理名稱(chēng),我們就可以更改文件名,并使項(xiàng)目文件系統(tǒng)的組織易于管理。 真正節(jié)省時(shí)間和適應(yīng)性方面最顯著的增強(qiáng)方面體現(xiàn)在布局的繼承,這是 Struts 的另一個(gè)特性。在 中, “Page definitions”欄下面的節(jié)有兩個(gè)頁(yè)面: 和 。這些名稱(chēng)是任意的,如果您不喜歡 tile. 前綴,可以不使用它(但是您使用的名稱(chēng)必須與 文件中指定的目標(biāo)相匹配)。這些名稱(chēng)應(yīng)該與 中 forward name=x path=y/ 標(biāo)記中的 path 屬性匹配。這些名稱(chēng)還應(yīng)該與 中 action ... input= .../ 標(biāo)記中的 input 屬性匹配。在開(kāi)發(fā)用戶(hù)界面時(shí), definition name=x extends=y/ 標(biāo)記中的 extends 屬性是體現(xiàn)開(kāi)發(fā)靈活性的地方。通過(guò)指定主布局并擴(kuò)展它 ,您不僅能靈活更改象 topBanner 、 topMenu 、 panel1 、 panel2 這樣的公共元素及其它組件 , 還能隨意地將不同元素放在頁(yè)面上及更改它們的位置。例如,您可以添加 panel4 (一個(gè) tile)以在頁(yè)面左側(cè)的 panel2 下顯示本地天氣。只要天氣代碼不需要用戶(hù)的任何輸入或與頁(yè)面上的其它組件交互,您就可以添加 panel4 而不必對(duì)應(yīng)用程序業(yè)務(wù)邏輯做任何更改,甚至不必更改除 以外的 JSP 頁(yè)面。 Struts 和 Tiles 更復(fù)雜的使用 篇幅 所限,不允許我再深入討論 Struts 和 Tiles 包其它一些實(shí)用的方面,但我真的想簡(jiǎn)要地談一下,這樣您可以對(duì) Struts 和 Tiles 可以做些什么有所了解。如果有足夠的需求,可能這些內(nèi)容會(huì)是將來(lái)文章的主題。 6 您會(huì)在本文的兩個(gè)示例中注意到,我編碼了名、姓、喜歡的顏色和出生日期等基本驗(yàn)證。我所采用的這類(lèi)簡(jiǎn)單驗(yàn)證實(shí)際上可由 Struts 通過(guò)使用它的格式驗(yàn)證來(lái)執(zhí)行,這樣節(jié)省了花在編碼 Java 語(yǔ)句的時(shí)間。例如,檢查是否是空字符串、檢查字符串是否與日期相匹配或檢查是否與其它某些正則表達(dá)式匹配等,這都是可行的。您會(huì)發(fā)現(xiàn)將格式驗(yàn)證用作第一級(jí)檢查會(huì)是十分方便的,在檢查是否符合更復(fù)雜的業(yè)務(wù)邏輯規(guī)則之前,程序可用第一級(jí)檢查來(lái)消除一些明顯錯(cuò)誤。 Struts 提供了一個(gè)驗(yàn)證器包,可以通過(guò) WEBINF 目錄中的 描述符來(lái)配置該包。在這兩個(gè)示例中我沒(méi)有包含該驗(yàn)證器包;如果您想試一下,它包含在 Struts 包中。 Struts 和 Tiles 包的另一個(gè)有用特性是與容器管理的安全性相集成。很多人都在使用諸如 IBM WebSphere 或 JBoss 之類(lèi)的應(yīng)用程序服務(wù)器。這 些服務(wù)器通過(guò)處理安全性中所涉及的許多日常任務(wù)使認(rèn)證和授權(quán)更便捷,讓您擺脫必須對(duì)它們進(jìn)行的編碼。 Struts 允許您根據(jù)用戶(hù)角色有條件地向不同用戶(hù)顯示 bean 數(shù)據(jù)的不同位,這可以通過(guò)自動(dòng)查詢(xún)?nèi)萜鱽?lái)決定。同樣地, Tiles 框架允許您根據(jù)用戶(hù)角色有條件地向不同用戶(hù)顯示不同的視圖組件,這也是通過(guò)自動(dòng)查詢(xún)?nèi)萜鱽?lái)決定的。我在自己的應(yīng)用程序中使用這些特性向管理員、常規(guī)用戶(hù)或 guest 用戶(hù)顯示不同的菜單。 最后要說(shuō)明,我在示例中沒(méi)有談到國(guó)際化,但對(duì)于那些構(gòu)建將來(lái)最終要以多種語(yǔ)言發(fā)布的大型應(yīng)用程序的人來(lái)說(shuō),這實(shí)際是 一個(gè)重要方面。 Struts 讓您創(chuàng)建消息資源特性文件,這些文件指定標(biāo)簽、標(biāo)題和其它輸出的文本。如果您用幾種不同語(yǔ)言的輸出填入消息資源特性文件的話(huà),只要簡(jiǎn)單更改語(yǔ)言設(shè)置就能將所有窗口構(gòu)件和硬編碼的文本更改成適當(dāng)語(yǔ)言,甚至在應(yīng)用程序運(yùn)行中也可以更改。 未來(lái)的方向 Struts 和 Tiles 將走向何方呢?剛出現(xiàn)的兩個(gè)更改必定會(huì)使 Struts 和 Tiles 更加有用。第一個(gè)更改是工作流管理系統(tǒng)。您很可能處理過(guò)許多多步驟的業(yè)務(wù)過(guò)程;目前,在 Web 應(yīng)用程序中對(duì)它們進(jìn)行編碼可以是一個(gè)主要的麻煩,因?yàn)槟仨殔f(xié)調(diào) 不同的步驟,而每一步驟都是 Java 類(lèi)中的一個(gè)單獨(dú)方法或是一個(gè)單獨(dú)的 servlet 或 JSP 頁(yè)面。商業(yè)市場(chǎng)中的工具通過(guò)使開(kāi)發(fā)人員能對(duì)多步驟的業(yè)務(wù)過(guò)程進(jìn)行建模,然后自動(dòng)生成 Java 代碼作為進(jìn)一步開(kāi)發(fā)的基礎(chǔ),來(lái)幫助管理這些過(guò)程。 Struts 工作流管理系統(tǒng)將提供類(lèi)似的功能,使開(kāi)發(fā)人員能夠編寫(xiě)業(yè)務(wù)過(guò)程的腳本,這些過(guò)程在 Web 應(yīng)用程序中跨多個(gè)頁(yè)面,并指定這些頁(yè)面如何通過(guò)核心的基于規(guī)則的系統(tǒng)進(jìn)行交互。 另一個(gè)同樣有望即將出現(xiàn)的更改是將 Struts 標(biāo)記合并到 JSP 標(biāo)準(zhǔn)標(biāo)記庫(kù)( JSP Standard Tag Library)中,這個(gè)庫(kù)是 Jakarta 項(xiàng)目,它試圖產(chǎn)生一個(gè)有用的定制標(biāo)記集合,從而簡(jiǎn)化并加快用 JSP 頁(yè)面編寫(xiě) Web 應(yīng)用程序的開(kāi)發(fā)。其中的意義在于: Web 應(yīng)用程序的開(kāi)發(fā)將比過(guò)去任何時(shí)候都更容易,因?yàn)槟梢酝ㄟ^(guò)使用定制標(biāo)記的標(biāo)準(zhǔn)機(jī)制得到一個(gè)在此基礎(chǔ)上做進(jìn)一步開(kāi)發(fā)的預(yù)先編寫(xiě)好的代碼庫(kù)。而且, JSP STL 標(biāo)準(zhǔn)中這個(gè)項(xiàng)目的合并甚至確保了 Struts 和 Tiles 功 7 能更為廣泛的分發(fā)和可用,這意味具有這種技能的開(kāi)發(fā)人員在眾多項(xiàng)目中大有作為,而且公司更加容易獲得開(kāi)發(fā) Web 應(yīng)用程 序的熟練人才。 1 Struts and Tiles aid ponentbased 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 shortings. Notably, certain operations mon 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 you39。re only using JSP pages for this process, you39。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 page39。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 opensource ModelViewController 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 2 and officially sanctioned opensource implementation of a ModelViewController 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 patible with the reference implementation for the JSP and Servlet specifications, and th