前言
SSH2(struts2+spring+hibernate)框架.是当今最为流行的项目开发框架组合.用于构建灵活、易于扩展的多层Web应用程序.掌握SHH2对于今后的工作就业显得尤为重要。本次项目的要求就是以SSH2框架为依托.开发一个完整的网上商城项目(手机销售网站).最终要形成一个可以独立运行的系统.并完成相关的文档的建立。
一、需求分析
1.功能分析
1.1功能划分
本网站为用户提供狗粮销售服务.用户可通过本网站浏览并购买狗粮。依据功能需求.包括用户控制、商品浏览、商品筛选、购物车、结算与支付等功能。
1.2功能描述
前台部分
用户控制:提供用户登录、注册和注销功能.其中非登陆用户只能浏览而不能购买商品。
商品浏览:展示所有商品信息.并提供分页功能。
商品筛选:提供商品筛选功能.用户可以依据商品关键字进行商品搜索.也可以按照品
牌或者价格区间对商品进行筛选。
购物车:当登陆用户点击产品的购买按钮时.购物车页面弹出.可以将商品加入到购物车.并可对购物车中的数量进行修改。
结算与支付:登陆用户点击购物车上的结算按钮.后台将会把购物车中的商品生成订单.并跳转到订单中心.订单中心会显示用户所有的订单详细信息(时间.商品信息.数量.订单号.总金额).此时用户可以选择进行支付或是继续浏览商品。
2.性能分析
2.1数据精确度
本项目是一个网上商城项目.涉及到金钱交易.数据的准确性直接影响买卖双方的利益.因此要求极高的数据精确度。
2.2时间特性
该网上商城项目包含订单功能.要求订单生成时间准确、存入到数据库中的内容及时且有效。
2.3适应性
本项目采用SHH2框架进行开发.使用MySQL数据库进行信息存取.以Tomcat作为网页服务器.在页面开发过程中也充分考虑到各种浏览器的兼容性问题.能够提供统一、美观的用户界面.支持跨平台.能满足绝大部分用户的需求。
3.开发环境
开发工具:MyEclipse10+MySQL6.0
操作系统:Win7
运行环境:JDK1.6+Tomcat
二、页面设计
该项目前台部分主要包含以下页面:
showCart.jsp:购物车页面.实现购物车功能
index.jsp: 项目主界面.提供最新的产品信息
Checkout_success.jsp:确认订单界面.展示你的订单的信息
browseBookPaging.jsp:产品列表页面.展示所有狗粮.用户再次页面购买狗粮
menu.jsp:菜单页面.物品的分类
searchbook.jsp:通过此页面展示搜索结果
register.jsp:用户注册页面
Register_success.jsp:用户注册成功页面
login.jsp:用户登录界面
login_success.jsp:用户登录成功界面
searchBook.jsp:查找图书页面
addToCart_success.jsp:添加商品页面
三、项目实现
1.配置文件
a) web.xml
xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd\"> org.apache.struts2.dispatcher.FilterDispatcher org.springframework.web.context.ContextLoaderListener b) applicationContext.xml xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:p=\"http://www.springframework.org/schema/p\" xsi:schemaLocation=\"http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd\"> org.hibernate.dialect.MySQLDialect org/easybooks/bookstore/vo/Catalog.hbm.xml org/easybooks/bookstore/vo/Orderitem.hbm.xml id=\"userService\" id=\"bookService\" id=\"shoppingAction\" id=\"orderService\" c) sturts.xml \"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN\" \"http://struts.apache.org/dtds/struts-2.0.dtd\"> 部分核心代码 BookAction.java package org.easybooks.bookstore.action; import java.util.*; import org.easybooks.bookstore.service.ICatalogService; import org.easybooks.bookstore.service.IBookService; import com.opensymphony.xwork2.*; import org.easybooks.bookstore.util.Pager; public class BookAction extends ActionSupport{ protected ICatalogService catalogService; protected IBookService bookService; protected Integer catalogid; private Integer currentPage=1; private String bookname; public String browseCatalog() throws Exception{ List catalogs=catalogService.getAllCatalogs(); Map request=(Map)ActionContext.getContext().get(\"request\"); request.put(\"catalogs\ return SUCCESS; } public String browseBook() throws Exception{ List books=bookService.getBookbyCatalogid(catalogid); Map request=(Map)ActionContext.getContext().get(\"request\"); request.put(\"books\ return SUCCESS; } public String browseBookPaging() throws Exception{ int totalSize=bookService.getTotalbyCatalog(catalogid); Pager pager=new Pager(currentPage,totalSize); List books=bookService.getBookbyCatalogidPaging(catalogid,currentPage, pager.getPageSize()); Map request=(Map)ActionContext.getContext().get(\"request\"); request.put(\"books\ request.put(\"pager\ //购物车要返回时,需要记住返回的地址 Map session=ActionContext.getContext().getSession(); request.put(\"catalogid\ return SUCCESS; } public String searchBook() throws Exception { StringBuffer hql=new StringBuffer(\"from Book b \"); if(bookname!=null&&bookname.length()!=0) hql.append(\"where b.bookname like '%\"+bookname+\"%'\"); List books=bookService.getRequiredBookbyHql(hql.toString()); Map request=(Map)ActionContext.getContext().get(\"request\"); request.put(\"books\ return SUCCESS; } public Integer getCatalogid(){ return this.catalogid; } public void setCatalogid(Integer catalogid){ this.catalogid=catalogid; } public ICatalogService getCatalogService(){ return this.catalogService; } public void setCatalogService(ICatalogService catalogService){ this.catalogService=catalogService; } public IBookService getBookService(){ return bookService; } public void setBookService(IBookService bookService){ this.bookService=bookService; } public Integer getCurrentPage() { return currentPage; } public void setCurrentPage(Integer currentPage) { this.currentPage=currentPage; } public String getBookname() { return bookname; } public void setBookname(String bookname) { this.bookname=bookname; } } ShoppingAction.java package org.easybooks.bookstore.action; import java.util.Map; import java.util.Iterator; import java.util.Date; import org.easybooks.bookstore.model.Cart; import org.easybooks.bookstore.service.IBookService; import org.easybooks.bookstore.service.IOrderService; import org.easybooks.bookstore.vo.*; import com.opensymphony.xwork2.*; public class ShoppingAction extends ActionSupport{ private int quantity; private Integer bookid; private IBookService bookService; private IOrderService orderService; //添加到购物车 public String addToCart() throws Exception{ Book book=bookService.getBookbyId(bookid); Orderitem orderitem=new Orderitem(); orderitem.setBook(book); orderitem.setQuantity(quantity); Map session=ActionContext.getContext().getSession(); Cart cart=(Cart)session.get(\"cart\"); if(cart==null){ cart=new Cart(); } cart.addBook(bookid, orderitem); session.put(\"cart\ return SUCCESS; } //更新购物车 public String updateCart() throws Exception{ Map session=ActionContext.getContext().getSession(); Cart cart=(Cart)session.get(\"cart\"); cart.updateCart(bookid, this.getQuantity()); session.put(\"cart\ return SUCCESS; } public String checkout() throws Exception{ Map session=ActionContext.getContext().getSession(); User user=(User)session.get(\"user\"); Cart cart=(Cart)session.get(\"cart\"); if(user==null || cart ==null) return ActionSupport.ERROR; Orders order=new Orders(); order.setOrderdate(new Date()); order.setUser(user); for(Iterator it=cart.getItems().values().iterator();it.hasNext();){ Orderitem orderitem=(Orderitem)it.next(); orderitem.setOrders(order); order.getOrderitems().add(orderitem); } orderService.saveOrder(order); Map request=(Map)ActionContext.getContext().get(\"request\"); request.put(\"order\ return SUCCESS; } public Integer getBookid() { return bookid; } public void setBookid(Integer bookid) { this.bookid=bookid; } public int getQuantity() { return quantity; } public void setQuantity(int quantity) { this.quantity=quantity; } public IBookService getBookService() { return bookService; } public void setBookService(IBookService bookService) { this.bookService=bookService; } public IOrderService getOrderService() { return orderService; } public void setOrderService(IOrderService orderService) { this.orderService=orderService; } } UserAction.java package org.easybooks.bookstore.action; import java.util.Map; import org.easybooks.bookstore.service.IUserService; import org.easybooks.bookstore.vo.User; import com.opensymphony.xwork2.*; public class UserAction extends ActionSupport{ private User user; protected IUserService userService; //用户注册.调用service层的saveUser()方法 public String register() throws Exception{ userService.saveUser(user); return SUCCESS; } //用户登录.调用service层的validateUser()方法 public String execute() throws Exception{ User u=userService.validateUser(user.getUsername(),user.getPassword()); if(u!=null) { Map session=ActionContext.getContext().getSession(); //保存此次会话的u(用户账号)信息 session.put(\"user\ return SUCCESS; } return ERROR; } //用户注销.去除会话中的用户账号信息即可.无须调用service层 public String logout() throws Exception{ Map session=ActionContext.getContext().getSession(); session.remove(\"user\"); //session.remove(\"cart\"); return SUCCESS; } public User getUser(){ return this.user; } public void setUser(User user){ this.user=user; } public IUserService getUserService(){ return this.userService; } public void setUserService(IUserService userService){ this.userService=userService; } } Cart.java package org.easybooks.bookstore.model; import java.util.*; import org.easybooks.bookstore.vo.*; public class Cart { protected Map //构造函数 public Cart(){ if(items==null) items=new HashMap } //添加图书到购物车 public void addBook(Integer bookid,Orderitem orderitem){ //是否存在,如果存在,更改数量 //如果不存在的话,添加入集合 if(items.containsKey(\"bookid\")){ Orderitem _orderitem=items.get(bookid); orderitem.setQuantity(_orderitem.getOrderitemid()+orderitem.getQuantity()); items.put(bookid,_orderitem); } else{ items.put(bookid,orderitem); } } //更新购物车的购买书籍数量 public void updateCart(Integer bookid,int quantity){ Orderitem orderitem=items.get(bookid); orderitem.setQuantity(quantity); items.put(bookid, orderitem); } //计算总价格 public int getTotalPrice(){ int totalPrice=0; for(Iterator it=items.values().iterator();it.hasNext();){ Orderitem orderitem=(Orderitem)it.next(); Book book=orderitem.getBook(); int quantity=orderitem.getQuantity(); totalPrice+=book.getPrice()*quantity; } return totalPrice; } public Map return items; } public void setItems(Map this.items=items; } } BaseDAO.java package org.easybooks.bookstore.dao; import org.hibernate.SessionFactory; import org.hibernate.Session; public class BaseDAO { private SessionFactory sessionFactory; public SessionFactory getSessionFactory(){ return sessionFactory; } public void setSessionFactory(SessionFactory sessionFactory){ this.sessionFactory=sessionFactory; } public Session getSession(){ Session session=sessionFactory.openSession(); return session; } } BookDAO.java package org.easybooks.bookstore.dao.impl; import java.util.List; import org.easybooks.bookstore.dao.*; import org.hibernate.*; import org.easybooks.bookstore.vo.Book; public class BookDAO extends BaseDAO implements IBookDAO{ public List getBookbyCatalogid(Integer catalogid){ Session session=getSession(); String hql=\"from Book b where b.catalog.catalogid=?\"; Query query=session.createQuery(hql); query.setParameter(0, catalogid); List books=query.list(); session.close(); return books; } public List getBookbyCatalogidPaging(Integer catalogid,int currentPage,int pageSize){ Session session=getSession(); Query query=session.createQuery(\"from Book b where b.catalog.catalogid=?\"); query.setParameter(0, catalogid); //确定起始游标的位置 int startRow=(currentPage-1)*pageSize; query.setFirstResult(startRow); query.setMaxResults(pageSize); List books=query.list(); session.close(); return books; } public int getTotalbyCatalog(Integer catalogid){ Session session=getSession(); Query query=session.createQuery(\"from b.catalog.catalogid=?\"); query.setParameter(0,catalogid); List books=query.list(); b where Book int totalSize=books.size(); session.close(); return totalSize; } public List getRequiredBookbyHql(String hql) { Session session=getSession(); Query query=session.createQuery(hql); List books=query.list(); return books; } //根据图书号得到图书 public Book getBookbyId(Integer bookid){ Session session=getSession(); Book book=(Book)session.get(Book.class,bookid); session.close(); return book; } } IBookService.java package org.easybooks.bookstore.service; import java.util.List; import org.easybooks.bookstore.vo.Book; public interface IBookService { public List getBookbyCatalogid(Integer catalogid); public List getBookbyCatalogidPaging(Integer catalogid,int currentPage,int pageSize); public int getTotalbyCatalog(Integer catalogid); public List getRequiredBookbyHql(String hql); public Book getBookbyId(Integer bookid); } BookService.java package org.easybooks.bookstore.service.impl; import java.util.List; import org.easybooks.bookstore.dao.IBookDAO; import org.easybooks.bookstore.service.IBookService; import org.easybooks.bookstore.vo.Book; public class BookService implements IBookService{ private IBookDAO bookDAO; public List getBookbyCatalogid(Integer catalogid){ return bookDAO.getBookbyCatalogid(catalogid); } public List getBookbyCatalogidPaging(Integer catalogid,int currentPage,int pageSize){ return bookDAO.getBookbyCatalogidPaging(catalogid, pageSize); } public int getTotalbyCatalog(Integer catalogid){ return bookDAO.getTotalbyCatalog(catalogid); } public List getRequiredBookbyHql(String hql) { return bookDAO.getRequiredBookbyHql(hql); } public IBookDAO getBookDAO() { return bookDAO; currentPage, } public void setBookDAO(IBookDAO bookDAO) { this.bookDAO=bookDAO; } public Book getBookbyId(Integer bookid){ return bookDAO.getBookbyId(bookid); } } Pager.java package org.easybooks.bookstore.util; public class Pager { private int currentPage;//当前页面 private int pageSize=3;//每页的记录数.此处赋了一个初始值.每页显示3条 private int totalSize;//总的记录数 private int totalPage;//总的页数.由总的记录数除以每页的记录数得到:totalSize/pageSize private boolean hasFirst;//是否有第一页 private boolean hasPrevious;//是否有上一页 private boolean hasNext;//是否有下一页 private boolean hasLast;//是否有最后一页 //构造函数.传递当前页、总的记录数 public Pager(int currentPage,int totalSize){ this.currentPage=currentPage; this.totalSize=totalSize; } public int getCurrentPage() { return currentPage; } public void setCurrentPage(int currentPage) { this.currentPage=currentPage; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize=pageSize; } public int getTotalSize() { return totalSize; } public void setTotalSize(int totalSize) { this.totalSize=totalSize; } public int getTotalPage() { totalPage=totalSize/pageSize; if(totalSize%pageSize!=0) totalPage++; return totalPage; } public void setTotalPage(int totalPage) { this.totalPage=totalPage; } public boolean isHasFirst() { if(currentPage==1){ return false; } return true; } public void setHasFirst(boolean hasFirst) { this.hasFirst=hasFirst; } public boolean isHasPrevious() { if(isHasFirst()) return true; else return false; } public void setHasPrevious(boolean hasPrevious) { this.hasPrevious=hasPrevious; } public boolean isHasNext() { if(isHasLast()) return true; else return false; } public void setHasNext(boolean hasNext) { this.hasNext=hasNext; } public boolean isHasLast() { if(currentPage==getTotalPage()) return false; else return true; } public void setHasLast(boolean hasLast) { this.hasLast=hasLast; } } Book.java package org.easybooks.bookstore.vo; import java.util.HashSet; import java.util.Set; /** * Book entity. @author MyEclipse Persistence Tools */ public class Book implements java.io.Serializable { // Fields private Integer bookid; private Catalog catalog; private String bookname; private Integer price; private String picture; private Set orderitems = new HashSet(0); // Constructors /** default constructor */ public Book() { } /** minimal constructor */ public Book(Catalog catalog, String bookname, Integer price, String picture) { this.catalog = catalog; this.bookname = bookname; this.price = price; this.picture = picture; } /** full constructor */ public Book(Catalog catalog, String bookname, Integer price, String picture, Set orderitems) { this.catalog = catalog; this.bookname = bookname; this.price = price; this.picture = picture; this.orderitems = orderitems; } // Property accessors public Integer getBookid() { return this.bookid; } public void setBookid(Integer bookid) { this.bookid = bookid; } public Catalog getCatalog() { return this.catalog; } public void setCatalog(Catalog catalog) { this.catalog = catalog; } public String getBookname() { return this.bookname; } public void setBookname(String bookname) { this.bookname = bookname; } public Integer getPrice() { return this.price; } public void setPrice(Integer price) { this.price = price; } public String getPicture() { return this.picture; } public void setPicture(String picture) { this.picture = picture; } public Set getOrderitems() { return this.orderitems; } public void setOrderitems(Set orderitems) { this.orderitems = orderitems; } } 2.效果截图 网页首页 注册页面 注册页面 登陆页面 产品页面 购物车页面 下单成功页面 搜索页面 按价格搜索 四、项目总结 本系统从需求分析、系统总体设计到系统的具体实现对网上购物系统进行全面的论述。网上购物系统采用基于Java开发语言的JavaEE平台进行开发.运用了MVC框架技术Struts2.采MySQL数据库管理系统以及开源框架Hibernate进行数据存储.对数据库表设计上满足3NF。本系统的设计.秉承了前人不断探索积累的优秀思想和方法.系统的设计总体来说是比较成功的.它顺利地通过了各项测试.可以成功地替代手工购物的管理。进而提高了 管理效率.节省了成本.有很大的发展空间。 因篇幅问题不能全部显示,请点此查看更多更全内容