軟件三層設計入門-表單校驗,購物車

1.搭建開發環境
        1.1 導入項目所需的開發包
                dom4j-1.6.1.jar
                jaxen-1.1-beta-6.jar
                commons-beanutils-1.8.0.jar
                commons-logging.jar
                jstl.jar
                standard.jar

         1.2 創建程序的包名
                cn.itcast.domain
                cn.itcast.dao
                cn.itcast.dao.impl
                cn.itcast.service
                cn.itcast.service.impl
                cn.itcast.web.controller  
                cn.itcast.web.UI  (user interface)(放爲用戶提供用戶界面的servlet)
                cn.itcast.utils
                junit.test

                在web-inf\jsp目錄,保存jsp頁面

          1.3 在類目錄下面,創建用於保存用戶數據的xml文件(users.xml)


2、開發實體user
        private String id;
        private String username;
        private String password;
        private String email;
        private Date birthday;

3、開發dao
       3.1  開發UserDaoXmlImpl
            public void add(User user)
            public User find(String username)
            public User find(String username,String password)

       3.2  抽取接口

       3.3  開發工具類: XmlUtils 
       3.4  開發測試類


4、開發service(service 對web層提供所有的業務服務)
        4.1  開發BusinessService
  public void registerUser(User user) throws UserExistException
  public User loginUser(String username,String password);
 
 
 5、開發web層
  5.1 開發註冊
  5.1.1  寫一個RegisterUIServlet爲用戶提供註冊界面,它收到請求,跳到register.jsp
  5.1.2  寫register.jsp
  5.1.3  register.jsp提交請求,交給RegisterServlet處理

  5.1.4  寫RegisterServlet

  1.設計用於校驗表單數據RegisterFormbean 
  2、寫WebUtils工具類,封裝請求數據到formbean中
  3、如果校驗失敗跳回到register.jsp,並回顯錯誤信息
  4、如果校驗通過,調用service向數據庫中註冊用戶
 
  5.2 開發登陸
  5.2.1   寫一個LoginUIServlet爲用戶提供註冊界面,它收到請求,跳到login.jsp

  5.2.2   login.jsp提交給LoginServlet處理登陸請求


校驗:

/*
private String username;  用戶名不能爲空,並且要是3-8的字符 abcdABcd
private String password;  密碼不能爲空,並且要是3-8的數字
private String password2; 兩次密碼要一致
private String email;     可以爲空,不爲空要是一個合法的郵箱
private String birthday;  可以爲空,不爲空時,要是一個合法的日期
 * 
 */
public boolean validate(){
	
	boolean isOk = true;
	
	if(this.username==null || this.username.trim().equals("") ){
		isOk = false;
		errors.put("username", "用戶名不能爲空!!");
	}else{
		if(!this.username.matches("[a-zA-Z]{3,8}")){
			isOk = false;
			errors.put("username", "用戶名必須是3-8位的字母!!");
		}
	}
	
	
	if(this.password==null || this.password.trim().equals("")){
		isOk = false;
		errors.put("password", "密碼不能爲空!!");
	}else{
		if(!this.password.matches("\\d{3,8}")){
			isOk = false;
			errors.put("password", "密碼必須是3-8位的數字!!");
		}
	}
	
	//private String password2; 兩次密碼要一致
	if(this.password2!=null){
		if(!this.password2.equals(this.password)){
			isOk = false;
			errors.put("password2", "兩次密碼不一致!!");
		}
	}
	
	//private String email;     可以爲空,不爲空要是一個合法的郵箱
	// [email protected]
	if(this.email!=null){
		if(!this.email.matches("\\w+@\\w+(\\.\\w+)+")){
			isOk = false;
			errors.put("email", "郵箱不是一個合法郵箱!!");
		}
	}
	
	
	//private String birthday;  可以爲空,不爲空時,要是一個合法的日期
	if(this.birthday!=null){
		try{
			DateLocaleConverter conver = new DateLocaleConverter();
			conver.convert(this.birthday);
		}catch (Exception e) {
			isOk = false;
			errors.put("birthday", "生日必須要是一個日期!!");
		}
	}
	
	return isOk;
} 				


數據封裝:

package cn.itcast.utils;

import java.util.Enumeration;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;

public class WebUtils {

	//把request對象中的請求參數封裝到bean中
	public static <T> T request2Bean(HttpServletRequest request,Class<T> clazz){
		try{
			
			T bean = clazz.newInstance();
			//username=aa password=bb [email protected]
			Enumeration e = request.getParameterNames(); 
			while(e.hasMoreElements()){
				String name = (String) e.nextElement();  //username=aaa password=123
				String value = request.getParameter(name);
				BeanUtils.setProperty(bean, name, value);
			}
			return bean;
		}catch (Exception e) {
			throw new RuntimeException(e);
		}
		
	}
	
	
	public static String makeId(){
		//UUID   128 36位字符
		
		return UUID.randomUUID().toString();
	}
	
}


購物車案例:

購物車:
package cn.itcast.domain;

import java.util.LinkedHashMap;
import java.util.Map;

//代表購物車
public class Cart {

	//用於保存購物車中所有商品
	private Map<String,CartItem> map = new LinkedHashMap();
	private double price;  //0
	
	public void add(Book book){  //javaweb
		
		CartItem item = map.get(book.getId());
		if(item!=null){
			item.setQuantity(item.getQuantity()+1);
		}else{
			item = new CartItem();
			item.setBook(book);
			item.setQuantity(1);
			map.put(book.getId(), item);
		}
		
	}
	
	
	public Map<String, CartItem> getMap() {
		return map;
	}
	public void setMap(Map<String, CartItem> map) {
		this.map = map;
	}
	public double getPrice() {
		double totalprice = 0;
		for(Map.Entry<String,CartItem> me : map.entrySet()){
			CartItem item = me.getValue();
			totalprice += item.getPrice();
		}
		return totalprice;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	
	
}

購物項:

package cn.itcast.domain;

//cartItem代表某本書,並表示書出現多少次
public class CartItem {

	private Book book;
	private int quantity;
	private double price;
	
	public Book getBook() {
		return book;
	}
	public void setBook(Book book) {
		this.book = book;
	}
	public int getQuantity() {
		return quantity;
	}
	public void setQuantity(int quantity) {
		this.quantity = quantity;
	}
	public double getPrice() {
		return book.getPrice()*this.quantity;
	}
	public void setPrice(double price) {
		this.price = price;
	}
	
	
	
}













發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章