星期四, 10月 11, 2007

Wicket lab5 備忘記

Wicket lab 5 為實作 簡單問答FAQ系統,
這裡使用了 AjaxLink 及 AjaxSubmitButton(AjaxButton) 元件,
實作了 ModelWindow 及 Panel, 並製作可重用的元件,
Wicket 提供了 WICKET AJAX DEBUG WINDOW, 這使用 ajax 的 debug 更方便.

執行畫面如下圖所示:





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

[1] 實作 lab 5:
<!------------- 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.lab5;

import org.apache.wicket.protocol.http.WebApplication;

public class MyApp extends WebApplication {

public Class getHomePage() {
return ListFAQ.class;
}

}
/************** MyApp.java **************/

/************** ListFAQ.java **************/
package cm269.lab5;

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

import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;

public class ListFAQ extends WebPage {
private static List questions;
private static List answers;

public ListFAQ() {
questions = new ArrayList();
questions.add("How to run Eclipse?");
questions.add("How to run NetBeans?");
questions.add("How to run Tomcat?");
answers = new ArrayList();
answers.add("Double click its icon.");
answers.add("Type netbeans at the command line.");
answers.add("Run startup.bat.");
ListView eachQuestion = new ListView("eachQuestion", questions) {
protected void populateItem(ListItem item) {
int idx = item.getIndex();
item.add(new Question("question", (String) questions.get(idx),
(String) answers.get(idx)));
}
};
add(eachQuestion);
}
}

/************** ListFAQ.java **************/

<!------------- ListFAQ.html --------------->
<html>
<body>
<div wicket:id="eachQuestion">
<div wicket:id="question"/>
</div>
</body>
</html>
<!------------- ListFAQ.html --------------->

/************** Question.java **************/
package cm269.lab5;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.AbstractReadOnlyModel;

public class Question extends Panel {

private Label answer;
private Rating r = new Rating();
private Label question;
private ModalWindow modal;
private ModalWindow modifyAnswerModal;
private NewAnswer newAnswer = new NewAnswer();

public Question(String id, final String questionText,
final String answerText) {
super(id);
AjaxLink link = new AjaxLink("link") {
public void onClick(AjaxRequestTarget target) {
answer.setVisible(!answer.isVisible());
target.addComponent(answer);
}
};
add(link);
question = new Label("question", new AbstractReadOnlyModel() {
public Object getObject() {
return questionText + " (" + r.getAverageRating() + ")";
}
});
question.setOutputMarkupId(true);
link.add(question);
answer = new Label("answer", new AbstractReadOnlyModel() {
public Object getObject() {
if(newAnswer.getNewAnswer() == null){
return answerText;
}
return newAnswer.getNewAnswer();
}
});
answer.setVisible(false);
answer.setOutputMarkupPlaceholderTag(true);
add(answer);
AjaxLink rate = new AjaxLink("rate") {
public void onClick(AjaxRequestTarget target) {
modal.show(target);
}
};
add(rate);
modal = new ModalWindow("modal");
modal.setInitialWidth(200);
modal.setInitialHeight(100);
add(modal);

GetRating getRating = new GetRating(modal.getContentId(), r, question, modal);
modal.setContent(getRating);

AjaxLink modifyAnswer = new AjaxLink("modifyAnswer") {
public void onClick(AjaxRequestTarget target) {
modifyAnswerModal.show(target);
}
};
add(modifyAnswer);
modifyAnswerModal = new ModalWindow("modifyAnswerModal");
modifyAnswerModal.setInitialWidth(400);
modifyAnswerModal.setInitialHeight(100);
add(modifyAnswerModal);

GetAnswer getAnswer = new GetAnswer(modifyAnswerModal.getContentId(), newAnswer, answer, modifyAnswerModal);
modifyAnswerModal.setContent(getAnswer);
}
}

}
/************** Question.java **************/

<!------------- Question.html ------------->
<html>
<wicket:panel>
<div>
<a wicket:id="link">
<span wicket:id="question">Q: abc</span>
</a>
<a wicket:id="rate">Rate</a>

<a wicket:id="modifyAnswer">Modify Answer</a>
<div wicket:id="modal"/>
</div>
<div wicket:id="answer">A: xyz</div>
<div wicket:id="modifyAnswerModal"/>
</wicket:panel>
</html>
<!------------- Question.html ------------->

/************** GetRating.java **************/
package cm269.lab5;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

public class GetRating extends Panel {
private int rating = 5;

public GetRating(String id, final Rating r , final Component toBeRegreshed, final ModalWindow modal) {
super(id);
Form f = new Form("f", new CompoundPropertyModel(this));
add(f);
f.add(new TextField("rating", Integer.class));
f.add(new AjaxSubmitButton("rate", f) {
protected void onSubmit(AjaxRequestTarget target, Form form) {
r.add(rating);
target.addComponent(toBeRegreshed);
modal.close(target);
}
});
}
}
/************** GetRating.java **************/

<!------------- GetRating.html ------------->
<html>
<wicket:panel>
<form wicket:id="f" style="float:right">
<input type="text" wicket:id="rating" size="2">
<input type="submit" wicket:id="rate" value="Rate">
</form>
</wicket:panel>
</html>
<!------------- GetRating.html ------------->

/************** GetAnswer.java **************/
package cm269.lab5;

import org.apache.wicket.Component;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.form.AjaxSubmitButton;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.CompoundPropertyModel;

public class GetAnswer extends Panel {
private String newAnswer;

public GetAnswer(String id, final NewAnswer n, final Component toBeRegreshed, final ModalWindow modal) {
super(id);
Form f = new Form("f", new CompoundPropertyModel(this));
add(f);
f.add(new TextField("newAnswer", String.class));
f.add(new AjaxSubmitButton("modify", f) {
protected void onSubmit(AjaxRequestTarget target, Form form) {
n.setNewAnswer(getNewAnswer());
target.addComponent(toBeRegreshed);
modal.close(target);
}
});
}

public String getNewAnswer() {
return newAnswer;
}

public void setNewAnswer(String newAnswer) {
this.newAnswer = newAnswer;
}
}
/************** GetAnswer.java **************/

<!------------- GetAnswer.html ------------->
<html>
<wicket:panel>
<form wicket:id="f" style="float:left">
<table border="0" align="left">
<tr>
<td align="left">
New Answer:<input type="text" wicket:id="newAnswer" size="30">
</td>
</tr>
<tr>
<td align="left">
<input type="submit" wicket:id="modify" value="Modify">
</td>
</tr>
</table>
</form>
</wicket:panel>
</html>
<!------------- GetAnswer.html ------------->

/************** Rating.java **************/
package cm269.lab5;

import java.io.Serializable;

public class Rating implements Serializable {
private int totalRating = 0;
private int noRatings = 0;

public int getAverageRating() {
return noRatings == 0 ? 0 : totalRating / noRatings;
}

public void add(int rating) {
totalRating += rating;
noRatings++;
}
}
/************** Rating.java **************/

/************** NewAnswer.java **************/
package cm269.lab5;

import java.io.Serializable;

public class NewAnswer implements Serializable{
private String newAnswer;

public String getNewAnswer() {
return newAnswer;
}

public void setNewAnswer(String newAnswer) {
this.newAnswer = newAnswer;
}
}
/************** NewAnswer.java **************/

項目結構如下圖所示:


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

星期二, 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/


星期一, 10月 08, 2007

Wicket lab3 備忘記

Wicket lab3 主要是實作 簡單的結帳系統,
使用靜態的 map 模擬資料庫存取動作,

這裡並沒有使用 PropertyModel,
而使用更簡單的 CompoundPropertyModel(自動對應 form 及 pojo 的 property),
並在 TextField 裡使用 built-in validators,
以及使用 properties file 自定 wicket 的錯誤訊息,

在 validation 方便, 實作了 AbstractValidator 及 AbstractFormValidator 介面.
實作 AbstractValidator 主要對單個 form property 作自定 validation.
而 AbstractFormValidator 則可對多個 form property 作自定 validation.

利用 WebMarkupContainer 實作動態 css 錯誤顯示.

開始備忘記:

[1] Requirement in lab3
[2] 實作 lab3

[1] Requirement in lab3:
– Lab3 can be accessed by http://127.0.0.1:8080/CM269/lab3/
– There are 2 build-in Tables in the system for lookup

Customer Table
Customer ID Deposit %
c1 10
c2 15
c3 20

Item Table
Item Code Price
a1 100
a2 120
a3 150

– The first page of Lab3 is an Html Input Form
(The words in blue color indicates user input fields)
Order Input Form
Customer Id c1
Item Code a1
Quantity 10
Deposit 150
『Submit』
– On validation of the Input Values
1. Customer Id : must input, must exist in the Customer Table.
2. Item Code : must input, must exist in the Item Table.
3. Quantity : must >0 and <= 100
4. Deposit : must > 0
5. Whole Form Validation : deposit >= (quantity * price) * depositPercent / 100
(price is the got from Item Table, depositPrecent is got from Customer Table)
– If the inputs can pass all the validation rules, a Simple result page is shown.
(1000, 150, 850 calculated from the Demo Inputs)
Your orde is accepted, thank you !
Total Amount = 1000, Deposit = 150, Remaining Balance = 850.

[2] 實作 lab3:

<!----------- 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>


</web-app>

<!----------- web.xml --------------------->



/************** MyApp.java *************/

package cm269.lab3;



import org.apache.wicket.protocol.http.WebApplication;



public class MyApp extends WebApplication {



public Class getHomePage() {

return Lab3.class;

}



}

/************** MyApp.java *************/



/************** Lab3.java *************/

package cm269.lab3;



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

import org.apache.wicket.markup.html.form.Form;

import org.apache.wicket.markup.html.form.TextField;

import org.apache.wicket.markup.html.panel.FeedbackPanel;

import org.apache.wicket.model.CompoundPropertyModel;

import org.apache.wicket.validation.validator.NumberValidator;



public class Lab3 extends WebPage {

private Order order = new Order("","",0,0);



public Lab3() {

add(new FeedbackPanel("feedback"));

Form form = new Form("form", new CompoundPropertyModel(order)){

protected void onSubmit() {

setResponsePage(new Lab3Result(order.getTotalAmount(), order.getDeposit()));

}

};



TextField customerId = new TextField("customerId");

customerId.setRequired(true);

customerId.add(new CustomerIdValidator());

form.add(customerId);

FeedbackLabel customerIdLabel = new FeedbackLabel("customerIdLabel",customerId);

form.add(customerIdLabel);



TextField itemCode = new TextField("itemCode");

itemCode.setRequired(true);

itemCode.add(new ItemCodeValidator());

form.add(itemCode);

FeedbackLabel itemCodeLabel = new FeedbackLabel("itemCodeLabel",itemCode);

form.add(itemCodeLabel);



TextField quantity = new TextField("quantity",Integer.class);

quantity.setRequired(true);

quantity.add(NumberValidator.minimum(1));

quantity.add(NumberValidator.maximum(100));

form.add(quantity);

FeedbackLabel quantityLabel = new FeedbackLabel("quantityLabel",quantity);

form.add(quantityLabel);



TextField deposit = new TextField("deposit",Integer.class);

deposit.setRequired(true);

deposit.add(NumberValidator.minimum(1));

form.add(deposit);

FeedbackLabel depositLabel = new FeedbackLabel("depositLabel",deposit);

form.add(depositLabel);



form.add(new LightValidator(customerId,itemCode,quantity,deposit));



add(form);

}



}

/************** Lab3.java *************/



<!----------- Lab3.html --------------------->

<html>

<head>

<title>Lab3</title>

<style type="text/css">

li.feedbackPanelERROR {

color: red;

font-weight: bold;

}

td.invalidField {

color: red;

font-weight: bold;

}

</style>

</head>

<body>



<span wicket:id="feedback" />



<form wicket:id="form">

<table border>

<tr><th colspan="2">Order Input Form</th></tr>

<tr>

<td wicket:id="customerIdLabel">Customer Id</td>

<td>

<input type="text" wicket:id="customerId" />

</td>

</tr>

<tr>

<td wicket:id="itemCodeLabel">Item Code</td>

<td>

<input type="text" wicket:id="itemCode" />

</td>

</tr>

<tr>

<td wicket:id="quantityLabel">Quantity</td>

<td>

<input type="text" wicket:id="quantity" />

</td>

</tr>

<tr>

<td wicket:id="depositLabel">Deposit</td>

<td>

<input type="text" wicket:id="deposit" />

</td>

</tr>

<tr>

<td colspan="2">

<input type="submit" value="Submit" />

</td>

</tr>

</table>

</form>



</body>

</html>

<!----------- Lab3.html --------------------->



############### Lab3.properties #############

form.customerId.CustomerIdValidator=Could not find customer: ${input}

form.itemCode.ItemCodeValidator=Could not find item: ${input}

form.quantity.NumberValidator.minimum=${label} must >= ${minimum}

form.quantity.NumberValidator.maximum=${label} must <= ${maximum}

form.deposit.NumberValidator.minimum=${label} must >= ${minimum}

form.deposit.LightValidator=${label3} must >= (${label2} * price of ${label1}) * deposit percent of ${label0} / 100

############### Lab3.properties #############



/************** Lab3Result.java *************/


package cm269.lab3;



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

import org.apache.wicket.markup.html.basic.Label;



public class Lab3Result extends WebPage {



public Lab3Result(int totalAmount, int deposit) {

int remainBalance = totalAmount - deposit;

add(new Label("totalAmount",String.valueOf(totalAmount)));

add(new Label("deposit",String.valueOf(deposit)));

add(new Label("remainBalance",String.valueOf(remainBalance)));

}



}

/************** Lab3Result.java *************/



<!----------- Lab3Result.html ---------------->

<html>

<head>

<title>Lab3Result</title>

</head>

<body>



<table border="0">

<tr>

<td>Your order is accepted,thank you!</td>

</tr>

<tr>

<td>

Total Amount = <span wicket:id="totalAmount"></span>,

Deposit = <span wicket:id="deposit"></span>,

Remaining Balance = <span wicket:id="remainBalance"></span>.

</td>

</tr>



</table>



</body>

</html>

<!----------- Lab3Result.html ---------------->



/************** Order.java *************/

package cm269.lab3;



import java.io.Serializable;

import java.util.HashMap;

import java.util.Map;



public class Order implements Serializable{

private String customerId;

private String itemCode;

private int quantity;

private int deposit;

private static Map customerTable;

private static Map itemTable;



static{

customerTable = new HashMap();

customerTable.put("c1", new Integer(10));

customerTable.put("c2", new Integer(15));

customerTable.put("c3", new Integer(20));



itemTable = new HashMap();

itemTable.put("a1", new Integer(100));

itemTable.put("a2", new Integer(120));

itemTable.put("a3", new Integer(150));

}



public Order(String customerId, String itemCode, int quantity, int deposit) {

this.customerId = customerId;

this.itemCode = itemCode;

this.quantity = quantity;

this.deposit = deposit;

}



public int getTotalAmount(){

int price = ((Integer)getItemTable().get(itemCode)).intValue();

return quantity * price;

}



public static Map getCustomerTable() {

return customerTable;

}



public static Map getItemTable() {

return itemTable;

}



public static boolean isExistInCustomerTable(String customerId){

return customerTable.containsKey(customerId);

}



public static boolean isExistInItemTable(String itemCode){

return itemTable.containsKey(itemCode);

}



public String getCustomerId() {

return customerId;

}



public void setCustomerId(String customerId) {

this.customerId = customerId;

}



public String getItemCode() {

return itemCode;

}



public void setItemCode(String itemCode) {

this.itemCode = itemCode;

}



public int getQuantity() {

return quantity;

}



public void setQuantity(int quantity) {

this.quantity = quantity;

}



public int getDeposit() {

return deposit;

}



public void setDeposit(int deposit) {

this.deposit = deposit;

}



}

/************** Order.java *************/



/************** FeedbackLabel.java *************/

package cm269.lab3;



import org.apache.wicket.markup.ComponentTag;

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

import org.apache.wicket.markup.html.form.FormComponent;



public class FeedbackLabel extends WebMarkupContainer {

private FormComponent subject;



public FeedbackLabel(String id, FormComponent subject) {

super(id);

this.subject = subject;

}



protected void onComponentTag(ComponentTag tag){

if(!subject.isValid()){

tag.put("class", "invalidField");

}

super.onComponentTag(tag);

}



}

/************** FeedbackLabel.java *************/



/************** CustomerIdValidator.java *************/

package cm269.lab3;



import org.apache.wicket.validation.IValidatable;

import org.apache.wicket.validation.validator.AbstractValidator;



public class CustomerIdValidator extends AbstractValidator {



protected void onValidate(IValidatable validatable) {

if(!Order.isExistInCustomerTable((String)validatable.getValue())){

error(validatable);

}

}



}

/************** CustomerIdValidator.java *************/



/************** ItemCodeValidator.java *************/

package cm269.lab3;



import org.apache.wicket.validation.IValidatable;

import org.apache.wicket.validation.validator.AbstractValidator;



public class ItemCodeValidator extends AbstractValidator {



protected void onValidate(IValidatable validatable) {

if(!Order.isExistInItemTable((String)validatable.getValue())){

error(validatable);

}

}



}

/************** ItemCodeValidator.java *************/



/************** LightValidator.java *************/

package cm269.lab3;



import org.apache.wicket.markup.html.form.Form;

import org.apache.wicket.markup.html.form.FormComponent;

import org.apache.wicket.markup.html.form.TextField;

import org.apache.wicket.markup.html.form.validation.AbstractFormValidator;



public class LightValidator extends AbstractFormValidator {

private TextField customerId;

private TextField itemCode;

private TextField quantity;

private TextField deposit;



public LightValidator(TextField customerId, TextField itemCode,

TextField quantity, TextField deposit) {

super();

this.customerId = customerId;

this.itemCode = itemCode;

this.quantity = quantity;

this.deposit = deposit;

}



public FormComponent[] getDependentFormComponents() {

return new FormComponent[]{customerId, itemCode, quantity, deposit};

}



public void validate(Form form) {

int iDepositPercentage = ((Integer)Order.getCustomerTable().get(customerId.getConvertedInput())).intValue();

int iPrice = ((Integer)Order.getItemTable().get(itemCode.getConvertedInput())).intValue();

int iQuantity = ((Integer)this.quantity.getConvertedInput()).intValue();

int iDeposit = ((Integer)this.deposit.getConvertedInput()).intValue();

if(!(iDeposit >= (iQuantity * iPrice) * iDepositPercentage / 100)){

error(this.deposit);

}

}



}

/************** LightValidator.java *************/



項目結構如下圖所示:





執行畫面如下圖所示:



參考資料:

http://wicket.apache.org/

星期日, 10月 07, 2007

PDF 應用 備忘記

PDFCreator 及 CutePDF Writer 以列印功能般輸出 PDF.
PDFCreator:
http://sourceforge.net/projects/pdfcreator/

CutePDF Writer:
http://www.cutepdf.com/

Free PDF Compressor 壓縮 PDF:
http://www.nicepdf.com/products.html

PDF Watermark Creator 在 PDF 裡加入水印:
http://www.coolpdf.com/pdfwatermark.html

PDFHelper 將 PDF 文件分拆:
http://rptea.com/

PDF Bundle 在 PDF 裡加入其他檔案(如 Word):
http://www.coolpdf.com/products.html

Infix Pro 修改 PDF檔案:
http://www.iceni.com/

Superior Search 能夠在大量的 PDF 文件中搜尋訊息:
http://www.superiorsearch.com/en/products/

可以利用 FillOutAForm 及 Ghostscript 將 PDF form 轉成 BMP後填上資料.
FillOutAForm:
http://jdmcox.com/

Ghostscript:
http://www.ghostscript.com/awki

Easy PDF to Text Converter 將 PDF 檔案轉成文字, html, word 檔.
http://www.pdf-to-html-word.com/pdf-to-text/


PeaZip 為綠色壓縮軟件不用安裝, 適合放在 USB 裡使用.
http://peazip.sourceforge.net/
可建立: 7Z, BZ2, GZ, PAQ.LPAQ, PEA, QUAD, TAR, UPX, ZIP
可開啟: ACE, ARJ, CAB, DEB, ISO, LHA, RAR, RPM

7ZIP 免費的壓縮軟件.
http://www.7-zip.org/

星期二, 9月 18, 2007

Wicket lab2 備忘記

Wicket Lab2 要求使用 TextField, DropDownChoice 及 Date picker 實作頁面跳轉的效果.


開始備忘記:
[1] Lab2 要求
[2] 實作 Lab2


[1] Lab2 要求:
本次要求如下圖所示,
當介面選擇 Male 後, 跳至 male 的訊息,
當介面選擇 Female 後, 跳至 female 的訊息,



[2] 實作 Lab2
<!----------- 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>WicketLab1First</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>WicketLab1First</filter-name>
<url-pattern>/lab1/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>WicketLab2First</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>WicketLab2First</filter-name>
<url-pattern>/lab2/*</url-pattern>
</filter-mapping>


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



############### Lab2.properties ###############
f.sex.null=-Pls Select-
f.birth.IConverter.Date=You must input a date.
############### Lab2.properties ###############


這裡的 f.sex.null 表示 html 頁裡 "f" form 的 sex element,
表示 DropDownChoice 預設為 -Pls Select-
如果 key/value 設成 null=-Pls Select- 則對所有 DropDownChoice 起作用.



/************* Lab2.java ****************/
package cm269.lab2;


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


import org.apache.wicket.extensions.yui.calendar.DatePicker;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.panel.FeedbackPanel;
import org.apache.wicket.model.Model;


public class Lab2 extends WebPage {


private static final long serialVersionUID = 381689366528324660L;


private Model nameModel;
private Model sexModel;
private Model birthModel;

public Lab2() {
FeedbackPanel feedback = new FeedbackPanel("msgs");
add(feedback);
Form form = new Form("f"){
private static final long serialVersionUID = -8897899191585355738L;


protected void onSubmit() {
String sName = (String)nameModel.getObject();
String sSex = (String)sexModel.getObject();
Date dBirth = (Date)birthModel.getObject();

if(sSex.equals("Male")){
AbstractResult result = new Lab2ResultA(sName,dBirth);
setResponsePage(result); // 這裡表示按 submit 後, 跳至 Lab2ResultA 頁面
} else if(sSex.equals("Female")){
AbstractResult result = new Lab2ResultB(sName,dBirth);
setResponsePage(result); // 這裡表示按 submit 後, 跳至 Lab2ResultB 頁面
}


}
};

nameModel = new Model();
TextField name = new TextField("name", nameModel, String.class);
name.setRequired(true);
form.add(name);

sexModel = new Model();
List sexes = new ArrayList();
sexes.add("Male");
sexes.add("Female");
DropDownChoice sex = new DropDownChoice("sex", sexModel, sexes);
sex.setRequired(true);
form.add(sex);

birthModel = new Model();
TextField birth = new TextField("birth", birthModel, Date.class);
// TextField birth = new TextField("birth", birthModel, Date.class){
// private static final long serialVersionUID = 1441781496720134137L;
// public IConverter getConverter(Class type){
// return type == Date.class ? new DateConverter():null;
// }
// };
birth.setRequired(true);
birth.add(new DatePicker());
form.add(birth);

add(form);
}

}
/************* Lab2.java ****************/



<!----------- Lab2.html ----------------->
<html>
<head>
<title>Lab2</title>
</head>
<body>

<span wicket:id="msgs" />
<form wicket:id="f">
<table border>
<tr><th colspan="2">CM269 Lab2</th></tr>
<tr>
<td>Name</td>
<td>
<input type="text" wicket:id="name" />
</td>
</tr>
<tr>
<td>Sex</td>
<td>
<select wicket:id="sex">
<option>Male</option>
<option>Female</option>
</select>
</td>
</tr>
<tr>
<td>Date of Birth</td>
<td>
<input type="text" wicket:id="birth" />
</td>
</tr>
<tr>
<td colspan="2">
<input type="submit" value="OK" />
</td>
</tr>
</table>
</form>

</body>
</html>
<!----------- Lab2.html ----------------->



/************* AbstractResult.java ****************/
package cm269.lab2;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


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


public class AbstractResult extends WebPage{


private static final long serialVersionUID = 3148730572565560427L;


protected String name;

protected Date birth;

public void doResult() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");

add(new Label("name",getName()));
add(new Label("birth",dateFormat.format(getBirth())));
}


public String getName() {
return name;
}


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


public Date getBirth() {
return birth;
}


public void setBirth(Date birth) {
this.birth = birth;
}


}
/************* AbstractResult.java ****************/



/************* Lab2ResultA.java ****************/
package cm269.lab2;


import java.util.Date;


public class Lab2ResultA extends AbstractResult {


private static final long serialVersionUID = -1230297067586624251L;


public Lab2ResultA(String sName, Date dBirth) {
setName(sName);
setBirth(dBirth);
doResult();
}

}
/************* Lab2ResultA.java ****************/



<!----------- Lab2ResultA.html ----------------->
<html>
<head>
<title>Lab2ResultA</title>
</head>
<body>

<table border="0">
<tr>
<td colspan="2">Dear Sir,</td>
</tr>
<tr>
<td width="20">&nbsp;</td>
<td>Your Registration is accepted.</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Name:<span wicket:id="name">joeyta</span></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Date of Birth:<span wicket:id="birth">1980/01/01</span></td>
</tr>

</table>

</body>
</html>
<!----------- Lab2ResultA.html ----------------->



/************* Lab2ResultB.java ****************/
package cm269.lab2;


import java.util.Date;


public class Lab2ResultB extends AbstractResult {


private static final long serialVersionUID = -5461297645709489576L;


public Lab2ResultB(String sName, Date dBirth) {
setName(sName);
setBirth(dBirth);
doResult();
}

}
/************* Lab2ResultB.java ****************/



<!----------- Lab2ResultB.html ----------------->
<html>
<head>
<title>Lab2ResultB</title>
</head>
<body>

<table border="0">
<tr>
<td colspan="2">Dear Miss,</td>
</tr>
<tr>
<td width="20">&nbsp;</td>
<td>Your Registration is accepted.</td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Name:<span wicket:id="name">joeyta</span></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>Date of Birth:<span wicket:id="birth">1980/01/01</span></td>
</tr>

</table>

</body>
</html>
<!----------- Lab2ResultB.html ----------------->



項目結構如下圖所示:



參考資料:

http://wicket.apache.org/



星期五, 9月 07, 2007

Wicket 備忘記

Wicket framkework 使用邏輯及設計頁面分離概念,
不但使元件更能重用, 而且網頁設計師及程式員可以同時工作.
網頁設計師使用網頁編輯器設計網頁, 程式員則在頁面裡插入 Wicket 特有的 html attribute.
這方面與 Tapestry 很相似.

開始備忘記:
[1]
安裝及準備
[2] 建立第一個 Wicket Project


[1] 安裝及準備:
下載 jdk-6u2-windows-i586-p.exe
http://java.sun.com/javase/downloads/index.jsp
雙擊 jdk-6u2-windows-i586-p.exe 安裝至 C:\jdk1.6.0_02\
將 JAVA_HOME=C:\jdk1.6.0_02 加入至環境變數中


下載 apache-tomcat-6.0.14.zip
http://tomcat.apache.org/download-60.cgi
解壓縮至 C:/apache-tomcat-6.0.14


下載 apache-wicket-1.3.0-beta3.zip
http://www.apache.org/dyn/closer.cgi/wicket/1.3.0-beta3/
解壓縮至 C:\apache-wicket-1.3.0-beta3


然後再解至縮 C:\apache-wicket-1.3.0-beta3\wicket-examples-1.3.0-beta3.war
至 C:\apache-wicket-1.3.0-beta3\lib\wicket-examples-1.3.0-beta3


下載 eclipse-java-europa-win32.zip
http://www.eclipse.org/downloads/
解壓縮至 C:\eclipse-java-europa-win32
雙擊 C:\eclipse-java-europa-win32\eclipse\eclipse.exe 啟動 Eclipse


下載 wtp-all-in-one-sdk-R-2.0-200706260303-win32.zip
http://download.eclipse.org/webtools/downloads/drops/R2.0/R-2.0-200706260303/
解壓縮至 C:\wtp-all-in-one-sdk-R-2.0-200706260303-win32
雙擊 C:\wtp-all-in-one-sdk-R-2.0-200706260303-win32\eclipse\eclipse.exe 啟動 Eclipse


[2] 建立第一個 Wicket Project:
Eclipse 加入 Tomcat 為 Server:
Eclipse:Window -> Show View -> Other ->> Server -> Servers
右鍵點選 Server -> New -> Server ->> Apache -> Tomcat v6.0 Server
Tomcat installation directory 選擇 C:\apache-tomcat-6.0.14
然後按 Finish


Eclipse 建立 FirstWicket project:
Eclipse:File -> New -> Other ->> Web -> Dynamic Web Project
Project name: FirstWicket
然後按 Finish


將 C:\apache-wicket-1.3.0-beta3\lib\wicket-examples-1.3.0-beta3\WEB-INF\lib\*.jar
複製至 FirstWicket/WebContent/WEB-INF/lib


右鍵點擊 FirstWicket -> Run As -> Run on Server
選擇 Tomcat v6.0 Server at localhost -> Next -> Finish


輸入網址 http://localhost:8080/FirstWicket/lab1/
出現如下圖所示:


/**************** Lab1.java *******************/
package cm269.lab1;


import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;


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


public class Lab1 extends WebPage {


private static final long serialVersionUID = 1L;


public Lab1() {
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
DateFormat timeFormat = new SimpleDateFormat("hh:mm:ss");

add(new Label("date",dateFormat.format(new Date())));
add(new Label("time",timeFormat.format(new Date())));
add(new Label("owner","Joeyta Chan"));
}


}
/**************** Lab1.java *******************/
在 Web Page 裡定義 3 個 Label 為 date, time, owner


<!--------------- Lab1.html -------------->
<html>
<head>
<title>FirstWicket</title>
</head>
<body>

<table border="1">
<tr>
<th colspan="2">CM269 Lab1</th>
</tr>
<tr>
<td>Current Date</td>
<td><span wicket:id="date">date</span></td>
</tr>
<tr>
<td>Current Time</td>
<td wicket:id="time"><span>time</span></td>
</tr>
<tr>
<td>Done By</td>
<td wicket:id="owner"><span>owner</span></td>
</tr>

</table>

</body>
</html>
<!--------------- Lab1.html -------------->
可以使用 html attribute wicket:id 將 web page 裡的 label 呼叫出來.


/**************** FirstWicket.java *******************/
package cm269.lab1;


import org.apache.wicket.protocol.http.WebApplication;


public class FirstWicket extends WebApplication {


public Class getHomePage() {
return Lab1.class;
}


}
/**************** FirstWicket.java *******************/
這裡返回 Lab1 生成後的網頁.


<!--------------- web.html -------------->
<?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>FirstWicket</display-name>

<servlet>
<servlet-name>FirstWicket</servlet-name>
<servlet-class>org.apache.wicket.protocol.http.WicketServlet</servlet-class>
<init-param>
<param-name>applicationClassName</param-name>
<param-value>cm269.lab1.FirstWicket</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>FirstWicket</servlet-name>
<url-pattern>/lab1/*</url-pattern>
</servlet-mapping>


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


項目結構如下圖所示:


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

星期六, 8月 25, 2007

Ubuntu (Virtual PC) 備忘記

自從 Virtual PC 提供免費下載後, 已很久沒有使用 VMware 了,
但在 Virtual PC 裡安裝 Ubuntu 往往會遇到一些問題,
如 24 bits color 下的 Ubuntu 安裝 GUI 與 VPC 只提供 16 及 32 bits color 的不相容,
及滑鼠 在 Ubuntu KDE 裡 不能啟動的問題,

本備忘記首先記錄如何下載及安裝免費的 Virtual PC 2007,
然後下載 Ubuntu 的影像檔並製成光碟,
最後在 Virtual PC 2007 裡安裝 Ubuntu 並解決過程中遇到的問題.


開始備忘記:
[1] 安裝 Virtual PC 2007
[2] 製作 Ubuntu 7.04 Live 及 Install CD
[3] 安裝 Ubuntu

[1] 安裝 Virtual PC 2007:
下載 Virtual PC 2007:
http://www.microsoft.com/downloads/details.aspx?FamilyId=04D26402-3199-48A3-AFA2-2DC0B40A73B6&displaylang=en
選擇 32 BIT\setup.exe 下載, 然後雙擊 setup.exe 安裝

安裝後 執行 virtual PC 2007
開始 -> 所有程式 -> Microsoft Virtual PC
Virtual PC console -> New -> Next -> Create a virtual machine
Name and location:Ubuntu_7.04 -> Next
Operation System:Other -> Next
Ajusting the RAM -> 256m -> Next
A new virtual hard disk -> Next -> Next -> Finish

[2] 製作 Ubuntu 7.04 Live 及 Install CD:
下載 ubuntu-7.04-desktop-i386.iso 影像檔 :
http://www.ubuntu.com/getubuntu/download

下載後使用 Nero 將 ubuntu-7.04-desktop-i386.iso 影像檔 製作成 安裝 CD 光碟.
Nero -> 製作與複製 -> 燒錄影像檔
然後選擇 ubuntu-7.04-desktop-i386.iso

將燒錄後的 CD 放進 CD-ROM 準備 boot installation

[3] 安裝 Ubuntu:
在 Virutal PC Console 點選 Ubuntu_7.04 -> start
當啟動 Ubuntu_7.04 virtual pc 後,
CD -> Use Phisical Drive F:
再按 Action -> Reset

開始進入安裝畫面,
由於 virtual pc 只支援 1632 bits color,
而 Ubuntu 7.04 是 24 bits color ,
因此需要按 F4 選擇 800 X 600 X 16

並選擇第二項 Start Ubuntu in safe graphics mode,
不要選擇第一項,因為按 Start or install Ubuntu 會出現怪畫面.

如下圖所示


然後按 Enter 進入 Ubuntu Live CD 的 GUI 畫面,
由於 Ubuntu 7.04 kernel 2.6.20 的 bug 所致,
故 detect 不到 Virtual Pc ps/2 的滑鼠

解決方法:
(左鍵 Alt) + F1 進入 Ubuntu GUI 工作列
選擇 System -> Preferences -> Accessibility -> Keyboard Accessibility

如下圖所示


進入 Keyboard Accessibility Preferences -> Basic 面版
Space 鍵 選擇 Enable keyboard accessibility features

如下圖所示


然後按 Tab 及 兩下 "->" 鍵至 Mouse Keys 面版
再按 TabSpace 選擇 Enable Mouse Keys
及按 Close

如下圖所示


以上做法是為了能使用 右側數字鍵盤 來控制滑鼠
首先關閉 Num Lock
4 代表向
6 代表向
8 代表向
2 代表向
4 代表 click
Enter 代表 double click

然後按 桌面上的 Install 進行 Ubuntu 安裝

如下圖所示


選擇 語言後按 (左鍵 Alt) + F [下一頁]
一直按 [下一頁] 至 "Who are you?" 頁面 建立 帳號 及 密碼
登入名稱為 ubuntu
密碼亦為 ubuntu

如下圖所示


繼續按 [下一頁] 並按 Install 進行安裝

如下圖所示


可能是只分配了 256m Memory 的關係, 大概安裝了五十多分鐘.
安裝完成後按 Restart now

當重新啟動時, 按 Esc 進行 Grub 選項
然後按 "e", 選擇至 "kernel ..." 再按 "e" 進行編輯
在 kernel parameter 欄最後 加入 i8042.noloop

如下圖所示


然後按 Enter 再按 "b" 進行 boot 機.
以上做法是為了啟動滑鼠功能

boot 完後, 已可以使用滑鼠功能

如下圖所示


輸入
Username: ubuntu
Password: ubuntu
登入
此時亦可以按 (右鍵 Alt) 跳至 windows 視窗

由於剛才只在 boot 機時 加入了 Grub 的 kernel parameter
當 重新開機後, 這些 Grub 裡的 kernel parameter 會回復原狀
所以在 Ubuntu GUI 的工作列裡 選擇
Application -> 附屬應用程式 -> 終端機

輸入 sudo vi /boot/grub/menu.lst 指令進行修改 grub 設定
密碼為 ubuntu

如下圖所示


vi 進入 /boot/grub/menu.lst 後找到 kernel 的位置
然後按 "A" 後在 行尾處加入 i8042.noloop
然後按 Esc
再按 ":""q", "w"
Enter 修改完成
然後重新啟動 Ubuntu 就可以永久使用滑鼠功能

如果使用 static IP, 登入後可以進行網路設定
在 Ubuntu GUI 的工作列裡 選擇
System -> 管理 -> 網路
進入 Network Settings 面版後
選擇 Wired Connection 按 Properties
Configuration 選擇 Static IP address
IP address: 192.168.1.123
Subnet mask: 255.255.255.0
Gateway address 192.168.1.1

如下圖所示


重新啟動後就可以使用 firefox 瀏覽網頁了

如下圖所示


參考資料:
http://www.ubuntu.com/getubuntu/download

http://wiki.ubuntu.org.tw/index.php/Ubuntu7.04Install

http://www.ubuntu.org.tw/

http://www.ubuntu.org.cn/

http://hi.baidu.com/uroot/blog/item/9ea05f103fd180fec3ce7995.html