如下图所示建立工程:
使用的lib包是MyEclipse6自带的。
代码如下:
Cart.java
- package mo.org.cpttm.shop;
- import java.util.*;
- import org.apache.tapestry.*;
- import org.apache.tapestry.html.*;
- public class Cart extends BasePage {
- private Product currentProduct;
- public List getProducts() {
- List cart = ((Visit) getVisit()).getCart();
- List products = new ArrayList();
- for (Iterator iter = cart.iterator(); iter.hasNext();) {
- String productId = (String) iter.next();
- products.add(((Global) getGlobal()).getGlobalCatalog().lookup(
- productId));
- }
- return products;
- }
- public Product getCurrentProduct() {
- return currentProduct;
- }
- public void setCurrentProduct(Product currentProduct) {
- this.currentProduct = currentProduct;
- }
- public void onCheckout(IRequestCycle cycle) {
- cycle.activate("Confirm");
- }
- public void continueShopping(IRequestCycle cycle) {
- cycle.activate("Home");
- }
- }
Catalog.java
- package mo.org.cpttm.shop;
- import java.util.*;
- public class Catalog {
- private List products;
- public Catalog() {
- products = new ArrayList();
- }
- public List getProducts() {
- return products;
- }
- public void add(Product product) {
- products.add(product);
- }
- public Product lookup(String productId) {
- for (Iterator iter = products.iterator(); iter.hasNext();) {
- Product product = (Product) iter.next();
- if (product.getId().equals(productId)) {
- return product;
- }
- }
- throw new IllegalArgumentException("Unknown product id: " + productId);
- }
- }
Confirm.java
- package mo.org.cpttm.shop;
- import java.util.*;
- import org.apache.tapestry.*;
- import org.apache.tapestry.callback.*;
- import org.apache.tapestry.event.*;
- import org.apache.tapestry.html.*;
- public class Confirm extends BasePage implements PageValidateListener {
- public double getTotal() {
- double total = 0;
- Visit visit = (Visit) getVisit();
- for (Iterator iter = visit.getCart().iterator(); iter.hasNext();) {
- String productId = (String) iter.next();
- total += ((Global) getGlobal()).getGlobalCatalog().lookup(productId).getPrice();
- }
- return total;
- }
- public String getCreditCardNo() {
- Visit visit = (Visit) getVisit();
- return visit.getLoggedInUser().getCreditCardNo();
- }
- public void onConfirm(IRequestCycle cycle) {
- //place the order.
- System.out.println("Placing the order...");
- cycle.activate("Home");
- }
- public void onContinueShopping(IRequestCycle cycle) {
- cycle.activate("Home");
- }
- public void pageValidate(PageEvent event) {
- Visit visit = (Visit) getVisit();
- User loggedInUser = visit.getLoggedInUser();
- if (loggedInUser == null) {
- Login login = (Login) event.getRequestCycle().getPage("Login");
- // login.setNextPage("Confirm");
- login.setNextPage(new PageCallback("Confirm"));
- throw new PageRedirectException(login);
- }
- }
- }
Global.java
- package mo.org.cpttm.shop;
- public class Global {
- private Catalog globalCatalog;
- private Users knownUsers;
- public Global() {
- globalCatalog = new Catalog();
- globalCatalog.add(new Product("p01", "Pencil", "a", 1.20));
- globalCatalog.add(new Product("p02", "Eraser", "b", 2.00));
- globalCatalog.add(new Product("p03", "Ball pen", "c", 3.50));
- knownUsers = new Users();
- knownUsers.add(new User("u001", "paul@yahoo.com", "aaa", "1111 2222 3333 4444"));
- knownUsers.add(new User("u002", "john@hotmail.com", "bbb", "2222 3333 4444 5555"));
- knownUsers.add(new User("u003", "mary@gmail.com", "aaa", "3333 4444 5555 6666"));
- }
- public Catalog getGlobalCatalog() {
- return globalCatalog;
- }
- public Users getKnownUsers() {
- return knownUsers;
- }
- }
Home.java
- package mo.org.cpttm.shop;
- import java.util.*;
- import org.apache.tapestry.*;
- import org.apache.tapestry.html.*;
- public class Home extends BasePage {
- public List getProducts() {
- return ((Global) getGlobal()).getGlobalCatalog().getProducts();
- }
- public void onClickDetailsLink(IRequestCycle cycle) {
- String productId = (String) cycle.getServiceParameters()[0];
- IPage page = cycle.getPage("ProductDetails");
- ProductDetails detailsPage = (ProductDetails) page;
- detailsPage.setProductId(productId);
- cycle.activate(detailsPage);
- }
- }
Login.java
- package mo.org.cpttm.shop;
- import org.apache.tapestry.*;
- import org.apache.tapestry.callback.*;
- import org.apache.tapestry.html.*;
- import org.apache.tapestry.valid.*;
- public abstract class Login extends BasePage {
- abstract public String getEmail();
- abstract public String getPassword();
- abstract public void setNextPage(ICallback callback);
- abstract public ICallback getNextPage();
- public void onLogin(IRequestCycle cycle) {
- try {
- User user = ((Global) getGlobal()).getKnownUsers().getUser(
- getEmail(),
- getPassword());
- Visit visit = (Visit)getVisit();
- visit.setLoggedInUser(user);
- ICallback callback = getNextPage();
- if (callback != null) {
- callback.performCallback(cycle);
- } else {
- cycle.activate("Home");
- }
- } catch (RuntimeException e) {
- ValidationDelegate delegate =
- (ValidationDelegate) getBeans().getBean("validationDelegate");
- delegate.setFormComponent(null);
- delegate.record("Login failed", null);
- }
- }
- }
Product.java
- package mo.org.cpttm.shop;
- public class Product {
- private String id;
- private String name;
- private String desc;
- private double price;
- public Product(String id, String name, String desc, double price) {
- this.id = id;
- this.name = name;
- this.desc = desc;
- this.price = price;
- }
- public String getId() {
- return id;
- }
- public String getName() {
- return name;
- }
- public double getPrice() {
- return price;
- }
- public String getDesc() {
- return desc;
- }
- }
ProductDetails.java
- package mo.org.cpttm.shop;
- import java.util.*;
- import org.apache.tapestry.*;
- import org.apache.tapestry.callback.*;
- import org.apache.tapestry.html.*;
- public class ProductDetails extends BasePage implements IExternalPage {
- private String productId;
- public void setProductId(String id) {
- this.productId = id;
- }
- public void activateExternalPage(Object[] parameters, IRequestCycle cycle) {
- setProductId((String) parameters[0]);
- }
- public String getName() {
- return lookup().getName();
- }
- public String getDesc() {
- return lookup().getDesc();
- }
- private Product lookup() {
- return ((Global) getGlobal()).getGlobalCatalog().lookup(productId);
- }
- public String getProductId() {
- return productId;
- }
- public void addToCart(IRequestCycle cycle) {
- List cart = ((Visit) getVisit()).getCart();
- cart.add(productId);
- cycle.activate("Cart");
- }
- public void continueShopping(IRequestCycle cycle) {
- cycle.activate("Home");
- }
- public void login(IRequestCycle cycle) {
- setProductId((String) cycle.getServiceParameters()[0]);
- Login login = (Login) cycle.getPage("Login");
- login.setNextPage(new ExternalCallback(this,
- new String[] { getProductId() }));
- cycle.activate(login);
- }
- }
User.java
- package mo.org.cpttm.shop;
- import java.io.*;
- public class User implements Serializable {
- private String id;
- private String email;
- private String password;
- private String creditCardNo;
- public User(String id, String email, String password, String creditCardNo) {
- this.id = id;
- this.email = email;
- this.password = password;
- this.creditCardNo = creditCardNo;
- }
- public boolean authenticate(String email, String password) {
- return this.email.equals(email) && this.password.equals(password);
- }
- public String getCreditCardNo() {
- return creditCardNo;
- }
- }
Users.java
- package mo.org.cpttm.shop;
- import java.util.*;
- public class Users {
- private List users;
- public Users() {
- users = new ArrayList();
- }
- public void add(User user) {
- users.add(user);
- }
- public User getUser(String email, String password) {
- for (Iterator iter = users.iterator(); iter.hasNext();) {
- User user = (User) iter.next();
- if (user.authenticate(email, password)) {
- return user;
- }
- }
- throw new RuntimeException();
- }
- }
Visit.java
- package mo.org.cpttm.shop;
- import java.io.*;
- import java.util.*;
- public class Visit implements Serializable {
- private List cart;
- private User loggedInUser;
- public Visit() {
- cart = new ArrayList();
- }
- public List getCart() {
- return cart;
- }
- public User getLoggedInUser() {
- return loggedInUser;
- }
- public void setLoggedInUser(User loggedInUser) {
- this.loggedInUser = loggedInUser;
- }
- }
Cart.html
- <html>
- <head>
- <title>Shopping cart</title>
- </head>
- <body>
- <h1>Shopping cart</h1>
- <table border="1">
- <tr jwcid="products">
- <td><span jwcid="id">p01</span></td>
- <td><span jwcid="name">Pencil</span></td>
- <td><span jwcid="price">1.20</span></td>
- </tr>
- </table>
- <form jwcid="cartActionForm">
- <input type="submit" value="Checkout" jwcid="checkout"/>
- <input type="submit" value="Continue shopping" jwcid="continueShopping"/>
- </form>
- </body>
- </html>
Cart.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.shop.Cart">
- <component id="products" type="Foreach">
- <binding name="source" expression="products"/>
- <binding name="value" expression="currentProduct"/>
- <static-binding name="element" value="tr"/>
- </component>
- <component id="id" type="Insert">
- <binding name="value" expression="currentProduct.id"/>
- </component>
- <component id="name" type="Insert">
- <binding name="value" expression="currentProduct.name"/>
- </component>
- <component id="price" type="Insert">
- <binding name="value" expression="currentProduct.price"/>
- </component>
- <component id="cartActionForm" type="Form"/>
- <component id="checkout" type="Submit">
- <binding name="listener" expression="listeners.onCheckout"/>
- </component>
- <component id="continueShopping" type="Submit">
- <binding name="listener" expression="listeners.continueShopping"/>
- </component>
- </page-specification>
Confirm.html
- <html>
- <head>
- <title>Confirmation</title>
- </head>
- <body>
- <h1>Confirm your order</h1>
- You're going to pay <span jwcid="total">100</span> with your
- credit card <span jwcid="creditCardNo">xxxx yyyy zzzz</span>.
- <p>
- <form jwcid="confirmForm">
- <input type="submit" value="Confirm" jwcid="confirm"/>
- <input type="submit" value="Continue shopping" jwcid="continueShopping"/>
- </form>
- </body>
- </html>
Confirm.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.shop.Confirm">
- <component id="total" type="Insert">
- <binding name="value" expression="total"/>
- </component>
- <component id="creditCardNo" type="Insert">
- <binding name="value" expression="creditCardNo"/>
- </component>
- <component id="confirmForm" type="Form"/>
- <component id="confirm" type="Submit">
- <binding name="listener" expression="listeners.onConfirm"/>
- </component>
- <component id="continueShopping" type="Submit">
- <binding name="listener" expression="listeners.onContinueShopping"/>
- </component>
- </page-specification>
Home.html
- <html>
- <head>
- <title>Shop</title>
- </head>
- <body>
- <h1>Product listing</h1>
- <table border="1">
- <tr jwcid="products">
- <td><span jwcid="id">p01</span></td>
- <td><span jwcid="detailsLink"><span jwcid="name">Pencil</span></span></td>
- <td><span jwcid="price">1.20</span></td>
- </tr>
- </table>
- <p>
- <a href="" jwcid="loginLink">Login</a>
- <a href="" jwcid="logoutLink">Logout</a>
- </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.shop.Home">
- <property-specification name="currentProduct" type="mo.org.cpttm.shop.Product"/>
- <component id="products" type="Foreach">
- <binding name="source" expression="products"/>
- <binding name="value" expression="currentProduct"/>
- <static-binding name="element" value="tr"/>
- </component>
- <component id="id" type="Insert">
- <binding name="value" expression="currentProduct.id"/>
- </component>
- <component id="name" type="Insert">
- <binding name="value" expression="currentProduct.name"/>
- </component>
- <component id="price" type="Insert">
- <binding name="value" expression="currentProduct.price"/>
- </component>
- <component id="detailsLink" type="DirectLink">
- <binding name="listener" expression="listeners.onClickDetailsLink"/>
- <binding name="parameters" expression="currentProduct.id"/>
- </component>
- <component id="loginLink" type="PageLink">
- <static-binding name="page" value="Login"/>
- </component>
- <component id="logoutLink" type="ServiceLink">
- <static-binding name="service" value="restart"/>
- </component>
- </page-specification>
Login.html
- <html>
- <head>
- <title>Login</title>
- </head>
- <body>
- <h1>Login</h1>
- <span style="color: red"><span jwcid="errorMsg"/></span>
- <form jwcid="loginForm">
- <table border="0">
- <tr><td>Email:</td><td><input type="text" jwcid="email"/></td></tr>
- <tr><td>Password:</td><td><input type="password" jwcid="password"/></td></tr>
- <tr><td></td><td><input type="submit" value="Login"/></td></tr>
- </table>
- </form>
- </body>
- </html>
Login.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.shop.Login">
- <bean name="validationDelegate" class="org.apache.tapestry.valid.ValidationDelegate"/>
- <property-specification
- name="nextPage"
- type="org.apache.tapestry.callback.ICallback"
- persistent="yes"
- initial-value="'Home'"/>
- <property-specification name="email" type="java.lang.String"/>
- <property-specification name="password" type="java.lang.String"/>
- <component id="loginForm" type="Form">
- <binding name="listener" expression="listeners.onLogin"/>
- </component>
- <component id="email" type="TextField">
- <binding name="value" expression="email"/>
- </component>
- <component id="password" type="TextField">
- <binding name="value" expression="password"/>
- <static-binding name="hidden" value="true"/>
- </component>
- <component id="errorMsg" type="Delegator">
- <binding name="delegate" expression="beans.validationDelegate.firstError"/>
- </component>
- </page-specification>
ProductDetails.html
- <html>
- <head>
- <title><span jwcid="name">Pencil</span></title>
- </head>
- <body>
- <h1><span jwcid="name2">Pencil</span></h1>
- <span jwcid="desc">xxx</span>
- <form jwcid="productActionForm">
- <input type="hidden" jwcid="productId"/>
- <input type="submit" value="Add to cart" jwcid="addToCart"/>
- <input type="submit" value="Continue shopping" jwcid="continueShopping"/>
- </form>
- <p>
- <a href="" jwcid="loginLink">Login</a>
- </body>
- </html>
ProductDetails.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.shop.ProductDetails">
- <component id="name" type="Insert">
- <binding name="value" expression="name"/>
- </component>
- <component id="name2" copy-of="name"/>
- <component id="desc" type="Insert">
- <binding name="value" expression="desc"/>
- </component>
- <component id="productActionForm" type="Form">
- </component>
- <component id="productId" type="Hidden">
- <binding name="value" expression="productId"/>
- </component>
- <component id="addToCart" type="Submit">
- <binding name="listener" expression="listeners.addToCart"/>
- </component>
- <component id="continueShopping" type="Submit">
- <binding name="listener" expression="listeners.continueShopping"/>
- </component>
- <component id="loginLink" type="DirectLink">
- <binding name="listener" expression="listeners.login"/>
- <binding name="parameters" expression="productId"/>
- </component>
- </page-specification>
Shop.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="Shop" engine-class="org.apache.tapestry.engine.BaseEngine">
- <description>add a description</description>
- <property name="org.apache.tapestry.visit-class" value="mo.org.cpttm.shop.Visit"/>
- <property name="org.apache.tapestry.global-class" value="mo.org.cpttm.shop.Global"/>
- <page name="Home" specification-path="Home.page"/>
- </application>
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>Shop</display-name>
- <servlet>
- <servlet-name>Shop</servlet-name>
- <servlet-class>org.apache.tapestry.ApplicationServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>Shop</servlet-name>
- <url-pattern>/app</url-pattern>
- </servlet-mapping>
- </web-app>