首页
会员中心
到顶部
到尾部
Java毕业设计

javaStruts+hibernate网上书城系统(无论文)013

时间:2020/10/13 14:45:27  作者:  来源:  查看:0  评论:0
内容摘要: 无论文package bookstore;import java.util.Date;import java.util.Enumeration;import java.util.Hashtable;import java.util.List;import java.uti...

无论文

package bookstore;

import java.util.Date;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.List;
import java.util.ListIterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.hibernate.Session;

import bookstore.database.Bookinfo;
import bookstore.database.HibernateUtil;
import bookstore.database.Order;
import bookstore.database.Userinfo;

public class PayAction extends Action {

    public ActionForward execute(ActionMapping mapping, ActionForm srcForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
        HttpSession session = request.getSession();
        Session dbSession = HibernateUtil.currentSession();
        List books = (List) session.getAttribute("books");
        PayForm form = (PayForm) srcForm;
        // 将一份订单中的相同书籍记录合并成一条,并使数量加1
        Hashtable result = combineBook(books);
        // 将订单写入数据库
        Enumeration enu = result.keys();
        Bookinfo book = new Bookinfo();
        HibernateUtil.beginTransaction();
        while (enu.hasMoreElements()) {
            Order order = new Order();
            order.setUserinfo((Userinfo) session.getAttribute("user"));
            order.setAddress(new String(form.getAddress().getBytes("ISO-8859-1"), "GB2312"));
            order.setPostcode(form.getPostcode());
            order.setStatus((short) 1);
            order.setOrderdate(new Date(System.currentTimeMillis()));
            Integer bookid = (Integer) enu.nextElement();
            Integer number = (Integer) result.get(bookid);
            book.setId(bookid);
            order.setBookinfo(book);
            order.setNumber(number.shortValue());
            dbSession.saveOrUpdate(order);
        }
        HibernateUtil.commitTransaction();
        // 可在此处添加代码以维护用户折扣信息
        session.removeAttribute("books");
        session.removeAttribute("cart");
        return mapping.findForward("frontPage");
    }

    private Hashtable combineBook(List books) {
        ListIterator iter = books.listIterator();
        Hashtable result = new Hashtable();
        while (iter.hasNext()) {
            Bookinfo book = (Bookinfo) iter.next();
            if (result.get(book.getId()) == null) {
                result.put(book.getId(), new Integer(1));
            } else {
                Integer now = (Integer) result.get(book.getId());
                result.remove(book.getId());
                result.put(book.getId(), new Integer(now.intValue() + 1));
            }
        }
        return result;
    }
}

  


相关评论
Java毕业设计
    本类推荐
      广告联系QQ:45157718 点击这里给我发消息 电话:13516821613 杭州余杭东港路118号雷恩国际科技创新园  网站技术支持:黄菊华互联网工作室 浙ICP备06056032号