Tapestry自定义组件

Tapestry最大的优势就是:可重复使用组件。

自定义组件的组成:
*.html + *.jwc + *.java
jwc文件类似于page文件的功能, java类必须继承BaseComponent接口

  [b]jwc文件[/b]
[code]
<?xml version="1.0"?>
<!DOCTYPE component-specification PUBLIC
"-//Apache Software Foundation//Tapestry Specification 4.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
<component-specification allow-body="no" allow-informal-parameters="yes" class="">
</component-specification>
[/code]
allow-body 参数含义是该组件是否有body
allow-informal-parameters 参数指定该组件是否能够使用HTML参数(这里的HTML参数是指没有该组件没有定义的普通的HTML参数)
class 参数含义是指定该自定义组件的类,也可以通过在.application配置文件中指定
*.application
[code]
<?xml version="1.0"?>
<!DOCTYPE application PUBLIC
"-//Apache Software Foundation//Tapestry Specification 4.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
<application name="Components">
<meta key="org.apache.tapestry.component-class-packages" value="..."/>
</application>
[/code]

e.g.1.
Copyright.html
[code]
<html>
<body jwcid="$content$">
<hr>
Copyright <span jwcid="year">2005</span>. Foo Inc All rights reserved.
</body>
</html>
[/code]
Copyright.jwc
[code]
<component-specification allow-body="no">
<description>It renders a copyright notice.</description>
<!-- allow-body="yes",允许嵌套,配置了allow-body="yes"以后可以不创建html模板 -->
<!-- allow-informal-parameters="yes",使用不规则参数,在引用中定义的html属性 -->
<component id="year" type="Insert">
<binding name="value" value="currentYear"/>
</component>
</component-specification>
[/code]
Copyright.java
[code]
public abstract class Copyright extends BaseComponent {

public int getCurrentYear(){
return new GregorianCalendar().get(GregorianCalendar.YEAR);
}
}
[/code]
Home.html
[code]
<span jwcid="copyright@Copyright">Copyright notic<b>Hello!</b></span>
[/code]

自定义组件的组件类也可以继承AbstractComponent接口.BaseComponent是Abstract Component的子类,AbstractComponent类中的renderComponent()方法是可以在类中自定义的, Basecomponet类中renderComponent()方法是表现该组件的模板(.html)中的内容.故继承AbstractComponent接口的自定义组件可以没有模板(.html),其内容可以在类中自定义.如:
[code]
public class Box extends AbstractComponent {

protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle){
writer.begin("table");
writer.attribute("border", 1);
renderInformalParameters(writer,cycle);
writer.begin("tr");
writer.begin("td");
renderBody(writer, cycle);
writer.end();
writer.end();
writer.end();
}
}[/code]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值