接上例:
http://blog.csdn.net/kunshan_shenbin/archive/2008/11/14/3299672.aspx
这里我们演示一下使用T3自定义控件的做法:
如下图所示建立工程
jar包与上例一致。
代码如下:
Box.java
- package mo.org.cpttm.components;
- import org.apache.tapestry.*;
- 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();
- }
- }
Copyright.java
- package mo.org.cpttm.components;
- import java.util.*;
- import org.apache.tapestry.*;
- public class Copyright extends BaseComponent {
- public int getCurrentYear() {
- return new GregorianCalendar().get(GregorianCalendar.YEAR);
- }
- }
Home.java
- package mo.org.cpttm.components;
- import org.apache.tapestry.*;
- import org.apache.tapestry.html.*;
- public abstract class Home extends BasePage {
- public abstract int getColor();
- public void onOk(IRequestCycle cycle) {
- System.out.println(Integer.toHexString(getColor()));
- }
- }
RGB.java
- package mo.org.cpttm.components;
- import org.apache.tapestry.*;
- import org.apache.tapestry.form.*;
- public abstract class RGB extends BaseComponent implements IFormComponent {
- abstract public String getBlue();
- abstract public void setBlue(String blue);
- abstract public String getGreen();
- abstract public void setGreen(String green);
- abstract public String getRed();
- abstract public void setRed(String red);
- abstract public void setForm(IForm form);
- public abstract int getColor();
- public abstract void setColor(int color);
- public String getDisplayName() {
- return null;
- }
- public boolean isDisabled() {
- return false;
- }
- public String getBackgroundColorAssignment() {
- return "background-color: rgb(" + getRed() + "," + getGreen() + ","
- + getBlue() + ")";
- }
- protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {
- setForm(Form.get(cycle));
- if (!cycle.isRewinding()) {
- setRed(Integer.toString(getColor() >> 16));
- setGreen(Integer.toString((getColor() >> 8) & 0xff));
- setBlue(Integer.toString(getColor() & 0xff));
- }
- super.renderComponent(writer, cycle);
- if (cycle.isRewinding()) {
- setColor((Integer.parseInt(getRed()) << 16)
- | (Integer.parseInt(getGreen()) << 8)
- | Integer.parseInt(getBlue()));
- }
- }
- }
Box.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">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <component-specification class="mo.org.cpttm.components.Box"
- allow-body="yes"
- allow-informal-parameters="yes">
- <description>add a description</description>
- </component-specification>
Components.library
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE library-specification PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
- <library-specification>
- <component-type type="Copyright" specification-path="Copyright.jwc"/>
- <component-type type="Box" specification-path="Box.jwc"/>
- <component-type type="RGB" specification-path="RGB.jwc"/>
- </library-specification>
Copyright.html
- <html>
- <body jwcid="$content$">
- <hr>
- Copyright <span jwcid="year">2005</span>. <span jwcid="holder">Foo Inc.</span> All rights reserved.
- </body>
- </html>
Copyright.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">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <component-specification class="mo.org.cpttm.components.Copyright"
- allow-body="no"
- allow-informal-parameters="yes">
- <description>It renders a copyright notice.</description>
- <parameter name="holder"
- required="no"
- default-value="'Foo Inc.'"
- type="java.lang.String"
- direction="in">
- <description>The copyright holder.</description>
- </parameter>
- <component id="year" type="Insert">
- <binding name="value" expression="currentYear"/>
- </component>
- <component id="holder" type="Insert">
- <binding name="value" expression="holder"/>
- </component>
- </component-specification>
RGB.html
- <html>
- <body jwcid="$content$">
- R: <input type="text" size="3" maxlength="3" jwcid="red"/>
- G: <input type="text" size="3" maxlength="3" jwcid="green"/>
- B: <input type="text" size="3" maxlength="3" jwcid="blue"/>
- Sample: <span jwcid="sample" style="background-color: rgb(255, 0, 0)">
- </span>
- </body>
- </html>
RGB.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">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <component-specification class="mo.org.cpttm.components.RGB"
- allow-body="no"
- allow-informal-parameters="no">
- <parameter name="color" required="yes" type="int" direction="form"/>
- <property-specification name="red" type="java.lang.String"/>
- <property-specification name="green" type="java.lang.String"/>
- <property-specification name="blue" type="java.lang.String"/>
- <property-specification name="form" type="org.apache.tapestry.IForm"/>
- <property-specification name="name" type="java.lang.String"/>
- <component id="red" type="TextField">
- <binding name="value" expression="red"/>
- </component>
- <component id="green" type="TextField">
- <binding name="value" expression="green"/>
- </component>
- <component id="blue" type="TextField">
- <binding name="value" expression="blue"/>
- </component>
- <component id="sample" type="Any">
- <static-binding name="element" value="span"/>
- <binding name="style" expression="backgroundColorAssignment"/>
- </component>
- </component-specification>
Components.application
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE application PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <application name="Components" engine-class="org.apache.tapestry.engine.BaseEngine">
- <description>add a description</description>
- <page name="Home" specification-path="Home.page"/>
- <component-type
- type="Copyright"
- specification-path="classes/mo/org/cpttm/components/Copyright.jwc"/>
- <component-type
- type="Box"
- specification-path="classes/mo/org/cpttm/components/Box.jwc"/>
- <component-type
- type="RGB"
- specification-path="classes/mo/org/cpttm/components/RGB.jwc"/>
- </application>
Home.html
- <html>
- <head></head>
- <body>
- <h1>Page 1</h1>
- This is page 1.
- <form jwcid="form">
- <span jwcid="color">edit color here</span>
- <input type="submit" value="OK"/>
- </form>
- <span jwcid="box" width="50%">
- <span jwcid="copyright">Copyright notice</span>
- </span>
- </body>
- </html>
Home.page
- <?xml version="1.0" encoding="UTF-8"?>
- <!DOCTYPE page-specification PUBLIC
- "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
- "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
- <!-- generated by Spindle, http://spindle.sourceforge.net -->
- <page-specification class="mo.org.cpttm.components.Home">
- <property-specification name="color" type="int"/>
- <component id="copyright" type="Copyright">
- <static-binding name="value" value="Foo Inc."/>
- </component>
- <component id="box" type="Box"/>
- <component id="form" type="Form">
- <binding name="listener" expression="listeners.onOk"/>
- </component>
- <component id="color" type="RGB">
- <binding name="color" expression="color"/>
- </component>
- </page-specification>
web.xml
- <?xml version="1.0"?>
- <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
- xmlns:xsi="http://www.w3.org/TR/xmlschema-1/"
- xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
- <display-name>Components</display-name>
- <servlet>
- <servlet-name>Components</servlet-name>
- <servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Components</servlet-name>
- <url-pattern>/app</url-pattern>
- </servlet-mapping>
- </web-app>