基于javaweb+mysql的ssh网上书店图书商城(java+ssh+mysql+jsp)
私信源码获取及调试交流
运行环境
Java≥8、MySQL≥5.7、Tomcat≥8
开发工具
eclipse/idea/myeclipse/sts等均可配置运行
适用
课程设计,大作业,毕业设计,项目练习,学习演示等
功能说明
基于javaweb的SSH网上书店图书商城(java+ssh+mysql+jsp)
后台:http://localhost:8080/manage/login.jsp
管理员:
admin 123456
前台:http://localhost:8080
用户:
user1 123456
user2 123456
}
this.setUrl("address.jsp");
return SUCCESS;
}
//生成订单页面
public void createorder() throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
HttpSession session = request.getSession();
double totalprice =0.0;
if(session.getAttribute("user")!=null&&!CartService.isEmpty()){
List<OrderItem> orderItems = new ArrayList<OrderItem>();
List<CartItem> cartItems = CartService.getItems();
for(CartItem ci:cartItems){
OrderItem oi = new OrderItem();
oi.setProductid((int)ci.getProduct().getId());
oi.setProductname(ci.getProduct().getName());
oi.setProductnum(ci.getNumber());
oi.setPrice(ci.getProduct().getPrice2()*ci.getNumber());
orderItems.add(oi);
totalprice += oi.getPrice();
Product product = productDao.selectBean(" where id= "+ci.getProduct().getId());
product.setXiaoliang(product.getXiaoliang()+ci.getNumber());
productDao.updateBean(product);
}
Order order = new Order();
order.setUser((User)session.getAttribute("user"));
order.setAddress(request.getParameter("address"));
order.setCreatetime(new Date());
order.setPhone(request.getParameter("phone"));
order.setUsername(request.getParameter("username"));
order.setTotalprice(totalprice);
//order.setfprice(CartService.getPrice2());
order.setOrderItems(orderItems);
order.setOrderid(new java.util.Date().getTime()+"");
for(OrderItem oi:orderItems ){
orderItemDao.insertBean(oi);
}
orderDao.insertBean(order);
CartService.clear();
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}else{
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
<td height="35">
${fn:substring(bean.createtime,0, 10)}
</td>
<td height="35">
${bean.username }
</td>
<td height="35">
货到付款
</td>
<td height="35">
<c:if test="${bean.status==0}">
提交成功
</c:if>
<c:if test="${bean.status==1}">
发货成功
</c:if>
</td>
<td height="35">
<a href="####" onclick="orderinfoshow(${bean.id })">查看详情</a>
</td>
</tr>
</c:forEach>
<tr >
<td colspan="6">${pagerinfo }</td>
</tr>
</table>
</DIV>
<DIV id=clear_div></DIV></DIV></DIV>
<DIV style="FLOAT: right" class=content_box_right_1>
<form action="index" method="post">
商品搜索: <input type="text" value="请输入商品名" name="keyword" size="10" class="inputfield" title="email address" onfocus="clearText(this)" onblur="clearText(this)" />
<input type="submit" name="submit" value="搜索" />
</form>
session.setAttribute("user", u);
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}
}
//用户退出操作
public void loginout() throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
session.removeAttribute("user");
CartService.clear();
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}
//查看商品信息操作
public String product(){
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("product", productDao.selectBean(" where id = "+request.getParameter("pid")));
this.setUrl("product.jsp");
return SUCCESS;
}
//添加商品到购物车操作
public String cartadd() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
//用户退出操作
public void loginout() throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
session.removeAttribute("user");
CartService.clear();
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}
//查看商品信息操作
public String product(){
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("product", productDao.selectBean(" where id = "+request.getParameter("pid")));
this.setUrl("product.jsp");
return SUCCESS;
}
//添加商品到购物车操作
public String cartadd() {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
User user = (User)session.getAttribute("user");
if(user==null){
this.setUrl("loginfirst.jsp");
return SUCCESS;
}
Product product = productDao.selectBean(" where id= "+ request.getParameter("pid"));
if(CartService.addItem(product)){
this.setUrl("cartadd.jsp");
return SUCCESS;
}else{
this.setUrl("cartaddfail.jsp");
return SUCCESS;
}
}
//购物车列表
public void setUserDao(UserDao userDao) {
this.userDao = userDao;
}
public CategoryDao getCategoryDao() {
return categoryDao;
}
public void setCategoryDao(CategoryDao categoryDao) {
this.categoryDao = categoryDao;
}
public ProductDao getProductDao() {
return productDao;
}
public void setProductDao(ProductDao productDao) {
this.productDao = productDao;
}
public OrderDao getOrderDao() {
return orderDao;
}
public void setOrderDao(OrderDao orderDao) {
this.orderDao = orderDao;
}
public OrderItemDao getOrderItemDao() {
return orderItemDao;
}
public void setOrderItemDao(OrderItemDao orderItemDao) {
response.setCharacterEncoding("utf-8");
response.getWriter().write("注册新用户成功!您的用户名"+user.getUsername()+",请妥善保管!");
}else{
response.setCharacterEncoding("utf-8");
response.getWriter().write("该用户名已经存在,请重新注册!");
}
}
//跳转到用户登录页面
public String login(){
HttpServletRequest request = ServletActionContext.getRequest();
List<Category> catelist = categoryDao.selectBeanList(0, 9999, " where catelock=0 ");
request.setAttribute("topcatelist",catelist );
request.setAttribute("xiaoliangprolist",productDao.selectBeanList(0, 15, " where productlock=0 order by xiaoliang desc "));//销量排行商品列表
this.setUrl("login.jsp");
return SUCCESS;
}
//用户登录操作
public void login2() throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
HttpServletResponse response = ServletActionContext.getResponse();
String username = request.getParameter("username");
String password = request.getParameter("password");
User u = userDao.selectBean(" where username='"+username+"' and password='"+password+"' and userlock=0");
if(u==null){
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}else{
HttpSession session = request.getSession();
session.setAttribute("user", u);
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}
}
//用户退出操作
public void loginout() throws IOException{
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
private static HashMap<Integer, CartItem> items = new HashMap<Integer, CartItem>();
public static boolean addItem(Product product) {
if (!items.containsKey((int)product.getId())) {
CartItem item = new CartItem();
item.setProduct(product);
item.setNumber(1);
item.setDrop(false);
items.put((int)product.getId(), item);
return true;
}
return false;
}
public static void clear() {
items.clear();
}
public static boolean drop(int pid) {
if (items.containsKey(pid)) {
CartItem item = items.get(pid);
item.setDrop(true);
return true;
}
return false;
}
public static List<CartItem> getDropItems() {
ArrayList<CartItem> list = new ArrayList<CartItem>();
Iterator<Integer> it = items.keySet().iterator();
while (it.hasNext()) {
CartItem item = items.get(it.next());
if (item.isDrop()) {
list.add(item);
}
}
return list;
}
public static List<CartItem> getItems() {
ArrayList<CartItem> list = new ArrayList<CartItem>();
//删除商品信息操作
public void productdelete() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
Product bean = productDao.selectBean(" where id= "+request.getParameter("id"));
bean.setProductlock(1);
productDao.updateBean(bean);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}
//新品推荐操作
public void productupdate3() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
Product product = productDao.selectBean(" where id = "+request.getParameter("id"));
if(product.getNewstuijian()==0){
product.setNewstuijian(1);
}else{
product.setNewstuijian(0);
}
productDao.updateBean(product);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}
//订单列表
public String orderlist() {
HttpServletRequest request = ServletActionContext.getRequest();
String orderid = request.getParameter("orderid");
String username = request.getParameter("username");
String status = request.getParameter("status");
String time1 = request.getParameter("time1");
String time2 = request.getParameter("time2");
StringBuffer sb = new StringBuffer();
sb.append(" where ");
if(orderid!=null&&!"".equals(orderid)){
sb.append("orderid like '%"+orderid.trim()+"%'");
sb.append(" and ");
user.setTruename("admin");
user.setUsername("admin");
userDao.insertBean(user);
}
}
}
public class CartService{
private static HashMap<Integer, CartItem> items = new HashMap<Integer, CartItem>();
public static boolean addItem(Product product) {
if (!items.containsKey((int)product.getId())) {
CartItem item = new CartItem();
item.setProduct(product);
item.setNumber(1);
item.setDrop(false);
items.put((int)product.getId(), item);
return true;
}
return false;
}
public static void clear() {
items.clear();
}
public void setProductnum(int productnum) {
this.productnum = productnum;
}
}
@Entity
@Table(name="t_Category")
public class Category implements Serializable{
private static final long serialVersionUID = -4350171000214458094L;
private long id;
private String catename;
private int catelock;
public int getCatelock() {
return catelock;
}
public void setCatelock(int catelock) {
this.catelock = catelock;
}
@Id
@GeneratedValue
double totalPrice = 0;
Iterator<Integer> it = items.keySet().iterator();
while (it.hasNext()) {
CartItem item = items.get(it.next());
if (!item.isDrop()) {
totalPrice += item.getProduct().getPrice1()
* item.getNumber();
}
}
return totalPrice;
}
public static double getPrice2() {
double totalPrice = 0;
Iterator<Integer> it = items.keySet().iterator();
while (it.hasNext()) {
CartItem item = items.get(it.next());
if (!item.isDrop()) {
totalPrice += item.getProduct().getPrice2()
* item.getNumber();
}
}
return totalPrice;
}
public static boolean isEmpty() {
return items.isEmpty();
}
public static boolean recovery(int pid) {
if (items.containsKey(pid)) {
CartItem item = items.get(pid);
item.setDrop(false);
return true;
}
return false;
}
return SUCCESS;
}
//跳转到修改密码页面
public String changepwd() {
this.setUrl("user/password.jsp");
return SUCCESS;
}
//修改密码操作
public void changepwd2() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
HttpSession session = request.getSession();
User u = (User)session.getAttribute("manage");
String password1 = request.getParameter("password1");
String password2 = request.getParameter("password2");
User bean = userDao.selectBean(" where username= '"+u.getUsername()+"' and password= '"+password1+"'");
if(bean!=null){
bean.setPassword(password2);
userDao.updateBean(bean);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}else{
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
}
}
//商品分类列表
public String catelist() {
HttpServletRequest request = ServletActionContext.getRequest();
int currentpage = 1;
int pagesize = 10;
if(request.getParameter("pagenum")!=null){
currentpage = Integer.parseInt(request.getParameter("pagenum"));
}
String where = " where catelock=0";
int total = categoryDao.selectBeanCount(where);
request.setAttribute("list", categoryDao.selectBeanList((currentpage - 1) * pagesize, pagesize, where));
request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
currentpage, "houtaimethod!catelist", "共有" + total + "条记录"));
this.setUrl("product/catelist.jsp");
request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
currentpage, "houtaimethod!productlist", "共有" + total + "条记录"));
this.setUrl("product/productlist.jsp");
return SUCCESS;
}
//跳转到添加商品页面
public String productadd() {
HttpServletRequest request = ServletActionContext.getRequest();
String where = " where catelock=0 ";
request.setAttribute("list", categoryDao.selectBeanList(0, 9999, where));
this.setUrl("product/productadd.jsp");
return SUCCESS;
}
private File uploadfile;
public File getUploadfile() {
return uploadfile;
}
public void setUploadfile(File uploadfile) {
this.uploadfile = uploadfile;
}
//添加商品操作
public void productadd2() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
String name = request.getParameter("name");
String cid = request.getParameter("cid");
String price1 = request.getParameter("price1");
String price2 = request.getParameter("price2");
String info5 = request.getParameter("info5");
String imgpath = this.uploadImg(uploadfile);
Product product = new Product();
product.setName(name);
product.setCate(categoryDao.selectBean( " where catelock=0 and id= " +cid));
product.setPrice1(Double.parseDouble(price1));
product.setPrice2(Double.parseDouble(price2));
product.setInfo5(info5);
product.setImgpath(imgpath);
product.setCreatetime(new Date());
productDao.insertBean(product);
HttpServletResponse response = ServletActionContext.getResponse();
response.setCharacterEncoding("gbk");response.setContentType("text/html; charset=gbk");
response
.getWriter()
.print(
public void setProductname(String productname) {
this.productname = productname;
}
public int getProductnum() {
return productnum;
}
public void setProductnum(int productnum) {
this.productnum = productnum;
}
}
@Entity
@Table(name="t_Category")
public class Category implements Serializable{
private static final long serialVersionUID = -4350171000214458094L;
private long id;
private String catename;
private int catelock;
public int getCatelock() {
return catelock;
}
public void setCatelock(int catelock) {
this.catelock = catelock;
}
this.getHibernateTemplate().save(bean);
}
@SuppressWarnings("unchecked")
public Category selectBean(String where) {
List<Category> list = this.getHibernateTemplate().find("from Category " +where);
if(list.size()==0){
return null;
}
return list.get(0);
}
public int selectBeanCount(String where) {
long count = (Long)this.getHibernateTemplate().find("select count(*) from Category "+where).get(0);
return (int)count;
}
@SuppressWarnings("unchecked")
public List<Category> selectBeanList(final int start,final int limit,final String where) {
return (List<Category>)this.getHibernateTemplate().executeFind(new HibernateCallback() {
public Object doInHibernate(final Session session)throws HibernateException, SQLException {
List<Category> list = session.createQuery("from Category "+where)
.setFirstResult(start)
.setMaxResults(limit)
.list();
return list;
}
});
}
public void updateBean(Category bean) {
this.getHibernateTemplate().update(bean);
}
}
}
public void setPrice(double price) {
this.price = price;
}
@Id
@GeneratedValue
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public int getProductid() {
return productid;
}
public void setProductid(int productid) {
this.productid = productid;
}
public String getProductname() {
return productname;
}
public void setProductname(String productname) {
this.productname = productname;
}
public int getProductnum() {
return productnum;
}
public void setProductnum(int productnum) {
this.productnum = productnum;
}
}
@Entity
@Table(name="t_order")
public class Order implements Serializable{
private static final long serialVersionUID = 5693797768042771915L;
private long id;
private String orderid;
private Date createtime;
private User user;
private String username;
private String address;
private String phone;
private double totalprice;
private List<OrderItem> orderItems;
private int status;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getUsername() {
return username;
}
//商品列表
public String productlist() {
HttpServletRequest request = ServletActionContext.getRequest();
int currentpage = 1;
int pagesize =10;
if(request.getParameter("pagenum")!=null){
currentpage = Integer.parseInt(request.getParameter("pagenum"));
}
String where = " where productlock=0 order by id desc";
int total = productDao.selectBeanCount(where.replaceAll("order by id desc", ""));
request.setAttribute("list", productDao.selectBeanList((currentpage - 1) * pagesize, pagesize, where));
request.setAttribute("pagerinfo", Pager.getPagerNormal(total, pagesize,
currentpage, "houtaimethod!productlist", "共有" + total + "条记录"));
this.setUrl("product/productlist.jsp");
return SUCCESS;
}
//跳转到添加商品页面
public String productadd() {
HttpServletRequest request = ServletActionContext.getRequest();
String where = " where catelock=0 ";
request.setAttribute("list", categoryDao.selectBeanList(0, 9999, where));
this.setUrl("product/productadd.jsp");
return SUCCESS;
}
private File uploadfile;
public File getUploadfile() {
return uploadfile;
}
public void setUploadfile(File uploadfile) {
this.uploadfile = uploadfile;
}
//添加商品操作
public void productadd2() throws IOException {
HttpServletRequest request = ServletActionContext.getRequest();
String name = request.getParameter("name");
String cid = request.getParameter("cid");
String price1 = request.getParameter("price1");
String price2 = request.getParameter("price2");
String info5 = request.getParameter("info5");
String imgpath = this.uploadImg(uploadfile);