用freemarker做模板創(chuàng)建一個struts2自定義標簽
Posted on 2008-04-11 16:09 云自無心水自閑 閱讀(7558) 評論(4) 編輯 收藏 所屬分類: Java 、心得體會 、Struts2首先,在web項目的頁面根目錄下建立目錄template
然后創(chuàng)建目錄simple和xhtml,以上的目錄名是struts2缺省使用的,不同的主題使用相應的目錄。然后再創(chuàng)建一個components目錄,在這個目錄下,創(chuàng)建一個property.ftl。 最后的目錄結構如下:
template/simple/components/property.ftl
template/xhtml/components/property.ftl
然后在property.ftl中可以使用FreeMarker來定義新的模板(FreeMarker的具體語法可以查看FreeMarker的官方網(wǎng)站,相當?shù)脑敿氁锥?:
<#include "/${parameters.templateDir}/${parameters.theme}/controlheader.ftl" />
<@s.if test="${parameters.value} == null || ${parameters.value} == '' "> </@s.if>
<@s.else><@s.property value="${parameters.value}" /></@s.else>
<#include "/${parameters.templateDir}/xhtml/controlfooter.ftl" />
以上是一個我自定義的模板,檢測結果是否為空字符串,如果是空的話,就輸出一個 這樣在輸出結果時表格的邊框線就是完整的了。
定義好之后,在jsp頁面中就可以這樣使用了:
<s:component template="/components/property.ftl" theme="simple">
<s:param name="value" value="%{'bookName'}"/>
</s:component>
第一行中的目錄名從自components開始,struts2會自動在template目錄下去尋找,如是主題是simple, 就在simple目錄下找。
另外,param的語法要注意一下,%{}里面需要加一對引號