在以前的開發(fā)過程中,我常常遭遇這樣的經(jīng)歷,要完成某個任務(wù),首先由頁面設(shè)計人員來設(shè)計頁面原型,再交給程序員進(jìn)行處理。很多情況下,我們總是要整理一個完整的頁面。事實上,我們做了太多的重復(fù)工作。很多頁面的結(jié)構(gòu)是完全相同的,頁面的頭,尾,導(dǎo)航菜單都是相同的,只是主體內(nèi)容部分不同而已。重復(fù)整理這些相同的部分是一個不小的負(fù)擔(dān),我們很自然的想到,要是這些頁面元素能夠復(fù)用就會省下很多事。
Struts 內(nèi)置了Tiles,利用它可以很好復(fù)用頁面,使用起來就像我們復(fù)用類一樣。
使用Tiles
在以前的開發(fā)過程中,我常常遭遇這樣的經(jīng)歷,要完成某個任務(wù),首先由頁面設(shè)計人員來設(shè)計頁面原型,再交給程序員進(jìn)行處理。很多情況下,我們總是要整理一個完整的頁面。事實上,我們做了太多的重復(fù)工作。很多頁面的結(jié)構(gòu)是完全相同的,頁面的頭,尾,導(dǎo)航菜單都是相同的,只是主體內(nèi)容部分不同而已。重復(fù)整理這些相同的部分是一個不小的負(fù)擔(dān),我們很自然的想到,要是這些頁面元素能夠復(fù)用就會省下很多事。
Struts 內(nèi)置了Tiles,利用它可以很好復(fù)用頁面,使用起來就像我們復(fù)用類一樣。
使用Tiles
1.如果頁面內(nèi)容對頁面進(jìn)行劃分,一個頁面可以分成很多塊,如header , footer , menu, content,等,如果新聞門戶網(wǎng)站,content可以進(jìn)一步細(xì)分,footall, entertainment, 等。
這里僅僅作演示,我將頁面分header , footer ,content。
展開Configuration Files,打開tile-defs.xml,定義一個最本的頁面模板,其它頁面在這個基礎(chǔ)上進(jìn)行擴(kuò)充(這有點像定義一個基類,其它則子類繼承基類,以獲得基類的特性)。
﹤definition name=".mainLayout" path="/layouts/default.jsp"> ﹤put name="title" value="Struts Demo Application" /> ﹤put name="header" value="/layouts/header.jsp" /> ﹤put name="footer" value="/layouts/footer.jsp" /> ﹤put name="body" value=".portal.body" /> ﹤/definition>
|
其它頁面以這個為基礎(chǔ),繼承這個模板內(nèi)容,可以重新定義title和body內(nèi)容。
﹤definition name="logonPage" extends=".mainLayout"> ﹤put name="title" value="Logon Page" /> ﹤put name="body" value="/layouts/logonBody.jsp" /> ﹤/definition> ﹤definition name="registerPage" extends=".mainLayout"> ﹤put name="title" value="New Registeration " /> ﹤put name="body" value="/layouts/registerBody.jsp" /> ﹤/definition> ﹤definition name="indexPage" extends=".mainLayout"> ﹤put name="title" value="Home Page " /> ﹤put name="body" value="/layouts/indexBody.jsp" /> ﹤/definition> ﹤definition name="registeredSuccessPage" extends=".mainLayout"> ﹤put name="title" value="Registered Successfully" /> ﹤put name="body" value="/layouts/registerSuccessBody.jsp" /> ﹤/definition>
|
事實,這和Java中類的繼承非常類似。
2.相應(yīng)的jsp頁面,在Web pages下新建一個layouts目錄。默認(rèn)模板(即default.jsp)內(nèi)容如下。
﹤html> ﹤head> ﹤meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> ﹤title>﹤tiles:getAsString name="title"/>﹤/title> ﹤/head> ﹤body> ﹤div id="header">﹤tiles:insert attribute="header"/>﹤/div> ﹤div id="content">﹤tiles:insert attribute="body"/>﹤/div> ﹤div id="footer">﹤tiles:insert attribute="footer"/>﹤/div> ﹤/body> ﹤/html>
|
其中,getAsString表示title以一個字符串的形式插入模板。
header.jsp中定義了頁面標(biāo)題。
﹤%@page contentType="text/html"%> ﹤%@page pageEncoding="UTF-8"%> ﹤div>﹤h1>Struts Demo Application﹤/h1>﹤/div>
|
footer.jsp內(nèi)容版本聲明,如下:
﹤%@page contentType="text/html"%> ﹤%@page pageEncoding="UTF-8"%> ﹤div>Copyright 2007© hantsy<hantsy@tom.com>﹤/div>
|
其它頁面,各復(fù)制一份到layouts目錄中,頁面只留body之間的內(nèi)容。
如indexBody.jsp內(nèi)容如下:
﹤%@page contentType="text/html"%> ﹤%@page pageEncoding="UTF-8"%> ﹤%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> ﹤%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> ﹤%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> ﹤h1>Home Page﹤/h1> Welcome, ﹤logic:notPresent name="username" scope="session"> Guest !﹤html:link forward="newLogon">Logon ﹤/html:link> or ﹤html:link forward="newRegister">Register Now...﹤/html:link> ﹤/logic:notPresent> ﹤logic:present name="username" scope="session"> ﹤bean:write name="username" scope="session"/>, ﹤html:link forward="logout">Log Out..﹤/html:link> ﹤/logic:present>
|
3.修改Struts配置文件,將所有jsp文件路徑替換成tile配置文件中定義的名稱。
﹤action-mappings> ﹤action path="/logout" type="com.myapp.web.LogoutAction"/> ﹤action input="registerPage" name="UserForm" path="/register" scope="session" type="com.myapp.web.RegisterAction"> ﹤forward name="success" path="registeredSuccessPage"/> ﹤/action> ﹤action input="logonPage" name="UserForm" path="/logon" scope="session" type="com.myapp.web.LogonAction"/> ﹤action path="/index" forward="indexPage"/> ﹤action path="/newLogon" forward="logonPage"/> ﹤action path="/newRegister" forward="registerPage"/> ﹤/action-mappings>
|
值得注意的是,使用Tile必須的兩個最重要的配置,NetBeans在創(chuàng)建項目時已經(jīng)幫我們自動完成了。
一個是注冊Tile插件,打開Struts配置文件struts-config.xml我們就可以看到。
﹤plug-in className="org.apache.struts.tiles.TilesPlugin" > ﹤set-property property="definitions-config" value="/WEB-INF/tiles-defs.xml" /> ﹤set-property property="moduleAware" value="true" /> ﹤/plug-in>
|
另外一個是,重新定義controller,將processorClass改成使用 org.apache.struts.tiles.TilesRequestProcessor。
﹤controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/>
4.運行項目進(jìn)行測試。
使用Sitemesh
Sitemesh是www.opensymphony.org帶來的另一款優(yōu)秀的頁面布局工具。它不專門針對Struts ,甚至其它語言的web程序也可以使用它。
它使用Decorator模式達(dá)到預(yù)期效果。這里可以將頁面分為兩類,decorator(修飾)和decoratored(被修飾)。這就好比有一個相框和各種不同可以用來變換的相片,當(dāng)相框中放入不同的相片,就得到不同的視覺效果。
1.下載并安裝Sitemesh。
從www.opensymphony.org下載最新的sitemesh 2.3,解壓到硬盤。
將﹤sitemesh>/lib下的common- collections.jar,freemarker.jar, velocity-deps-1.3.1.jar,velocity-tools-view-1.3.1.jar 和﹤sitemesh>/dist下sitemesh-2.3.jar添加到項目Libraries中。
將﹤sitemesh>\src\etc\tld\jsp1.2的sitemesh-decorator.tld和sitemesh-page.tld復(fù)制一份到項目的web/WEB-INF/下面。
2.配置sitemesh。
從configurations中打開web.xml,定義Sitemesh Filter。
﹤filter> ﹤filter-name>sitemesh﹤/filter-name> ﹤filter-class> com.opensymphony.module.sitemesh.filter.PageFilter ﹤/filter-class> ﹤/filter> ﹤filter-mapping> ﹤filter-name>sitemesh﹤/filter-name> ﹤url-pattern>/*﹤/url-pattern> ﹤/filter-mapping>
|
在下面的jsp-config中添加sitemesh的taglib定義,在最新的jsp 2.0標(biāo)準(zhǔn),這不是必須的。
﹤taglib> ﹤taglib-uri>sitemesh-page﹤/taglib-uri> ﹤taglib-location> /WEB-INF/lib/sitemesh-page.tld ﹤/taglib-location> ﹤/taglib> ﹤taglib> ﹤taglib-uri>sitemesh-decorator﹤/taglib-uri> ﹤taglib-location> /WEB-INF/lib/sitemesh-decorator.tld ﹤/taglib-location> ﹤/taglib>
|
在web/WEB-INF在定義兩個基本的sitemesh配置文件,這兩個文件可以sitemesh包復(fù)制過來。
sitemesh.xml定義最基本的應(yīng)用規(guī)則。
﹤sitemesh> ﹤property name="decorators-file" value="/WEB-INF/decorators.xml"/> ﹤excludes file="${decorators-file}"/> ﹤page-parsers> ﹤parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" /> ﹤/page-parsers> ﹤decorator-mappers> ﹤mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper"> ﹤param name="property.1" value="meta.decorator" /> ﹤param name="property.2" value="decorator" /> ﹤/mapper> ﹤mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper"> ﹤/mapper> ﹤mapper class="com.opensymphony.module.sitemesh.mapper.AgentDecoratorMapper"> ﹤param name="match.MSIE" value="ie" /> ﹤param name="match.Mozilla [" value="ns" /> ﹤param name="match.Opera" value="opera" /> ﹤param name="match.Lynx" value="lynx" /> ﹤/mapper> ﹤mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper"> ﹤param name="decorator" value="printable" /> ﹤param name="parameter.name" value="printable" /> ﹤param name="parameter.value" value="true" /> ﹤/mapper> ﹤mapper class="com.opensymphony.module.sitemesh.mapper.RobotDecoratorMapper"> ﹤param name="decorator" value="robot" /> ﹤/mapper> ﹤mapper class="com.opensymphony.module.sitemesh.mapper.ParameterDecoratorMapper"> ﹤param name="decorator.parameter" value="decorator" /> ﹤param name="parameter.name" value="confirm" /> ﹤param name="parameter.value" value="true" /> ﹤/mapper> ﹤mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper"> ﹤/mapper> ﹤mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper"> ﹤param name="config" value="${decorators-file}" /> ﹤/mapper> ﹤/decorator-mappers> ﹤/sitemesh>
|
decorators.xml中定義具體的decortor應(yīng)用規(guī)則。
﹤?xml version="1.0" encoding="ISO-8859-1"?> ﹤decorators defaultdir="/decorators"> ﹤!-- Any urls that are excluded will never be decorated by Sitemesh --> ﹤excludes> ﹤pattern>/exclude.jsp﹤/pattern> ﹤pattern>/exclude/*﹤/pattern> ﹤/excludes> ﹤decorator name="main" page="main.jsp"> ﹤pattern>/*﹤/pattern> ﹤/decorator> ﹤decorator name="panel" page="panel.jsp"/> ﹤/decorators>
|
3.定義Decorator。
在Web Pages下新建一個目錄,命名為decorators,在這個新建的目錄中新建main.jsp,作為主要的decorator。
﹤%@page contentType="text/html"%> ﹤%@page pageEncoding="UTF-8"%> ﹤%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> ﹤%@ taglib uri="http://www.opensymphony.com/sitemesh/page" prefix="page" %> ﹤html> ﹤head> ﹤title>Demo-﹤decorator:title default="Struts Sitemesh Demo..." />﹤/title> ﹤!--link href="﹤%= request.getContextPath() %>/decorators/main.css" rel="stylesheet" type="text/css"--> ﹤decorator:head /> ﹤/head> ﹤body> ﹤div> ﹤table width="100%"> ﹤tr> ﹤td id="pageTitle"> ﹤h1> ﹤decorator:title />﹤/h1> ﹤/td> ﹤/tr> ﹤tr> ﹤td valign="top" height="100%"> ﹤decorator:body /> ﹤/td> ﹤/tr> ﹤tr> ﹤td id="footer"> ﹤page:applyDecorator page="/footer.html" name="panel" /> ﹤/td> ﹤/tr> ﹤/table> ﹤/div> ﹤/body> ﹤/html>
|
另外再定義一個panel。
﹤%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator" %> ﹤p> ﹤table width="100%" border=0 cellpadding=0 cellspacing=0> ﹤tr> ﹤td class="panelBody"> ﹤decorator:body /> ﹤/td> ﹤/tr> ﹤/table> ﹤/p>
|
文件中,decorator:title和decorator:body定義要被替換的內(nèi)容位置,應(yīng)用之后,分別替換成decoratored頁面的title和body標(biāo)簽中間的內(nèi)容。
下面將Web Pages下的頁面恢復(fù)成原貌。如index.jsp內(nèi)容如下。
﹤%@page contentType="text/html"%> ﹤%@page pageEncoding="UTF-8"%> ﹤%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> ﹤%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> ﹤%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> ﹤html> ﹤head>﹤title>HomePage ﹤/title>﹤/head> ﹤body> Welcome, ﹤logic:notPresent name="username" scope="session"> Guest !﹤html:link forward="newLogon">Logon ﹤/html:link> or ﹤html:link forward="newRegister">Register Now...﹤/html:link> ﹤/logic:notPresent> ﹤logic:present name="username" scope="session"> ﹤bean:write name="username" scope="session"/>, ﹤html:link forward="logout">Log Out..﹤/html:link> ﹤/logic:present> ﹤/body> ﹤/html>
|
同時還要修改Struts定義文件,將上一節(jié)中tiles定義的page名稱全部還要成具體的頁面路徑。
﹤action-mappings> ﹤action path="/logout" type="com.myapp.web.LogoutAction"/> ﹤action input="/register.jsp" name="UserForm" path="/register" scope="session" type="com.myapp.web.RegisterAction"> ﹤forward name="success" path="/registerSuccess.jsp"/> ﹤/action> ﹤action input="/logon.jsp" name="UserForm" path="/logon" scope="session" type="com.myapp.web.LogonAction"/> ﹤action path="/Welcome" forward="/welcomeStruts.jsp"/> ﹤action path="/index" forward="/index.jsp"/> ﹤action path="/newLogon" forward="/logon.jsp"/> ﹤action path="/newRegister" forward="/register.jsp"/> ﹤/action-mappings>
|
另外還要禁用tiles,以免和sitemesh發(fā)生沖突。注釋掉controller定義,讓Struts使用默認(rèn)的requestProcessor。
【編輯推薦】
- NetBeans 6.7 RC2發(fā)布 正式版月底出爐
- Eclipse還是Netbeans
- 使用Netbeans操作MySQL數(shù)據(jù)庫
- Ubuntu9.04下的Netbeans6.5.1字體無法抗鋸齒的解決辦法
- Netbeans6.7平臺Scala插件V1版發(fā)布