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

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

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

    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 閱讀(1080) 評論(0)  編輯  收藏

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


    網站導航:
     
    主站蜘蛛池模板: 无码人妻一区二区三区免费看| 亚洲老熟女五十路老熟女bbw| 黄视频在线观看免费| 国产精品公开免费视频| 无码亚洲成a人在线观看| 性xxxx视频播放免费| 91在线亚洲综合在线| 24小时免费直播在线观看| 亚洲中文字幕无码爆乳app| 免费无码不卡视频在线观看| 亚洲色偷偷色噜噜狠狠99网| 日韩精品无码人妻免费视频| 精品亚洲国产成人av| 国产91精品一区二区麻豆亚洲| a视频免费在线观看| 亚洲人成在线播放网站岛国| 57pao国产成永久免费视频| 亚洲人成77777在线播放网站不卡| 成年女人18级毛片毛片免费观看| 无码天堂va亚洲va在线va| 久久久久亚洲AV无码专区网站| 182tv免费视频在线观看| 亚洲精品综合一二三区在线| 99久久久国产精品免费无卡顿| 亚洲熟妇自偷自拍另欧美| 亚洲高清成人一区二区三区| 四虎国产精品免费永久在线| 亚洲av无码片在线观看| 四虎影视永久免费观看网址| a级成人毛片免费图片| 亚洲国产超清无码专区| 四虎永久免费观看| 久久99精品国产免费观看| 亚洲人成色777777老人头| 丁香五月亚洲综合深深爱| 亚洲高清视频免费| 免费很黄无遮挡的视频毛片| 亚洲视频在线观看网址| 免费在线观看污网站| 亚洲免费观看网站| 一级做a爰片久久毛片免费看|