Tapestry自定义组件

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

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

   jwc文件
<?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>

allow-body 参数含义是该组件是否有body
allow-informal-parameters 参数指定该组件是否能够使用HTML参数(这里的HTML参数是指没有该组件没有定义的普通的HTML参数)
class 参数含义是指定该自定义组件的类,也可以通过在.application配置文件中指定
*.application
<?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>


e.g.1.
Copyright.html
<html>
<body jwcid="$content$">
<hr>
Copyright <span jwcid="year">2005</span>. Foo Inc All rights  reserved.
</body>
</html>

Copyright.jwc
<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>

Copyright.java
public abstract class Copyright extends BaseComponent {
	
	public int getCurrentYear(){
		return new GregorianCalendar().get(GregorianCalendar.YEAR);
	}
}

Home.html
<span jwcid="copyright@Copyright">Copyright notic<b>Hello!</b></span>


自定义组件的组件类也可以继承AbstractComponent接口.BaseComponent是Abstract Component的子类,AbstractComponent类中的renderComponent()方法是可以在类中自定义的, Basecomponet类中renderComponent()方法是表现该组件的模板(.html)中的内容.故继承AbstractComponent接口的自定义组件可以没有模板(.html),其内容可以在类中自定义.如:
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();
	}
}

你可能感兴趣的:(apache,html,xml,tapestry)