2. component標簽 component標簽用于使用自己的自定義組件,這是一個非常靈活的用法,如果經常需要使用某個效果片段,就可以考慮將這個效果片段定義成一個自定義組件,然后在頁面中使用component標簽來使用該自定義組件。因為使用自定義組件還是基于主題、模板管理的,因此在使用component標簽時,常常需要指定如下三個屬性:? theme:自定義組件所使用的主題,如果不指定該屬性,默認使用xhtml主題。? templateDir:指定自定義組件的主題目錄,如果不指定,默認使用系統的主題目錄,即template目錄。? template:指定自定義組件所使用的模板。除此之外,還可以在cmponent標簽內使用param子標簽,子標簽表示向該標簽模板中傳入額外的參數。如果希望在模板中取得該參數,總是采用如下形式:$parameters.paramname,或者$parameters['paramname']。提示:自定義的模板文件可以采用FreeMarker、jsp和Velocity三種技術來書寫。看下面的JSP頁面,該頁面多次使用了<s:component .../>標簽來使用自定義組件,下面是該頁面使用<s:component .../>標簽的代碼片段:
使用自定義主題,自定義主題目錄<br>
從Web應用根路徑下加載模板,使用ftl模板。
<s:component
theme="customTheme"
templateDir="customTemplateDir"
template="ftlCustomTemplate">
<s:paramname="list"value="{'SAjax寶典'}"/>
</s:component>
<hr/>
使用自定義主題,自定義主題目錄<br>
從Web應用根路徑下加載模板,使用JSP模板。
<s:component
theme="customTheme"
templateDir="customTemplateDir"
template="jspCustomTemplate.jsp">
<s:paramname="list"value="{'Spring2.0寶典','輕量級J2EE企業應用實戰','基于J2EE的Ajax寶典'}"/>
</s:component>
<hr/>
使用默認主題(xhtml),默認主題目錄(template)<br>
從Web應用中加載模板,使用JSP模板。
<s:componenttemplate="mytemplate.jsp">
<s:paramname="list"value="{'Spring2.0寶典','輕量級J2EE企業應用實戰','基于J2EE的Ajax寶典'}"/>
</s:component>
<hr/>
使用自定義主題,自定義主題目錄<br>
從/WEB-INF/classes路徑下加載模板,使用ftl模板。
<s:component
theme="myTheme"
templateDir="myTemplateDir"
template="myAnotherTemplate">
<s:paramname="list"value="{'Spring2.0寶典','輕量級J2EE企業應用實戰','基于J2EE的Ajax寶典'}"/>
</s:component>
在上面頁面中使用了2個FreeMarker模板,這兩個FreeMarker模板都使用了FreeMarker標簽。除此之外,本頁面中還使用了兩個JSP模板,這兩個JSP頁面只是加載的位置不同,兩個模板的代碼是相同的,下面是JSP模板的代碼:
<!--設置頁面編碼-->
<%@pagecontentType="text/html;charset=GBK"language="java"%>
<%@taglibprefix="s"uri="/struts-tags"%>
<divstyle="">
<b>JSP自定義模板<br>
請選擇您喜歡的圖書<br></b>
<!--使用select標簽迭代輸出list參數所穿過來的集合-->
<s:selectlist="parameters.list"/>
</div>
新聞熱點
疑難解答