Tapestry最新版5.1.0.5教程(四)
Posted on 2009-06-20 19:57 云自無心水自閑 閱讀(2534) 評論(3) 編輯 收藏 所屬分類: Java 、心得體會 、Tapestry1、Tapestry組件的寫法
a、<t:textfield t:id="userName" t:value="jack"/>,這樣的寫法的優(yōu)點是,看上去比較直觀,與Struts等Web框架的一致。但是缺點就是,使用瀏覽器(或者美工)直接看頁面的時候,瀏覽器無法正確顯示這個組件。
b、<input type="text" t:type="textfield" t:id="userName" t:value="jack"/>這樣寫的話,瀏覽器就能正常顯示一個文本輸入框了。這也是Tapestry一直鼓吹的一大優(yōu)點之一。
c、在Java類中使用注解來聲明組件。這種方法我個人并不推薦,只是一種選擇,并不直觀也不方便。
d、Tapestry組件標(biāo)簽是不區(qū)分大小寫的。
2、組件參數(shù)的前綴。從官方文檔來看,參數(shù)的前綴還挺多的:
Prefix Description
asset The relative path to an asset file (which must exist).
block The id of a block within the template.
component The id of another component within the same template.
context Context asset: path from context root.
literal A literal string.
nullfieldstrategy Used to locate a pre-defined NullFieldStrategy
message Retrieves a value from the component's message catalog.
prop A property expression to read or update.
translate The name of a configured translator.
validate A validator specification used to create some number of field validators.
var Allows a render variable of the component to be read or updated.
但最最常用的只有2個:prop和literal
簡單的說:prop表示這是一個變量名,literal表明這是一個常量
比如說select這個組件,它的基本寫法是: <t:select t:id="color" model="literal:Red,Green,Blue" label="Colour:"/>
查看select的參考文檔,model這個參數(shù)的缺省前綴是prop,所以如果直接寫model="red"的話,Tapestry會認(rèn)為red是一個變量名,會調(diào)用頁面對應(yīng)類的getRed()方法來獲得一個列表。所以,此處如果想使用一個常量的話,需要顯示指明literal前綴。而label的缺省前綴是literal,所以直接這樣寫就沒有問題。
3、Label,顯示一個標(biāo)簽,一般在需要多語言的環(huán)境下或者與textField配合使用。
<t:label t:for="userName">Label for the user name</t:label> Lable組件有一個t:for的屬性,這個屬性的值一般是一個textField的id。我們可以注意到Lable標(biāo)簽里寫一段文字,這一段文字在頁面部署運行后是不會顯示的,寫在這里,純粹是給美工這樣的靜態(tài)頁面瀏覽者看的。在實際運行中這一段文字會被替代。替代的內(nèi)容就是t:for屬性對應(yīng)的TextField組件的t:id的值,比如:userName。當(dāng)然Tapestry會自動把userName分成兩個單詞,而且把user的首字母大寫。
如果根據(jù)t:id生成的lable不符合要求,可以有另一種選擇:直接在textField中指定label:
<input type="text" t:type="textField" t:id="userName" t:label="Input User Name" t:value="jack"/>
4、PageLink,用于顯示一個鏈接,點擊后,跳轉(zhuǎn)到指定頁面
<a href="#" t:type="pageLink" t:page="register">Register</a>
5、ActionLink,用于顯示一個鏈接,點擊后,運行一個可以帶有參數(shù)的指令。
<a href="#" t:type="actionLink" t:context="id" t:id="clickLink">
${user.id}
</a>
如果一個參數(shù)不夠,比如,只用一個id不能唯一確定一個用戶,必須結(jié)合部門id才能唯一確定用戶的話,那么可以這樣做:
<a href="#" t:type="actionLink" t:context="userContext" t:id="clickLink">delete</a>
相應(yīng)的在java class中需要添加一個方法:getUserContext()
public Object[] getUserContext() {
return new Object[] {user.id, department.id};
}
6、Loop
Loop并不是一個真正的Form控件,嚴(yán)格說起來是一種邏輯控制語句。語法如下:
<t:loop source="userList" value="user">
<td>${user.name}</td>
</t:loop>
7、Radio?。Αadio
<t:radiogroup t:id="type">
<t:radio t:id="masterCard"/>
<t:label for="masterCard"/>
<t:radio t:id="visa"/>
<t:label for="visa"/>
<t:radio t:id="amex"/>
<t:label for="amex"/>
<t:radio t:id="dinersClub"/>
<t:label for="dinersClub"/>
<t:radio t:id="discover"/>
<t:label for="discover"/>
</t:radiogroup>
8、CheckBox
<t:checkbox t:id="showall" onclick="this.form.submit();"/> <t:label for="showall"/>
9、Select
<t:select t:id="color" model="literal:Red,Green,Blue"/>