两种方法创建自己定义标签:
一.通过AbstractComponent父类渲染,此种方法直接在java类中编写页面脚本。然后输出。
1.编写java类com/ailk/ech/ecop/view/test/jwc/LiuzfTest2.java
package com.ailk.ech.ecop.view.test.jwc; import org.apache.tapestry.AbstractComponent; import org.apache.tapestry.BaseComponent; import org.apache.tapestry.IMarkupWriter; import org.apache.tapestry.IRequestCycle; public abstract class LiuzfTest2 extends AbstractComponent{ public abstract String getLink(); public abstract String getValue(); @Override protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) { // TODO Auto-generated method stub writer.begin("a"); writer.attribute("href", getLink()); writer.print(getValue()); renderInformalParameters(writer, cycle); } }
2.编写jwc配置文件webecop estjwcLiuzfTest2.jwc。定义组件參数和映射的java类
<?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 allow-body="yes" allow-informal-parameters="yes" class="com.ailk.ech.ecop.view.test.jwc.LiuzfTest2"> <parameter name="value" direction="in" type="java.lang.String"/> <parameter name="link" direction="in" type="java.lang.String"/> </component-specification>
3.在application中引入组件
<component-type type="LiuzfTest2" specification-path="/ecop/test/jwc/LiuzfTest2.jwc"/>
4.然后就能够使用了,使用效果。输出一个<a href="http://www.baidu.com">test2</a>
<span jwcid="@LiuzfTest2" link="http://www.baidu.com" value="test2" />
二.通过BaseComponent父类渲染,此种方法须要定义html规范,通过html规范输出html代码,无需在java类中输出html
1.编写java类LiuzfTest3.java
package com.ailk.ech.ecop.view.test.jwc; import org.apache.tapestry.AbstractComponent; import org.apache.tapestry.BaseComponent; import org.apache.tapestry.IMarkupWriter; import org.apache.tapestry.IRequestCycle; public abstract class LiuzfTest3 extends BaseComponent{ public abstract String getLink(); public abstract String getValue(); }2.编写jwc配置文件,定义组件的參数。映射类
<?3.编写html规范,html文件与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 allow-body="yes" allow-informal-parameters="yes" class="com.ailk.ech.ecop.view.test.jwc.LiuzfTest3"> <parameter name="value" direction="in" type="java.lang.String"/> <parameter name="link" direction="in" type="java.lang.String"/> </component-specification>
<html> <body jwcid="$content$"> <hr> <a jwcid="@Any" href="ognl:link"><span jwcid="@Insert" value="ognl:value"></span></a> </body> </html>4.在application中引入这个组件
<component-type type="LiuzfTest3" specification-path="/ecop/test/jwc/LiuzfTest3.jwc"/>5然后就能够使用了,效果也是输出一个链接
<td><span jwcid="@LiuzfTest3" link="http://www.baidu.com" value="test3" /></td>