Tapstry3的追忆------综合示例演示

如下图所示建立工程:

使用的lib包是MyEclipse6自带的。

代码如下:

Cart.java

  1. package mo.org.cpttm.shop;
  2. import java.util.*;
  3. import org.apache.tapestry.*;
  4. import org.apache.tapestry.html.*;
  5. public class Cart extends BasePage {
  6.     private Product currentProduct;
  7.     public List getProducts() {
  8.         List cart = ((Visit) getVisit()).getCart();
  9.         List products = new ArrayList();
  10.         for (Iterator iter = cart.iterator(); iter.hasNext();) {
  11.             String productId = (String) iter.next();
  12.             products.add(((Global) getGlobal()).getGlobalCatalog().lookup(
  13.                     productId));
  14.         }
  15.         return products;
  16.     }
  17.     public Product getCurrentProduct() {
  18.         return currentProduct;
  19.     }
  20.     public void setCurrentProduct(Product currentProduct) {
  21.         this.currentProduct = currentProduct;
  22.     }
  23.     public void onCheckout(IRequestCycle cycle) {
  24.         cycle.activate("Confirm");
  25.     }
  26.     public void continueShopping(IRequestCycle cycle) {
  27.         cycle.activate("Home");
  28.     }
  29. }

Catalog.java

  1. package mo.org.cpttm.shop;
  2. import java.util.*;
  3. public class Catalog {
  4.     private List products;
  5.     public Catalog() {
  6.         products = new ArrayList();
  7.     }
  8.     public List getProducts() {
  9.         return products;
  10.     }
  11.     public void add(Product product) {
  12.         products.add(product);
  13.     }
  14.     public Product lookup(String productId) {
  15.         for (Iterator iter = products.iterator(); iter.hasNext();) {
  16.             Product product = (Product) iter.next();
  17.             if (product.getId().equals(productId)) {
  18.                 return product;
  19.             }
  20.         }
  21.         throw new IllegalArgumentException("Unknown product id: " + productId);
  22.     }
  23. }

Confirm.java

  1. package mo.org.cpttm.shop;
  2. import java.util.*;
  3. import org.apache.tapestry.*;
  4. import org.apache.tapestry.callback.*;
  5. import org.apache.tapestry.event.*;
  6. import org.apache.tapestry.html.*;
  7. public class Confirm extends BasePage implements PageValidateListener {
  8.     public double getTotal() {
  9.         double total = 0;
  10.         Visit visit = (Visit) getVisit();
  11.         for (Iterator iter = visit.getCart().iterator(); iter.hasNext();) {
  12.             String productId = (String) iter.next();
  13.             total += ((Global) getGlobal()).getGlobalCatalog().lookup(productId).getPrice();
  14.         }
  15.         return total;
  16.     }
  17.     public String getCreditCardNo() {
  18.         Visit visit = (Visit) getVisit();
  19.         return visit.getLoggedInUser().getCreditCardNo();
  20.     }
  21.     public void onConfirm(IRequestCycle cycle) {
  22.         //place the order.
  23.         System.out.println("Placing the order...");
  24.         cycle.activate("Home");
  25.     }
  26.     public void onContinueShopping(IRequestCycle cycle) {
  27.         cycle.activate("Home");
  28.     }
  29.     public void pageValidate(PageEvent event) {
  30.         Visit visit = (Visit) getVisit();
  31.         User loggedInUser = visit.getLoggedInUser();
  32.         if (loggedInUser == null) {
  33.             Login login = (Login) event.getRequestCycle().getPage("Login");
  34. //          login.setNextPage("Confirm");
  35.             login.setNextPage(new PageCallback("Confirm"));
  36.             throw new PageRedirectException(login);
  37.         }
  38.     }
  39. }

Global.java

  1. package mo.org.cpttm.shop;
  2. public class Global {
  3.     private Catalog globalCatalog;
  4.     private Users knownUsers;
  5.     
  6.     public Global() {
  7.         globalCatalog = new Catalog();
  8.         globalCatalog.add(new Product("p01""Pencil""a"1.20));
  9.         globalCatalog.add(new Product("p02""Eraser""b"2.00));
  10.         globalCatalog.add(new Product("p03""Ball pen""c"3.50));
  11.         knownUsers = new Users();
  12.         knownUsers.add(new User("u001""paul@yahoo.com""aaa""1111 2222 3333 4444"));
  13.         knownUsers.add(new User("u002""john@hotmail.com""bbb""2222 3333 4444 5555"));
  14.         knownUsers.add(new User("u003""mary@gmail.com""aaa""3333 4444 5555 6666"));
  15.     }
  16.     public Catalog getGlobalCatalog() {
  17.         return globalCatalog;
  18.     }
  19.     public Users getKnownUsers() {
  20.         return knownUsers;
  21.     }
  22. }

Home.java

  1. package mo.org.cpttm.shop;
  2. import java.util.*;
  3. import org.apache.tapestry.*;
  4. import org.apache.tapestry.html.*;
  5. public class Home extends BasePage {
  6.     public List getProducts() {
  7.         return ((Global) getGlobal()).getGlobalCatalog().getProducts();
  8.     }
  9.     public void onClickDetailsLink(IRequestCycle cycle) {
  10.         String productId = (String) cycle.getServiceParameters()[0];
  11.         IPage page = cycle.getPage("ProductDetails");
  12.         ProductDetails detailsPage = (ProductDetails) page;
  13.         detailsPage.setProductId(productId);
  14.         cycle.activate(detailsPage);
  15.     }
  16. }

Login.java

  1. package mo.org.cpttm.shop;
  2. import org.apache.tapestry.*;
  3. import org.apache.tapestry.callback.*;
  4. import org.apache.tapestry.html.*;
  5. import org.apache.tapestry.valid.*;
  6. public abstract class Login extends BasePage {
  7.     abstract public String getEmail();
  8.     abstract public String getPassword();
  9.     abstract public void setNextPage(ICallback callback);
  10.     abstract public ICallback getNextPage();
  11.     
  12.     public void onLogin(IRequestCycle cycle) {
  13.         try {
  14.             User user = ((Global) getGlobal()).getKnownUsers().getUser(
  15.                     getEmail(),
  16.                     getPassword());
  17.             Visit visit = (Visit)getVisit();
  18.             visit.setLoggedInUser(user);
  19.             ICallback callback = getNextPage();
  20.             if (callback != null) {
  21.                 callback.performCallback(cycle);
  22.             } else {
  23.                 cycle.activate("Home");
  24.             }
  25.         } catch (RuntimeException e) {
  26.             ValidationDelegate delegate = 
  27.                 (ValidationDelegate) getBeans().getBean("validationDelegate");
  28.             delegate.setFormComponent(null);
  29.             delegate.record("Login failed"null);
  30.         }
  31.     }
  32. }

Product.java

  1. package mo.org.cpttm.shop;
  2. public class Product {
  3.     private String id;
  4.     private String name;
  5.     private String desc;
  6.     private double price;
  7.     public Product(String id, String name, String desc, double price) {
  8.         this.id = id;
  9.         this.name = name;
  10.         this.desc = desc;
  11.         this.price = price;
  12.     }
  13.     public String getId() {
  14.         return id;
  15.     }
  16.     public String getName() {
  17.         return name;
  18.     }
  19.     public double getPrice() {
  20.         return price;
  21.     }
  22.     public String getDesc() {
  23.         return desc;
  24.     }
  25. }

ProductDetails.java

  1. package mo.org.cpttm.shop;
  2. import java.util.*;
  3. import org.apache.tapestry.*;
  4. import org.apache.tapestry.callback.*;
  5. import org.apache.tapestry.html.*;
  6. public class ProductDetails extends BasePage implements IExternalPage {
  7.     private String productId;
  8.     public void setProductId(String id) {
  9.         this.productId = id;
  10.     }
  11.     public void activateExternalPage(Object[] parameters, IRequestCycle cycle) {
  12.         setProductId((String) parameters[0]);
  13.     }
  14.     public String getName() {
  15.         return lookup().getName();
  16.     }
  17.     public String getDesc() {
  18.         return lookup().getDesc();
  19.     }
  20.     private Product lookup() {
  21.         return ((Global) getGlobal()).getGlobalCatalog().lookup(productId);
  22.     }
  23.     public String getProductId() {
  24.         return productId;
  25.     }
  26.     public void addToCart(IRequestCycle cycle) {
  27.         List cart = ((Visit) getVisit()).getCart();
  28.         cart.add(productId);
  29.         cycle.activate("Cart");
  30.     }
  31.     public void continueShopping(IRequestCycle cycle) {
  32.         cycle.activate("Home");
  33.     }
  34.     public void login(IRequestCycle cycle) {
  35.         setProductId((String) cycle.getServiceParameters()[0]);
  36.         Login login = (Login) cycle.getPage("Login");
  37.         login.setNextPage(new ExternalCallback(this,
  38.                 new String[] { getProductId() }));
  39.         cycle.activate(login);
  40.     }
  41. }

User.java

  1. package mo.org.cpttm.shop;
  2. import java.io.*;
  3. public class User implements Serializable {
  4.     private String id;
  5.     private String email;
  6.     private String password;
  7.     private String creditCardNo;
  8.     public User(String id, String email, String password, String creditCardNo) {
  9.         this.id = id;
  10.         this.email = email;
  11.         this.password = password;
  12.         this.creditCardNo = creditCardNo;
  13.     }
  14.     public boolean authenticate(String email, String password) {
  15.         return this.email.equals(email) && this.password.equals(password);
  16.     }
  17.     public String getCreditCardNo() {
  18.         return creditCardNo;
  19.     }
  20. }

Users.java

  1. package mo.org.cpttm.shop;
  2. import java.util.*;
  3. public class Users {
  4.     private List users;
  5.     public Users() {
  6.         users = new ArrayList();
  7.     }
  8.     public void add(User user) {
  9.         users.add(user);
  10.     }
  11.     public User getUser(String email, String password) {
  12.         for (Iterator iter = users.iterator(); iter.hasNext();) {
  13.             User user = (User) iter.next();
  14.             if (user.authenticate(email, password)) {
  15.                 return user;
  16.             }
  17.         }
  18.         throw new RuntimeException();
  19.     }
  20. }

Visit.java

  1. package mo.org.cpttm.shop;
  2. import java.io.*;
  3. import java.util.*;
  4. public class Visit implements Serializable {
  5.     private List cart;
  6.     private User loggedInUser;
  7.     public Visit() {
  8.         cart = new ArrayList();
  9.     }
  10.     public List getCart() {
  11.         return cart;
  12.     }
  13.     public User getLoggedInUser() {
  14.         return loggedInUser;
  15.     }
  16.     public void setLoggedInUser(User loggedInUser) {
  17.         this.loggedInUser = loggedInUser;
  18.     }
  19. }

Cart.html

  1. <html>
  2. <head>
  3. <title>Shopping cart</title>
  4. </head>
  5. <body>
  6. <h1>Shopping cart</h1>
  7. <table border="1">
  8.     <tr jwcid="products">
  9.         <td><span jwcid="id">p01</span></td>
  10.         <td><span jwcid="name">Pencil</span></td>
  11.         <td><span jwcid="price">1.20</span></td>
  12.     </tr>
  13. </table>
  14. <form jwcid="cartActionForm">
  15.     <input type="submit" value="Checkout" jwcid="checkout"/>
  16.     <input type="submit" value="Continue shopping" jwcid="continueShopping"/>
  17. </form>
  18. </body>
  19. </html>

Cart.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.shop.Cart">
  7.     <component id="products" type="Foreach">
  8.         <binding name="source" expression="products"/>
  9.         <binding name="value" expression="currentProduct"/>
  10.         <static-binding name="element" value="tr"/>
  11.     </component>
  12.     <component id="id" type="Insert">
  13.         <binding name="value" expression="currentProduct.id"/>
  14.     </component>
  15.     <component id="name" type="Insert">
  16.         <binding name="value" expression="currentProduct.name"/>
  17.     </component>
  18.     <component id="price" type="Insert">
  19.         <binding name="value" expression="currentProduct.price"/>
  20.     </component>
  21.     <component id="cartActionForm" type="Form"/>
  22.     <component id="checkout" type="Submit">
  23.         <binding name="listener" expression="listeners.onCheckout"/>
  24.     </component>
  25.     <component id="continueShopping" type="Submit">
  26.         <binding name="listener" expression="listeners.continueShopping"/>
  27.     </component>
  28. </page-specification>

Confirm.html

  1. <html>
  2. <head>
  3. <title>Confirmation</title>
  4. </head>
  5. <body>
  6. <h1>Confirm your order</h1>
  7. You're going to pay <span jwcid="total">100</span> with your
  8. credit card <span jwcid="creditCardNo">xxxx yyyy zzzz</span>.
  9. <p>
  10. <form jwcid="confirmForm">
  11.     <input type="submit" value="Confirm" jwcid="confirm"/>
  12.     <input type="submit" value="Continue shopping" jwcid="continueShopping"/>
  13. </form>
  14. </body>
  15. </html>

Confirm.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.shop.Confirm">
  7.     <component id="total" type="Insert">
  8.         <binding name="value" expression="total"/>
  9.     </component>
  10.     <component id="creditCardNo" type="Insert">
  11.         <binding name="value" expression="creditCardNo"/>
  12.     </component>
  13.     <component id="confirmForm" type="Form"/>
  14.     <component id="confirm" type="Submit">
  15.         <binding name="listener" expression="listeners.onConfirm"/>
  16.     </component>
  17.     <component id="continueShopping" type="Submit">
  18.         <binding name="listener" expression="listeners.onContinueShopping"/>
  19.     </component>
  20. </page-specification>

Home.html

  1. <html>
  2. <head>
  3. <title>Shop</title>
  4. </head>
  5. <body>
  6. <h1>Product listing</h1>
  7. <table border="1">
  8.     <tr jwcid="products">
  9.         <td><span jwcid="id">p01</span></td>
  10.         <td><span jwcid="detailsLink"><span jwcid="name">Pencil</span></span></td>
  11.         <td><span jwcid="price">1.20</span></td>
  12.     </tr>
  13. </table>  
  14. <p>
  15. <a href="" jwcid="loginLink">Login</a>
  16. <a href="" jwcid="logoutLink">Logout</a>
  17. </body>
  18. </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.shop.Home">
  7.     <property-specification name="currentProduct" type="mo.org.cpttm.shop.Product"/>
  8.     <component id="products" type="Foreach">
  9.         <binding name="source" expression="products"/>
  10.         <binding name="value" expression="currentProduct"/>
  11.         <static-binding name="element" value="tr"/>
  12.     </component>
  13.     <component id="id" type="Insert">
  14.         <binding name="value" expression="currentProduct.id"/>
  15.     </component>
  16.     <component id="name" type="Insert">
  17.         <binding name="value" expression="currentProduct.name"/>
  18.     </component>
  19.     <component id="price" type="Insert">
  20.         <binding name="value" expression="currentProduct.price"/>
  21.     </component>
  22.     <component id="detailsLink" type="DirectLink">
  23.         <binding name="listener" expression="listeners.onClickDetailsLink"/>
  24.         <binding name="parameters" expression="currentProduct.id"/>
  25.     </component>
  26.     <component id="loginLink" type="PageLink">
  27.         <static-binding name="page" value="Login"/>
  28.     </component>
  29.     <component id="logoutLink" type="ServiceLink">
  30.         <static-binding name="service" value="restart"/>
  31.     </component>
  32. </page-specification>

Login.html

  1. <html>
  2. <head>
  3. <title>Login</title>
  4. </head>
  5. <body>
  6. <h1>Login</h1>
  7. <span style="color: red"><span jwcid="errorMsg"/></span>
  8. <form jwcid="loginForm">
  9.     <table border="0">
  10.     <tr><td>Email:</td><td><input type="text" jwcid="email"/></td></tr>
  11.     <tr><td>Password:</td><td><input type="password" jwcid="password"/></td></tr>
  12.     <tr><td></td><td><input type="submit" value="Login"/></td></tr>
  13.     </table>
  14. </form>
  15. </body>
  16. </html>

Login.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.shop.Login">
  7.     <bean name="validationDelegate" class="org.apache.tapestry.valid.ValidationDelegate"/>
  8.     <property-specification 
  9.         name="nextPage"
  10.         type="org.apache.tapestry.callback.ICallback"
  11.         persistent="yes"
  12.         initial-value="'Home'"/>
  13.     <property-specification name="email" type="java.lang.String"/>
  14.     <property-specification name="password" type="java.lang.String"/>
  15.     <component id="loginForm" type="Form">
  16.         <binding name="listener" expression="listeners.onLogin"/>
  17.     </component>
  18.     <component id="email" type="TextField">
  19.         <binding name="value" expression="email"/>
  20.     </component>
  21.     <component id="password" type="TextField">
  22.         <binding name="value" expression="password"/>
  23.         <static-binding name="hidden" value="true"/>
  24.     </component>
  25.     <component id="errorMsg" type="Delegator">
  26.         <binding name="delegate" expression="beans.validationDelegate.firstError"/>
  27.     </component>
  28. </page-specification>

ProductDetails.html

  1. <html>
  2. <head>
  3. <title><span jwcid="name">Pencil</span></title>
  4. </head>
  5. <body>
  6. <h1><span jwcid="name2">Pencil</span></h1>
  7. <span jwcid="desc">xxx</span>   
  8. <form jwcid="productActionForm">
  9. <input type="hidden" jwcid="productId"/>
  10. <input type="submit" value="Add to cart" jwcid="addToCart"/>
  11. <input type="submit" value="Continue shopping" jwcid="continueShopping"/>
  12. </form>
  13. <p>
  14. <a href="" jwcid="loginLink">Login</a>
  15. </body>
  16. </html>

ProductDetails.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.shop.ProductDetails">
  7.     <component id="name" type="Insert">
  8.         <binding name="value" expression="name"/>
  9.     </component>
  10.     <component id="name2" copy-of="name"/>
  11.     <component id="desc" type="Insert">
  12.         <binding name="value" expression="desc"/>
  13.     </component>
  14.     <component id="productActionForm" type="Form">
  15.     </component>
  16.     <component id="productId" type="Hidden">
  17.         <binding name="value" expression="productId"/>
  18.     </component>
  19.     <component id="addToCart" type="Submit">
  20.         <binding name="listener" expression="listeners.addToCart"/>
  21.     </component>
  22.     <component id="continueShopping" type="Submit">
  23.         <binding name="listener" expression="listeners.continueShopping"/>
  24.     </component>
  25.     <component id="loginLink" type="DirectLink">
  26.         <binding name="listener" expression="listeners.login"/>
  27.         <binding name="parameters" expression="productId"/>
  28.     </component>
  29. </page-specification>

Shop.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="Shop" engine-class="org.apache.tapestry.engine.BaseEngine">
  7.     <description>add a description</description>
  8.     <property name="org.apache.tapestry.visit-class" value="mo.org.cpttm.shop.Visit"/>
  9.     <property name="org.apache.tapestry.global-class" value="mo.org.cpttm.shop.Global"/>
  10.     <page name="Home" specification-path="Home.page"/>
  11. </application>

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>Shop</display-name>
  6.     <servlet>
  7.         <servlet-name>Shop</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>Shop</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、付费专栏及课程。

余额充值