orderform.java

来自「struts的一些用的书籍」· Java 代码 · 共 104 行

JAVA
104
字号
package app13a.form;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import javax.servlet.http.HttpServletRequest;

public final class OrderForm extends ActionForm {
  
//	private static DecimalFormat decimalFormatter = new DecimalFormat("#####.##");
  private static SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");
  static {
    dateFormatter.setLenient(false);
  }

  private int productId;
  private String customer;
  private String quantity;
  private String price;
  private String orderDate;

  public String getCustomer() {
    return customer;
  }
  public void setCustomer(String customer) {
    this.customer = customer;
  }
  public String getOrderDate() {
    return orderDate;
  }
  public void setOrderDate(String orderDate) {
    this.orderDate = orderDate;
  }
  public String getPrice() {
    return price;
  }
  public void setPrice(String price) {
    this.price = price;
  }
  public int getProductId() {
    return productId;
  }
  public void setProductId(int productId) {
    this.productId = productId;
  }
  public String getQuantity() {
    return quantity;
  }
  public void setQuantity(String quantity) {
    this.quantity = quantity;
  }
  public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) {
    ActionErrors errors = new ActionErrors();
//
//    //Validate Customer
    if (customer.trim().equals("")) {
      errors.add("order", new ActionMessage("missing.customer"));
    }
    //Validate Quantity
    if (quantity.trim().equals("")) {
      errors.add("order", new ActionMessage("missing.quantity"));
    }
    else {
      try {
        Integer.parseInt(quantity);
      }
      catch (NumberFormatException e) {
        errors.add("order", new ActionMessage("invalid.quantity"));
      }
    }

    //Validate Price
    if (price.trim().equals("")) {
      errors.add("price", new ActionMessage("missing.price"));
    }
    else {
      try {
        Float.parseFloat(price);							
      }
      catch (NumberFormatException e) {
        errors.add("price", new ActionMessage("invalid.price"));
      }
    }

    //Validate OrderDate
    if (orderDate.trim().equals("")) {
      errors.add("order", new ActionMessage("missing.orderDate"));
    }
    else {
      // if lenient=true, dateFormatter.parse(String) will parse 32 December 2004 as 1 January 2005
      try {
        dateFormatter.parse(orderDate);
      }
      catch (ParseException e) {
        errors.add("order", new ActionMessage("invalid.orderDate"));
      }
    }
    return errors;
  }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?