星期二, 10月 09, 2007

Wicket lab4 備忘記

Wicket lab 4 為實作 電子購物系統,
與 Wicket lab 3 一樣, 使用靜態的 map 模擬資料庫存取動作,
在 WebApplication 的實作中重寫了 newSession 及 init,
並實作 WebSession, 將登入資訊及選擇的購物車貨品暫存至 session 裡,
這裡使用了新的元件 PasswordTextField 隱藏輸入的密碼,


貨品列表以 ListView 及 ListItem 顯示於頁面,
Link 的 onClick 動作作為轉頁功能, 而 PageLink 則直接轉至頁面,
並以 Button 的 onSubmit 動作將選擇的貨品加入購物車中,
最後在 WebPage 裡呼叫 getSession().invalidate() 作為登出動作.


執行畫面如下圖所示:


開始備忘記:
[1] 實作 lab 4


[1] 實作 lab 4:
<!------------- web.xml ----------------->
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>CM269</display-name>


<filter>
<filter-name>WicketLab1</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>cm269.lab1.MyApp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>WicketLab1</filter-name>
<url-pattern>/lab1/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>WicketLab2</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>cm269.lab2.MyApp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>WicketLab2</filter-name>
<url-pattern>/lab2/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>WicketLab3</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>cm269.lab3.MyApp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>WicketLab3</filter-name>
<url-pattern>/lab3/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>WicketLab4</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>cm269.lab4.MyApp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>WicketLab4</filter-name>
<url-pattern>/lab4/*</url-pattern>
</filter-mapping>


<filter>
<filter-name>WicketLab5</filter-name>
<filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>cm269.lab5.MyApp</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>WicketLab5</filter-name>
<url-pattern>/lab5/*</url-pattern>
</filter-mapping>


</web-app>
<!------------- web.xml ----------------->


/************** MyApp.java **************/
package cm269.lab4;


import org.apache.wicket.Request;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.authorization.strategies.page.SimplePageAuthorizationStrategy;
import org.apache.wicket.protocol.http.WebApplication;


public class MyApp extends WebApplication {
public Class getHomePage() {
return ShowCatalog.class;
}


public Session newSession(Request request, Response response) {
return new MySession(this, request);
}


protected void init() {
getSecuritySettings().setAuthorizationStrategy(
new SimplePageAuthorizationStrategy(AuthenticatedPage.class,
Login.class) {
protected boolean isAuthorized() {
return ((MySession) Session.get()).getLoggedInUser() != null;
}
});
}
}
/************** MyApp.java **************/


/************** ShowCatalog.java **************/
package cm269.lab4;


import java.util.List;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.link.PageLink;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;


public class ShowCatalog extends WebPage {
public ShowCatalog() {
List products = Catalog.globalCatalog.getProducts();
ListView eachProduct = new ListView("eachProduct", products) {
protected void populateItem(ListItem item) {
final Product p = (Product) item.getModelObject();
item.setModel(new CompoundPropertyModel(p));
item.add(new Label("id"));
Link detailsLink = new Link("detailsLink") {
public void onClick() {
ProductDetails details = new ProductDetails(p);
setResponsePage(details);
}
};
detailsLink.add(new Label("name"));
item.add(detailsLink);
item.add(new Label("price"));
}
};
add(eachProduct);
add(new PageLink("loginLink", Login.class));
add(new Link("logoutLink"){
public void onClick() {
getSession().invalidate();
setResponsePage(ShowCatalog.class);
}
});
}
}
/************** ShowCatalog.java **************/


<!------------- ShowCatalog.html --------------->
<html>
<h1>Product listing</h1>
<table border="1">
<tr wicket:id="eachProduct">
<td wicket:id="id">p01</td>
<td><a wicket:id="detailsLink"><span wicket:id="name">Pencil</span></a></td>
<td wicket:id="price">1.20</td>
</tr>
</table>
<p><a wicket:id="loginLink">Login</a>
<a wicket:id="logoutLink">Logout</a>
</html>
<!------------- ShowCatalog.html --------------->


/************** ProductDetails.java **************/
package cm269.lab4;


import java.util.List;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;


public class ProductDetails extends WebPage {
public ProductDetails(final Product p) {
add(new Label("title", p.getName()));
add(new Label("heading", p.getName()));
add(new Label("desc", p.getDesc()));
Form form = new Form("productActionForm");
add(form);
form.add(new Button("addToCart") {
public void onSubmit() {
List cart = ((MySession) getSession()).getCart();
cart.add(p.getId());
setResponsePage(ShowCart.class);
}
});
form.add(new Button("continueShopping") {
public void onSubmit() {
setResponsePage(ShowCatalog.class);
}
});
}
}
/************** ProductDetails.java **************/


<!------------- ProductDetails.html ------------->
<html>
<head>
<title wicket:id="title">Pencil</title>
</head>
<body>
<h1 wicket:id="heading">Pencil</h1>
<span wicket:id="desc">xxx</span>
<form wicket:id="productActionForm">
<input type="submit"
value="Add to cart" wicket:id="addToCart" />
<input type="submit"
value="Continue shopping" wicket:id="continueShopping" />
</form>
</body>
</html>
<!------------- ProductDetails.html ------------->


/************** ShowCart.java **************/
package cm269.lab4;


import java.util.List;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.model.CompoundPropertyModel;


public class ShowCart extends WebPage {
public ShowCart() {
List cart = ((MySession) getSession()).getCart();
ListView eachProduct = new ListView("eachProduct", cart) {
protected void populateItem(ListItem item) {
String id = (String) item.getModelObject();
Product p = loadProduct(id);
item.setModel(new CompoundPropertyModel(p));
item.add(new Label("id"));
item.add(new Label("name"));
item.add(new Label("price"));
}
};
add(eachProduct);
Form form = new Form("cartActionForm");
add(form);
form.add(new Button("checkout") {
public void onSubmit() {
setResponsePage(Checkout.class);
}
});
form.add(new Button("continueShopping") {
public void onSubmit() {
setResponsePage(ShowCatalog.class);
}
});
}


private Product loadProduct(String id) {
return Catalog.globalCatalog.lookup(id);
}
}
/************** ShowCart.java **************/


<!------------- ShowCart.html ------------->
<html>
<h1>Shopping cart</h1>
<table border="1">
<tr wicket:id="eachProduct">
<td wicket:id="id">p01</td>
<td wicket:id="name">Pencil</td>
<td wicket:id="price">1.20</td>
</tr>
</table>
<form wicket:id="cartActionForm"><input type="submit"
value="Checkout" wicket:id="checkout" /> <input type="submit"
value="Continue shopping" wicket:id="continueShopping" /></form>
</html>
<!------------- ShowCart.html ------------->


/************** Checkout.java **************/
package cm269.lab4;


import java.util.Iterator;


import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;


public class Checkout extends AuthenticatedPage {
public Checkout() {
MySession session = ((MySession) getSession());
double total = 0;
for (Iterator iter = session.getCart().iterator(); iter.hasNext();) {
String productId = (String) iter.next();
total += Catalog.globalCatalog.lookup(productId).getPrice();
}
User loggedInUser = session.getLoggedInUser();


add(new Label("total", Double.toString(total)));
add(new Label("creditCardNo", loggedInUser.getCreditCardNo()));
Form form = new Form("confirmForm");
add(form);
form.add(new Button("confirm") {
public void onSubmit() {
setResponsePage(ThankYou.class);
};
});
form.add(new Button("continueShopping") {
public void onSubmit() {
setResponsePage(ShowCatalog.class);
};
});
}
}
/************** Checkout.java **************/


<!------------- Checkout.html ------------->
<html>
<h1>Confirm your order</h1>
You're going to pay
<span wicket:id="total">100</span>
with your credit card
<span wicket:id="creditCardNo">xxxx yyyy zzzz</span>
.
<p>
<form wicket:id="confirmForm"><input type="submit"
value="Confirm" wicket:id="confirm" /> <input type="submit"
value="Continue shopping" wicket:id="continueShopping" /></form>
</html>
<!------------- Checkout.html ------------->


/************** Login.java **************/
package cm269.lab4;


import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.PasswordTextField;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.CompoundPropertyModel;


public class Login extends WebPage {
private String email;
private String password;


public Login() {
add(new FeedbackPanel("errorMsg"));
Form form = new Form("loginForm", new CompoundPropertyModel(this)) {
protected void onSubmit() {
try {
User user = Users.getKnownUsers().getUser(email, password);
((MySession)getSession()).setLoggedInUser(user);
if(!continueToOriginalDestination()){
setResponsePage(ShowCatalog.class);
}
setResponsePage(ShowCatalog.class);
} catch (AuthenticationException e) {
error("Login failed. Try again.");
}
}
};
add(form);
form.add(new TextField("email"));
form.add(new PasswordTextField("password"));
}


public String getEmail() {
return email;
}


public void setEmail(String email) {
this.email = email;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}
}
/************** Login.java **************/


<!------------- Login.html ------------->
<html>
<h1>Login</h1>
<span wicket:id="errorMsg" />
<form wicket:id="loginForm">
<table border="0">
<tr>
<td>Email:</td>
<td><input type="text" wicket:id="email"></td>
</tr>
<tr>
<td>Password:</td>
<td><input type="password" wicket:id="password"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Login"></td>
</tr>
</table>
</form>
</html>
<!------------- Login.html ------------->


/************** ThankYou.java **************/
package cm269.lab4;


import org.apache.wicket.markup.html.WebPage;


public class ThankYou extends WebPage {
}
/************** ThankYou.java **************/


<!------------- ThankYou.html ------------->
<html>
Thank you for your order!
</html>
<!------------- ThankYou.html ------------->


/*********** Catalog.java *********/
package cm269.lab4;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class Catalog {
private List products;


public Catalog() {
products = new ArrayList();
products.add(new Product("p01", "Pencil", "a", 1.20));
products.add(new Product("p02", "Eraser", "b", 2.00));
products.add(new Product("p03", "Ball pen", "c", 3.50));
}


public List getProducts() {
return products;
}


public Product lookup(String id) {
for (Iterator iter = products.iterator(); iter.hasNext();) {
Product p = (Product) iter.next();
if (p.getId().equals(id)) {
return p;
}
}
return null;
}


public static Catalog globalCatalog = new Catalog();
}
/*********** Catalog.java *********/


/*********** AuthenticationException.java *********/
package cm269.lab4;


public class AuthenticationException extends RuntimeException {
}
/*********** AuthenticationException.java *********/


/*********** AuthenticatedPage.java *********/
package cm269.lab4;


import org.apache.wicket.markup.html.WebPage;


public class AuthenticatedPage extends WebPage {
}
/*********** AuthenticatedPage.java *********/


/************** Product.java **************/
package cm269.lab4;


import java.io.Serializable;


public class Product implements Serializable {
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 void setId(String id) {
this.id = id;
}


public String getName() {
return name;
}


public void setName(String name) {
this.name = name;
}


public String getDesc() {
return desc;
}


public void setDesc(String desc) {
this.desc = desc;
}


public double getPrice() {
return price;
}


public void setPrice(double price) {
this.price = price;
}
}
/************** Product.java **************/


/************** User.java **************/
package cm269.lab4;


import java.io.Serializable;


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;
}


public String getId() {
return id;
}


public void setId(String id) {
this.id = id;
}


public String getEmail() {
return email;
}


public void setEmail(String email) {
this.email = email;
}


public String getPassword() {
return password;
}


public void setPassword(String password) {
this.password = password;
}


public void setCreditCardNo(String creditCardNo) {
this.creditCardNo = creditCardNo;
}
}
/************** User.java **************/


/************** Users.java **************/
package cm269.lab4;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;


public class Users {
private List users;
private static Users knownUsers;


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 AuthenticationException();
}


public static Users getKnownUsers() {
if (knownUsers == null) {
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"));
}
return knownUsers;
}
}
/************** Users.java **************/


/************** MySession.java **************/
package cm269.lab4;


import java.util.ArrayList;
import java.util.List;


import org.apache.wicket.Application;
import org.apache.wicket.Request;
import org.apache.wicket.protocol.http.WebSession;


public class MySession extends WebSession {
private List cart;
private User loggedInUser;


public MySession(Application application, Request request) {
super(application, request);
cart = new ArrayList();
}


public List getCart() {
return cart;
}


public User getLoggedInUser() {
return loggedInUser;
}


public void setLoggedInUser(User loggedInUser) {
this.loggedInUser = loggedInUser;
}
}
/************** MySession.java **************/


項目結構如下圖所示:


參考資料:
http://wicket.apache.org/


沒有留言: