<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Joeyta備忘記

      BlogJava :: 首頁 :: 聯(lián)系 :: 聚合  :: 管理
      9 Posts :: 0 Stories :: 9 Comments :: 0 Trackbacks

    Wicket lab3 主要是實作 簡單的結帳系統(tǒng),
    使用靜態(tài)的 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 實作動態(tài) 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 *************/

    項目結構如下圖所示:


    執(zhí)行畫面如下圖所示:

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

    posted on 2007-10-08 22:13 joeyta 閱讀(1084) 評論(0)  編輯  收藏

    只有注冊用戶登錄后才能發(fā)表評論。


    網(wǎng)站導航:
     
    主站蜘蛛池模板: rh男男车车的车车免费网站| 无码成A毛片免费| 国产在线观看片a免费观看| 久久精品亚洲综合一品| 久久久国产精品福利免费| 成人在线免费看片| 91亚洲国产在人线播放午夜| 国产成人亚洲综合一区| 日韩大片在线永久免费观看网站 | 国产精品视频白浆免费视频| 毛片a级毛片免费观看免下载 | 国产成人精品无码免费看| 亚洲乱码国产一区三区| 国产在线精品免费aaa片| 亚洲AV综合色区无码一区爱AV| 久久99精品免费视频| 亚洲自偷自拍另类图片二区| 91在线视频免费播放| 亚洲国产成人精品无码区花野真一| 国产情侣久久久久aⅴ免费| 亚洲成AV人片在线播放无码| 亚洲乱亚洲乱妇24p| 2021在线永久免费视频| 国产成+人+综合+亚洲专| 国产成人精品免费视频大全五级| 亚洲黄色在线视频| 成年女人免费视频播放77777| 国产精品亚洲综合| 国产曰批免费视频播放免费s | 一级一级一片免费高清| 噼里啪啦电影在线观看免费高清| 亚洲丶国产丶欧美一区二区三区| 亚洲国产成人久久综合区| 久久精品成人免费看| 亚洲午夜成人精品无码色欲| 精品国产亚洲一区二区在线观看 | 国产精品免费一区二区三区| 亚洲视频在线观看一区| 日韩免费视频播放| 99精品免费视品| 亚洲人成网站999久久久综合|