⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 orderform.java

📁 struts的一些用的书籍
💻 JAVA
字号:
package app19a.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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -