Tapestry3自定义组件

tapestry早就步入annotation时代了,公司用的还停留在3.0时代,不过3.0确实比较经典,tapestry4,5正在一步步迷失自己。
用tapestry无非就为了四个字:基于组件。
tapestry3自定义组件有两种方式,一种就是原始的print方式后台一点点画组件然后渲染到前台,还有种就是画好html模版,后台纯粹传参数来渲染组件。
先是第一种,第一种继承AbstractComponent,override方法renderComponent:

public class TestInsert extends AbstractComponent {

@Override
protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {

writer.begin("span");
writer.attribute("style", "color:red");
writer.print("红色的span标签");
writer.end("span");
this.renderInformalParameters(writer, cycle);
}

}

定义个Test.jwc

<component-specification class="aronlulu.test.TestInsert"
allow-body="yes"
allow-informal-parameters="yes">

<description>
redSpan
</description>
</component-specification>

再在.application中定义位置:
<component-type type="Test" specification-path="/Test.jwc"/>
然后页面中即可直接调用组件:
<span jwcid="test@Test"></span>
很灵活,一次麻烦,以后就处处可以重用。
第二种,基于模板的组件,第二种就是继承BaseComponent(基于AbstractComponent):

public class TestSpanByHtml extends BaseComponent{

public String getValue(){
return "红色的span标签";
}

}

同样定义个TestByHtml.jwc

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE component-specification PUBLIC
"-//Apache Software Foundation//Tapestry Specification 3.0//EN"
"http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">

<component-specification class="aronlulu.test.TestSpanByHtml"
allow-body="yes"
allow-informal-parameters="yes">


<component id="test" type="Insert">
<binding name="value">value</binding>
</component>

</component-specification>

再定义个html模板:

<html>
<body jwcid="$content$">
<hr>
<span style="color:red"><span jwcid="test"></span></span>
</body>
</html>

别忘了再定义上配置文件位置,然后页面就可以这样调用:
<span jwcid="test@TestByHtml"></span>。
以上就是两种定义方式,估计也是tapestry受欢迎的最主要原因吧,通过对页面组件化,然后用pool的概念管理组件,在较大规模系统扩展需求的时候确实会省很多的时间。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值