Tapstry3的追忆------学习笔记(二)

接上例: http://blog.csdn.net/kunshan_shenbin/archive/2008/11/14/3299672.aspx

这里我们演示一下使用T3自定义控件的做法:

如下图所示建立工程

jar包与上例一致。

代码如下:

Box.java

  1. package mo.org.cpttm.components;
  2. import org.apache.tapestry.*;
  3. public class Box extends AbstractComponent {
  4.     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {
  5.         writer.begin("table");
  6.         writer.attribute("border"1);
  7.         renderInformalParameters(writer, cycle);
  8.         writer.begin("tr");
  9.         writer.begin("td");
  10.         renderBody(writer, cycle);
  11.         writer.end();
  12.         writer.end();
  13.         writer.end();
  14.     }
  15. }

Copyright.java

  1. package mo.org.cpttm.components;
  2. import java.util.*;
  3. import org.apache.tapestry.*;
  4. public class Copyright extends BaseComponent {
  5.     public int getCurrentYear() {
  6.         return new GregorianCalendar().get(GregorianCalendar.YEAR);
  7.     }
  8. }

Home.java

  1. package mo.org.cpttm.components;
  2. import org.apache.tapestry.*;
  3. import org.apache.tapestry.html.*;
  4. public abstract class Home extends BasePage {
  5.     public abstract int getColor();
  6.     public void onOk(IRequestCycle cycle) {
  7.         System.out.println(Integer.toHexString(getColor()));
  8.     }
  9. }

RGB.java

  1. package mo.org.cpttm.components;
  2. import org.apache.tapestry.*;
  3. import org.apache.tapestry.form.*;
  4. public abstract class RGB extends BaseComponent implements IFormComponent {
  5.     abstract public String getBlue();
  6.     abstract public void setBlue(String blue);
  7.     abstract public String getGreen();
  8.     abstract public void setGreen(String green);
  9.     abstract public String getRed();
  10.     abstract public void setRed(String red);
  11.     abstract public void setForm(IForm form);
  12.     public abstract int getColor();
  13.     public abstract void setColor(int color);
  14.     public String getDisplayName() {
  15.         return null;
  16.     }
  17.     public boolean isDisabled() {
  18.         return false;
  19.     }
  20.     public String getBackgroundColorAssignment() {
  21.         return "background-color: rgb(" + getRed() + "," + getGreen() + ","
  22.                 + getBlue() + ")";
  23.     }
  24.     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {
  25.         setForm(Form.get(cycle));
  26.         if (!cycle.isRewinding()) {
  27.             setRed(Integer.toString(getColor() >> 16));
  28.             setGreen(Integer.toString((getColor() >> 8) & 0xff));
  29.             setBlue(Integer.toString(getColor() & 0xff));
  30.         }
  31.         super.renderComponent(writer, cycle);
  32.         if (cycle.isRewinding()) {
  33.             setColor((Integer.parseInt(getRed()) << 16)
  34.                     | (Integer.parseInt(getGreen()) << 8)
  35.                     | Integer.parseInt(getBlue()));
  36.         }
  37.     }
  38. }

Box.jwc

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE component-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
  5. <!-- generated by Spindle, http://spindle.sourceforge.net -->
  6. <component-specification class="mo.org.cpttm.components.Box"
  7.     allow-body="yes"
  8.     allow-informal-parameters="yes">
  9.     <description>add a description</description>
  10. </component-specification>

Components.library

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE library-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
  5. <library-specification>
  6.     <component-type type="Copyright" specification-path="Copyright.jwc"/>
  7.     <component-type type="Box" specification-path="Box.jwc"/>
  8.     <component-type type="RGB" specification-path="RGB.jwc"/>
  9. </library-specification>

Copyright.html

  1. <html>
  2. <body jwcid="$content$">
  3. <hr>
  4. Copyright <span jwcid="year">2005</span><span jwcid="holder">Foo Inc.</span> All rights reserved.
  5. </body>
  6. </html>

Copyright.jwc

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE component-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
  5. <!-- generated by Spindle, http://spindle.sourceforge.net -->
  6. <component-specification class="mo.org.cpttm.components.Copyright"
  7.     allow-body="no"
  8.     allow-informal-parameters="yes">
  9.     <description>It renders a copyright notice.</description>
  10.     <parameter name="holder"
  11.         required="no" 
  12.         default-value="'Foo Inc.'" 
  13.         type="java.lang.String"
  14.         direction="in">
  15.         <description>The copyright holder.</description>
  16.     </parameter>
  17.     <component id="year" type="Insert">
  18.         <binding name="value" expression="currentYear"/>
  19.     </component>
  20.     <component id="holder" type="Insert">
  21.         <binding name="value" expression="holder"/>
  22.     </component>
  23. </component-specification>

RGB.html

  1. <html>
  2. <body jwcid="$content$">
  3. R: <input type="text" size="3" maxlength="3" jwcid="red"/>
  4. G: <input type="text" size="3" maxlength="3" jwcid="green"/>
  5. B: <input type="text" size="3" maxlength="3" jwcid="blue"/>
  6. Sample: <span jwcid="sample" style="background-color: rgb(255, 0, 0)">
  7.     </span>
  8. </body>
  9. </html>

RGB.jwc

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE component-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
  5. <!-- generated by Spindle, http://spindle.sourceforge.net -->
  6. <component-specification class="mo.org.cpttm.components.RGB"
  7.     allow-body="no"
  8.     allow-informal-parameters="no">
  9.     <parameter name="color" required="yes" type="int" direction="form"/>
  10.     <property-specification name="red" type="java.lang.String"/>
  11.     <property-specification name="green" type="java.lang.String"/>
  12.     <property-specification name="blue" type="java.lang.String"/>
  13.     <property-specification name="form" type="org.apache.tapestry.IForm"/>
  14.     <property-specification name="name" type="java.lang.String"/>
  15.     <component id="red" type="TextField">
  16.         <binding name="value" expression="red"/>
  17.     </component>
  18.     <component id="green" type="TextField">
  19.         <binding name="value" expression="green"/>
  20.     </component>
  21.     <component id="blue" type="TextField">
  22.         <binding name="value" expression="blue"/>
  23.     </component>
  24.     <component id="sample" type="Any">
  25.         <static-binding name="element" value="span"/>
  26.         <binding name="style" expression="backgroundColorAssignment"/>
  27.     </component>
  28. </component-specification>

Components.application

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE application PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
  5. <!-- generated by Spindle, http://spindle.sourceforge.net -->
  6. <application name="Components" engine-class="org.apache.tapestry.engine.BaseEngine">
  7.     <description>add a description</description>
  8.     <page name="Home" specification-path="Home.page"/>
  9.     <component-type
  10.         type="Copyright"
  11.         specification-path="classes/mo/org/cpttm/components/Copyright.jwc"/>
  12.     <component-type
  13.         type="Box"
  14.         specification-path="classes/mo/org/cpttm/components/Box.jwc"/>
  15.     <component-type
  16.         type="RGB"
  17.         specification-path="classes/mo/org/cpttm/components/RGB.jwc"/>
  18. </application>

Home.html

  1. <html>
  2. <head></head>
  3. <body>
  4. <h1>Page 1</h1>
  5. This is page 1.
  6. <form jwcid="form">
  7. <span jwcid="color">edit color here</span>
  8. <input type="submit" value="OK"/>
  9. </form>
  10. <span jwcid="box" width="50%">
  11. <span jwcid="copyright">Copyright notice</span>
  12. </span>
  13. </body>
  14. </html>

Home.page

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE page-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 3.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_3_0.dtd">
  5. <!-- generated by Spindle, http://spindle.sourceforge.net -->
  6. <page-specification class="mo.org.cpttm.components.Home">
  7.     <property-specification name="color" type="int"/>
  8.     <component id="copyright" type="Copyright">
  9.         <static-binding name="value" value="Foo Inc."/>
  10.     </component>
  11.     <component id="box" type="Box"/>
  12.     <component id="form" type="Form">
  13.         <binding name="listener" expression="listeners.onOk"/>
  14.     </component>
  15.     <component id="color" type="RGB">
  16.         <binding name="color" expression="color"/>
  17.     </component>
  18. </page-specification>

web.xml

  1. <?xml version="1.0"?>
  2. <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
  3.     xmlns:xsi="http://www.w3.org/TR/xmlschema-1/"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd" version="2.4">
  5.     <display-name>Components</display-name>
  6.     <servlet>
  7.         <servlet-name>Components</servlet-name>
  8.         <servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
  9.         <load-on-startup>1</load-on-startup>
  10.     </servlet>
  11.     <servlet-mapping>
  12.         <servlet-name>Components</servlet-name>
  13.         <url-pattern>/app</url-pattern>
  14.     </servlet-mapping>
  15. </web-app>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值