追忆Tapstry4.0------学习笔记(二)

接上例:http://blog.csdn.net/kunshan_shenbin/archive/2008/11/18/3324472.aspx

这里介绍T4版本如何自定义控件。

T3参考实现:http://blog.csdn.net/kunshan_shenbin/archive/2008/11/14/3299950.aspx

如下图所示建立工程:

所需jar包同上例一致。

代码如下:

Box.java

  1. package com.ttdev.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 com.ttdev.components;
  2. import java.util.*;
  3. import org.apache.tapestry.*;
  4. import org.apache.tapestry.annotations.*;
  5. public abstract class Copyright extends BaseComponent {
  6.     @Parameter(name="holder", required=false, defaultValue="literal:Foo Inc.")
  7.     public abstract String getHolder();
  8.     public int getCurrentYear() {
  9.         return new GregorianCalendar().get(GregorianCalendar.YEAR);
  10.     }
  11. }

Home.java

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

RGB.java

  1. package com.ttdev.components;
  2. import org.apache.tapestry.*;
  3. import org.apache.tapestry.annotations.*;
  4. public abstract class RGB extends BaseComponent {
  5.     public abstract int getRed();
  6.     public abstract void setRed(int red);
  7.     public abstract int getGreen();
  8.     public abstract void setGreen(int green);
  9.     public abstract int getBlue();
  10.     public abstract void setBlue(int blue);
  11.     @Parameter(required=true)
  12.     public abstract int getColor();
  13.     public abstract void setColor(int color);
  14.     protected void renderComponent(IMarkupWriter writer, IRequestCycle cycle) {
  15.         if (!cycle.isRewinding()) {
  16.             setRed(getColor() >> 16);
  17.             setGreen((getColor() >> 8) & 0xff);
  18.             setBlue(getColor() & 0xff);
  19.         }
  20.         super.renderComponent(writer, cycle);
  21.         if (cycle.isRewinding()) {
  22.             setColor((getRed() << 16) | (getGreen() << 8) | getBlue());
  23.         }
  24.     }
  25.     public String getBackgroundColorAssignment() {
  26.         return "background-color: rgb(" + getRed() + "," + getGreen() + "," + getBlue() + ")";
  27.     }
  28. }

Box.jwc

  1. <?xml version="1.0"?>
  2. <!DOCTYPE component-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
  5. <component-specification
  6.     allow-body="yes"
  7.     allow-informal-parameters="yes">
  8. </component-specification>

Components.library

  1. <?xml version="1.0"?>
  2. <!DOCTYPE library-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
  5. <library-specification>
  6.     <meta key="org.apache.tapestry.component-class-packages" value="com.ttdev.components"/>
  7. </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"?>
  2. <!DOCTYPE component-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
  5. <component-specification
  6.     allow-body="no">
  7.     <description>It renders a copyright notice.</description>
  8.     <component id="year" type="Insert">
  9.         <binding name="value" value="currentYear"/>
  10.     </component>
  11.     <component id="holder" type="Insert">
  12.         <binding name="value" value="holder"/>
  13.     </component>
  14. </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"?>
  2. <!DOCTYPE component-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
  5. <component-specification allow-body="no" allow-informal-parameters="no">
  6.     <component id="red" type="TextField">
  7.         <binding name="value" value="red"/>
  8.     </component>
  9.     <component id="green" type="TextField">
  10.         <binding name="value" value="green"/>
  11.     </component>
  12.     <component id="blue" type="TextField">
  13.         <binding name="value" value="blue"/>
  14.     </component>
  15.     <component id="sample" type="Any">
  16.         <binding name="element" value="literal:span"/>
  17.         <binding name="style" value="ognl:backgroundColorAssignment"/>
  18.     </component>
  19. </component-specification>

Components.application

  1. <?xml version="1.0"?>
  2. <!DOCTYPE application PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
  5. <application name="Components">
  6.     <meta key="org.apache.tapestry.component-class-packages" value="com.ttdev.components"/>
  7.     <component-type
  8.         type="Copyright"
  9.         specification-path="classes/com/ttdev/components/Copyright.jwc"/>
  10.     <component-type
  11.         type="Box"
  12.         specification-path="classes/com/ttdev/components/Box.jwc"/>
  13.     <component-type
  14.         type="RGB"
  15.         specification-path="classes/com/ttdev/components/RGB.jwc"/>
  16. </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="rgb">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"?>
  2. <!DOCTYPE page-specification PUBLIC
  3.   "-//Apache Software Foundation//Tapestry Specification 4.0//EN"
  4.   "http://jakarta.apache.org/tapestry/dtd/Tapestry_4_0.dtd">
  5. <page-specification class="com.ttdev.components.Home">
  6.     <component id="copyright" type="Copyright">
  7.     </component>
  8.     <component id="box" type="Box"/>
  9.     <component id="form" type="Form">
  10.         <binding name="listener" value="listener:onOk"/>
  11.     </component>
  12.     <component id="rgb" type="RGB">
  13.         <binding name="color" value="color"/>
  14.     </component>
  15. </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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值