cartList.jsp
<%@page import="org.jsoft.vo.Cart"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<table border="1px">
<tr>
<th>商品名称</th>
<th>数量</th>
<th>小结</th>
<th>操作</th>
</tr>
${requestScope.str}
</table>
<a href="sys_userAction?type=check">去结算</a>
${requestScope.aStr}
</body>
</html>
CartAction
package org.jsoft.action;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.jsoft.service.ICartService;
import org.jsoft.service.impl.CartServiceImpl;
import org.jsoft.vo.Cart;
import org.jsoft.vo.Goods;
import org.jsoft.vo.Sys_User;
public class CartAction extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 1L;
@Override
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
@Override
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("type");
// 获取session中的用户信息,session中能保留30分钟的会话
HttpSession session = request.getSession();
ICartService cartService = new CartServiceImpl();
Sys_User s = (Sys_User) session.getAttribute("USER");
if ("add".equals(method)) {
String idd = request.getParameter("id"); // 传过来的商品id
Long id = Long.valueOf(idd);
Cart cart = new Cart();
cart.setGoods_id(id);
cart.setSys_user_id(s.getId());
boolean b = cartService.add(cart);
if (b == true) {
request.getRequestDispatcher("cartAction?type=list").forward(
request, response);
}
} else if ("list".equals(method)) { //查询
// 通过用户的id来查询用户的购物车
Cart cart = new Cart();
cart.setSys_user_id(s.getId());
//通过页面传过来的参数
String page = request.getParameter("page");
String num = request.getParameter("num");
//定义两个变量
int pageInt = 1;
int numInt = 3;
//判断传过来的变量是否为空,如果为空,那就是第一页,如果不为空,那就把值替换一下
if(page != null){
pageInt = Integer.valueOf(page);
}
if(num != null){
numInt = Integer.valueOf(num);
}
//从哪条开始查是算出来的(当前页-1)*要查询的条数 = 从哪条开始查询
int begin = (pageInt - 1)*numInt;
List<Cart> cartList = cartService.query(begin, numInt, cart);
//这是页面中拼接字符串的内容
String str = "";
if(cartList != null && cartList.size() > 0){
for(int i = 0 ;i < cartList.size() ; i++){
str +="<tr>";
str +=" <td>"+cartList.get(i).getGoods().getName()+"</td>";
str +=" <td>"+cartList.get(i).getNum()+"</td>";
str +=" <td>"+(cartList.get(i).getNum())*(cartList.get(i).getGoods().getPrice())+"</td>";
str +=" <td><a href=\"cartAction?type=remove&&cartId="+cartList.get(i).getId()+"\">移除购物车</a></td>";
str +="</tr>";
}
}
//查询购物车这个用户有多少条数据
int sum = cartService.count(cart);
//通过sum求出一共有多少页,如果总条数对3取余为0,那就是有那些页,否则得加+1
if(sum%3 == 0){
sum /= 3;
}else{
sum = sum/3 + 1;
}
//用来拼接超链接的字符串
String aStr = "";
for(int j = 1; j <= sum ; j++ ){
aStr += "<a href=\"cartAction?type=list&page="+j+"&num=3\">第"+j+"页</a>";
}
if (cart != null) {
//将字符串带到页面
request.setAttribute("str", str);
request.setAttribute("aStr", aStr);
request.getRequestDispatcher("cartList.jsp").forward(request,
response);
}
} else if ("remove".equals(method)) {
String cd = request.getParameter("cartId");
Long cartId = Long.valueOf(cd);
// 根据购物车的id进行删除
Cart cart = new Cart();
cart.setId(cartId);
int i = cartService.remove(cart);
if (i > 0) {
request.getRequestDispatcher("cartAction?type=list").forward(
request, response);
}
}
}
}