實(shí)際本章教程開始之前,讓我們看看由http://struts.apache.org給出的幾個(gè)定義:
Term | 描述 |
---|---|
tag | A small piece of code executed from within JSP, FreeMarker, or Velocity. |
template | A bit of code, usually written in FreeMarker, that can be rendered by certain tags (HTML tags). |
theme | A collection of templates packaged together to provide common functionality. |
我也建議去通過Struts2本土化章節(jié),因?yàn)槲覀儗⒉扇⊥瑯拥睦樱俅螆?zhí)行我們的練習(xí)。
當(dāng)使用Struts 2 標(biāo)簽如<s:submit...>,<s:textfield...>等在網(wǎng)頁中,Struts 2框架生成HTML代碼與預(yù)先設(shè)定的樣式和布局。 Struts 2自帶內(nèi)置的主題有三個(gè):
Theme | 描述 |
---|---|
simple theme | A minimal theme with no "bells and whistles". For example, the textfield tag renders the HTML <input/> tag without a label, validation, error reporting, or any other formatting or functionality. |
xhtml theme | This is the default theme used by Struts 2 and provides all the basics that the simple theme provides and adds several features like standard two-column table layout for the HTML, Labels for each of the HTML, Validation and error reporting etc. |
css_xhtml theme | This theme provides all the basics that the simple theme provides and adds several features like standard two-column CSS-based layout, using <div> for the HTML Struts Tags, Labels for each of the HTML Struts Tags, placed according to the CSS stylesheet. |
正如上面所提到的,如果不指定一個(gè)主題,然后Struts2中會(huì)使用默認(rèn)的XHTML主題。例如Struts 2中選擇標(biāo)簽:
<s:textfield name="name" label="Name" />
生成HTML標(biāo)記:
<tr> <td class="tdLabel"> <label for="empinfo_name" class="label">Name:</label> </td><td> <input type="text" name="name" value="" id="empinfo_name"/> </td> </tr>
這里empinfo struts.xml文件中定義動(dòng)作名稱。
可以指定主題Struts 2每一個(gè)標(biāo)簽的基礎(chǔ)上或指定的主題Struts 2使用,可以使用下列方法之一:
主題屬性的具體標(biāo)簽
主題屬性標(biāo)簽的周邊表單標(biāo)簽
頁面范圍的屬性,名為“主題”
請(qǐng)求范圍屬性名為“主題”
會(huì)話作用域?qū)傩悦麨?ldquo;主題”
應(yīng)用程序作用域的屬性命名為“主題”
在struts.properties struts.ui.theme屬性(默認(rèn)為XHTML)
以下語法指定他們?cè)跇?biāo)簽級(jí)別,如果愿意為不同的標(biāo)簽使用不同的主題:
<s:textfield name="name" label="Name" theme="xhtml"/>
因?yàn)樗皇欠浅?shí)用,每個(gè)標(biāo)簽的基礎(chǔ)上使用主題,所以干脆我們可以指定規(guī)則struts.properties文件中使用以下標(biāo)簽:
# Standard UI theme struts.ui.theme=xhtml # Directory where theme template resides struts.ui.templateDir=template # Sets the default template type. Either ftl, vm, or jsp struts.ui.templateSuffix=ftl
以下的結(jié)果是,我們拿起從本地化章節(jié)里我們使用默認(rèn)設(shè)置struts.ui.theme= XHTML的struts-default.properties文件中,默認(rèn)情況下,在 struts2-core.xy.z.jar文件,這是由主題。
對(duì)于一個(gè)給定的主題,每一個(gè)struts標(biāo)簽有關(guān)聯(lián)的模板,如:s:textfield -> text.ftl 和 s:password -> password.ftl等,這些模板文件來壓縮struts2-core.xy.z.jar文件。這些模板文件保持一個(gè)預(yù)先定義的HTML布局為每個(gè)標(biāo)簽。所以Struts2 框架生成最終的HTML標(biāo)記代碼使用Sturts標(biāo)簽和相關(guān)的模板。
Struts 2 tags + Associated template file = Final HTML markup code.
默認(rèn)模板已經(jīng)寫在FreeMarker和他們有擴(kuò)展名 .ftl??梢栽O(shè)計(jì)使用速度或JSP模板,并據(jù)此設(shè)置配置在使用struts.ui.templateSuffix 和 struts.ui.templateDir struts.properties。
最簡(jiǎn)單的方法來創(chuàng)建一個(gè)新的主題是復(fù)制現(xiàn)有的任何主題/模板文件,并做必要的修改。所以,讓我們開始創(chuàng)建一個(gè)文件夾 WebContent/WEB-INF/classes 名為模板和子文件夾與我們新的主題的名稱,例如WebContent/WEB-INF/classes/template/mytheme。從這里,可以從頭開始構(gòu)建模板,或者可以復(fù)制??模板從Struts2分布和根據(jù)需要進(jìn)行修改。
我們要修改現(xiàn)有的默認(rèn)模板XHTML學(xué)習(xí)目的。所以,現(xiàn)在讓,我們復(fù)制內(nèi)容從 struts2-core-x.y.z.jar/template/xhtml 到我們的主題目錄,并只修改WebContent/WEB-INF/classes/template/mytheme/control.ftl文件。當(dāng)我們打開control.ftl 它將有下面幾行:
<table class="${parameters.cssClass?default('wwFormTable')?html}"<#rt/> <#if parameters.cssStyle??> style="${parameters.cssStyle?html}"<#rt/> </#if> >
讓我們上述文件control.ftl改變有以下內(nèi)容:
<table style="border:1px solid black;">
如果檢查看 form.ftl 會(huì)發(fā)現(xiàn),control.ftl 這個(gè)文件中,form.ftl這個(gè)文件是指從XHTML主題。因此,讓我們改變?nèi)缦拢?/p>
<#include "/${parameters.templateDir}/xhtml/form-validate.ftl" /> <#include "/${parameters.templateDir}/simple/form-common.ftl" /> <#if (parameters.validate?default(false))> onreset="${parameters.onreset?default('clearErrorMessages(this); clearErrorLabels(this);')}" <#else> <#if parameters.onreset??> onreset="${parameters.onreset?html}" </#if> </#if> > <#include "/${parameters.templateDir}/mytheme/control.ftl" />
我假設(shè)不會(huì)有太多了解FreeMarker模板語言,仍然尋找FTL文件需要做什么,可以得到一個(gè)不錯(cuò)的主意。然而,讓我們除上述變動(dòng)外,并回到我們的本地化的例子,創(chuàng)建 WebContent/WEB-INF/classes/struts.properties 檔案的以下內(nèi)容:
# Customized them struts.ui.theme=mytheme # Directory where theme template resides struts.ui.templateDir=template # Sets the template type to ftl. struts.ui.templateSuffix=ftl
現(xiàn)在這種變化后,右鍵點(diǎn)擊項(xiàng)目名稱,并單擊Export > WAR File創(chuàng)建一個(gè)WAR文件。然后部署此WAR在Tomcat的webapps目錄下。最后,啟動(dòng)Tomcat服務(wù)器和嘗試訪問URL http://localhost:8080/HelloWorldStruts2。這會(huì)給出以下畫面:
XHTML主題復(fù)制后的變化,我們做了主題這是一個(gè)結(jié)果,可以看到一個(gè)表單組件周圍的邊框。 FreeMarker學(xué)習(xí),如果你努力,那么將能夠創(chuàng)建或修改主題很容易。至少現(xiàn)在,你必須有一個(gè)基本的了解Sturts2主題和模板。