云自无心水自闲

天平山上白云泉,云自无心水自闲。何必奔冲山下去,更添波浪向人间!
posts - 288, comments - 524, trackbacks - 0, articles - 6
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

Tapestry最新版5.1.0.5教程(四)

Posted on 2009-06-20 19:57 云自无心水自闲 阅读(2511) 评论(3)  编辑  收藏 所属分类: Java心得体会Tapestry

1、Tapestry组件的写法
a、<t:textfield t:id="userName" t:value="jack"/>,这样的写法的优点是,看上去比较直观,与Struts等Web框架的一致。但是缺点就是,使用浏览器(或者美工)直接看页面的时候,浏览器无法正确显示这个组件。
b、<input type="text" t:type="textfield" t:id="userName" t:value="jack"/>这样写的话,浏览器就能正常显示一个文本输入框了。这也是Tapestry一直鼓吹的一大优点之一。
c、在Java类中使用注解来声明组件。这种方法我个人并不推荐,只是一种选择,并不直观也不方便。
d、Tapestry组件标签是不区分大小写的。
2、组件参数的前缀。从官方文档来看,参数的前缀还挺多的:
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这个参数的缺省前缀是prop,所以如果直接写model="red"的话,Tapestry会认为red是一个变量名,会调用页面对应类的getRed()方法来获得一个列表。所以,此处如果想使用一个常量的话,需要显示指明literal前缀。而label的缺省前缀是literal,所以直接这样写就没有问题。
3、Label,显示一个标签,一般在需要多语言的环境下或者与textField配合使用。
<t:label t:for="userName">Label for the user name</t:label> Lable组件有一个t:for的属性,这个属性的值一般是一个textField的id。我们可以注意到Lable标签里写一段文字,这一段文字在页面部署运行后是不会显示的,写在这里,纯粹是给美工这样的静态页面浏览者看的。在实际运行中这一段文字会被替代。替代的内容就是t:for属性对应的TextField组件的t:id的值,比如:userName。当然Tapestry会自动把userName分成两个单词,而且把user的首字母大写。
如果根据t:id生成的lable不符合要求,可以有另一种选择:直接在textField中指定label:
<input type="text" t:type="textField" t:id="userName" t:label="Input User Name" t:value="jack"/>
4、PageLink,用于显示一个链接,点击后,跳转到指定页面
<a href="#" t:type="pageLink" t:page="register">Register</a>
5、ActionLink,用于显示一个链接,点击后,运行一个可以带有参数的指令。
<a href="#" t:type="actionLink" t:context="id" t:id="clickLink">
        ${user.id}
    </a>
  如果一个参数不够,比如,只用一个id不能唯一确定一个用户,必须结合部门id才能唯一确定用户的话,那么可以这样做:
  <a href="#" t:type="actionLink" t:context="userContext" t:id="clickLink">delete</a>
  相应的在java class中需要添加一个方法:getUserContext()
  public Object[] getUserContext() {
     return new Object[] {user.id, department.id};
  }
6、Loop
Loop并不是一个真正的Form控件,严格说起来是一种逻辑控制语句。语法如下:
<t:loop source="userList" value="user">
    <td>${user.name}</td>
</t:loop>
7、Radio & Radio
            <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"/>




评论

# re: Tapestry最新版5.1.0.5教程(四)  回复  更多评论   

2009-07-06 10:46 by skybirds
麻烦tapestry5.1的教程能出快点不?

# re: Tapestry最新版5.1.0.5教程(四)  回复  更多评论   

2010-01-10 13:48 by 贾俊超
很好的内容,太少了,有很有的课件或者视频的就好了!

# re: Tapestry最新版5.1.0.5教程(四)  回复  更多评论   

2010-03-03 00:38 by idreamer
非常感谢。我做毕业设计,web层用的就是tapestry。不喜欢struts2,感觉很乱,我和作者的想法不一样。本来打算自己做个mvc的,叫beanstruts,最后发现好多东西需要做,缓存,注入(事件注入已经实现),验证。。。最后发现了tapestry感觉思想上很容易接受,不过用起来真不容易。

只有注册用户登录后才能发表评论。


网站导航: