Joeyta備忘記

  BlogJava :: 首页 :: 联系 :: 聚合  :: 管理
  9 Posts :: 0 Stories :: 9 Comments :: 0 Trackbacks

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/

posted on 2007-10-08 22:13 joeyta 阅读(1075) 评论(0)  编辑  收藏

只有注册用户登录后才能发表评论。


网站导航: