鍍金池/ 教程/ Java/ Struts2 主題和模板
設(shè)置復(fù)選框的默認(rèn)值
<s:if>, <s:elseIf>, <s:else> 標(biāo)簽示例
下拉框示例
Struts2 &lt;s:bean&gt;標(biāo)簽示例
Struts2 &lt;s:date&gt;標(biāo)簽示例
<s:text>標(biāo)簽示例
Struts2的ActionError&ActionMessage示例
Struts2 MySQL數(shù)據(jù)庫訪問
Struts2上傳多個(gè)文件例子
Struts2+Log4j集成
Struts 2 hello world (XML版本)
Struts2和Spring集成
Struts2 控件標(biāo)簽
Struts2 和 JSON 實(shí)例
Struts2 環(huán)境配置
Struts2 &lt;s:checkboxlist&gt;多個(gè)復(fù)選框例子
Struts2 注釋類型
在Struts2中配置靜態(tài)參數(shù)
Struts2 驗(yàn)證框架
Struts2 param 標(biāo)簽
Struts2 &lt;s:hidden&gt;隱藏值例子
Struts2下載文件實(shí)例
Struts2 &lt;sx:autocompleter&gt;自動(dòng)提示例子
Struts2 action標(biāo)簽
autocompleter自動(dòng)完成示例
Struts2 merge標(biāo)簽
Struts2 &lt;s:action&gt;標(biāo)簽示例
<s:property>標(biāo)簽示例
Struts2 &lt;s:sort&gt;標(biāo)簽示例
Struts2和Hibernate集成
自動(dòng)選擇下拉框示例
Struts2 結(jié)果和結(jié)果類型
Struts2 是什么?
如何刪除Struts2動(dòng)作的后綴擴(kuò)展名
Struts2 Hello World 實(shí)例
<s:action>標(biāo)簽示例
<s:bean>標(biāo)簽示例
Struts2 + Quartz調(diào)度集成實(shí)例
如何獲取ServletContext對(duì)象
Struts2 &lt;s:i18n&gt;標(biāo)簽示例
Struts2 append標(biāo)簽
i18n 或本地化示例
autocompleter + JSON 示例
Struts2獲取HttpServletRequest實(shí)例
updownselect 示例
Struts2 &lt;s:debug&gt;標(biāo)簽示例
Struts2重寫攔截器參數(shù)
多個(gè)Struts配置文件示例
<s:i18n>標(biāo)簽示例
Struts2中文本地化問題
文件上傳示例
Struts2 &lt;s:property&gt;標(biāo)簽實(shí)例
Struts2攔截器棧的例子
<s:merge>標(biāo)簽示例
Struts2 數(shù)據(jù)標(biāo)簽
Struts2 &lt;s:merge&gt;標(biāo)簽示例
復(fù)選框checkboxes 示例
Struts2 generator標(biāo)簽
如何配置Struts2全局資源包
key 屬性示例
Struts2 注釋
FilterDispatcher 和 StrutsPrepareAndExecuteFilter區(qū)別?
Struts 2 Hello World (注解版本)
Struts 2 + Log4j 集成實(shí)例
Struts2 值棧/OGNL
Struts2配置Action類的靜態(tài)參數(shù)
Struts2 本地化/國(guó)際化(i18n)
Chinese 本地化問題
Struts2單選預(yù)選按鈕值
Struts2 include標(biāo)簽
Struts2 + Hibernate 集成實(shí)例
配置全局資源包
Struts2 異常處理
Struts2 &lt;s:set&gt;標(biāo)簽示例
Struts2 &lt;s:doubleselect&gt;示例
Struts2 iterator標(biāo)簽
optiontransferselect 示例
Struts2映射攔截動(dòng)作
head 示例
Struts2+Hibernate使用Full Hibernate Plugin集成
Struts2命名空間配置和解釋
<s:param>標(biāo)簽示例
Struts2+Hibernate集成實(shí)例
Struts2 &lt;s:head&gt;示例
Struts2 &lt;s:push&gt;標(biāo)簽示例
Struts2文件上傳例子
Struts2 &lt;s:param&gt;標(biāo)簽示例
@ResultPath 注釋示例
Struts2 體系結(jié)構(gòu)
Struts2 execAndWait攔截器例子
Struts2 攔截器
Textarea - 文本域
Struts2教程
<s:push>標(biāo)簽示例
Struts2 文件上傳
Struts2 date 標(biāo)簽
Struts2 &lt;s:texttag&gt;標(biāo)簽示例
Struts2+Quartz2調(diào)度集成實(shí)例
Struts2開發(fā)模式
Struts2 autocompleter+JSON例子
Struts2 類型轉(zhuǎn)換
<s:url>標(biāo)簽示例
Struts2 Ajax 標(biāo)簽
Struts2 bean 標(biāo)簽
<s:iterator>標(biāo)簽迭代示例
Hidden隱藏值示例
在Struts2獲取HttpServletResponse對(duì)象
Struts2 主題和模板
Struts2自動(dòng)選擇下拉框的值
Struts2下載文件實(shí)例
在Struts2中獲取 HttpServletRequest
級(jí)聯(lián)選擇示例
Struts2 &lt;sx:datetimepicker&gt;示例
Struts2 include(包含)多個(gè)配置文件
Struts2 &lt;s:select&gt;下拉框例子
Struts2 Action/動(dòng)作
Struts2 &lt;s:textarea&gt;文本域?qū)嵗?/span>
Struts2+Spring集成實(shí)例
Struts2 &lt;s:updownselect&gt;示例
Struts2 property 標(biāo)簽
Struts2 &lt;s:url&gt;標(biāo)簽示例
Struts2 表單標(biāo)簽
Struts2資源包使用示例
subset tag example
Struts 2 ActionError &amp; ActionMessage Example
Struts2 MVC架構(gòu)
Struts2 if/else標(biāo)簽
Struts2 &lt;s:generator&gt;標(biāo)簽示例
<s:include>標(biāo)簽示例
Struts2開發(fā)者模式
<s:generator> 標(biāo)簽示例
復(fù)選框heckbox 示例
Struts2 text 標(biāo)簽
Radio 單選按鍵示例
Struts2 &lt;s:a&gt;標(biāo)簽示例
Struts2 key鍵屬性示例
Struts2設(shè)置多個(gè)復(fù)選框缺省值
Struts2 基于XML校驗(yàn)
Struts2 配置文件
Struts2 &lt;s:radio&gt;單選按鈕例子
Struts2 發(fā)送郵件Email
Struts2獲取HttpServletResponse實(shí)例
<s:sort>標(biāo)簽示例
Struts2 &lt;s:optiontransferselect&gt;示例
Struts2和Tiles集成
Struts2模型驅(qū)動(dòng)實(shí)例
Struts2的FilterDispatcher和StrutsPrepareAndExecuteFilter有什么不同?
<s:append>標(biāo)簽示例
組合框示例
Struts2 &lt;s:password&gt;密碼示例
datetimepicker 日期選擇
Struts2 &lt;s:iterator&gt;迭代器標(biāo)簽示例
TextBox文檔示例
Struts2 url標(biāo)簽
資源包使用示例
Struts2本地化和國(guó)際化
Struts2 &lt;s:subset&gt;標(biāo)簽示例
Struts2 + Hibernate使用“Full Hibernate Plugin"插件集成
Struts2和JSON實(shí)例
Struts2注解示例
Struts2 &lt;s:checkbox&gt;復(fù)選框例子
Struts2快速入門
<s:date>標(biāo)簽示例
Struts2 set 標(biāo)簽
<s:set>標(biāo)簽示例
Struts2 命名空間配置和解釋
Struts 2 hello world (XML版本)
&nbsp;預(yù)選單選按鈕
<s:a>標(biāo)簽示例
Struts2+Spring+Hibernate集成實(shí)例
Struts2 &lt;s:combobox&gt;組合框的例子
Struts2 if,elseif,else標(biāo)簽示例
Struts2 @ResultPath注釋示例
struts2 &lt;s:append&gt;標(biāo)簽示例
Struts2 &lt;s:textfield&gt;文本示例
多文件上傳示例
使用Struts2動(dòng)作
<s:debug>標(biāo)簽示例
在Struts2中如何獲取ServletContext對(duì)象
Struts2 &lt;s:include&gt;標(biāo)簽示例
Password 示例

Struts2 主題和模板

實(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文件,這是由主題。

English Output

主題如何工作的?

對(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。

創(chuàng)建新的主題:

最簡(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ì)給出以下畫面: 

Theme and Template

XHTML主題復(fù)制后的變化,我們做了主題這是一個(gè)結(jié)果,可以看到一個(gè)表單組件周圍的邊框。 FreeMarker學(xué)習(xí),如果你努力,那么將能夠創(chuàng)建或修改主題很容易。至少現(xiàn)在,你必須有一個(gè)基本的了解Sturts2主題和模板。