步骤 2 : 模仿和排错 步骤 3 : 界面效果 步骤 4 : ForeServlet.cart() 步骤 5 : cart.jsp 步骤 6 : cartPage.jsp
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
public String cart(HttpServletRequest request, HttpServletResponse response, Page page) {
User user =(User) request.getSession().getAttribute("user");
List<OrderItem> ois = orderItemDAO.listByUser(user.getId());
request.setAttribute("ois", ois);
return "cart.jsp";
}
package tmall.servlet;
import java.io.BufferedWriter;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.util.HtmlUtils;
import tmall.bean.Category;
import tmall.bean.OrderItem;
import tmall.bean.Product;
import tmall.bean.ProductImage;
import tmall.bean.PropertyValue;
import tmall.bean.Review;
import tmall.bean.User;
import tmall.comparator.ProductAllComparator;
import tmall.comparator.ProductDateComparator;
import tmall.comparator.ProductPriceComparator;
import tmall.comparator.ProductReviewComparator;
import tmall.comparator.ProductSaleCountComparator;
import tmall.dao.CategoryDAO;
import tmall.dao.ProductDAO;
import tmall.dao.ProductImageDAO;
import tmall.util.Page;
public class ForeServlet extends BaseForeServlet {
public String home(HttpServletRequest request, HttpServletResponse response, Page page) {
List<Category> cs= new CategoryDAO().list();
new ProductDAO().fill(cs);
new ProductDAO().fillByRow(cs);
request.setAttribute("cs", cs);
return "home.jsp";
}
public String register(HttpServletRequest request, HttpServletResponse response, Page page) {
String name = request.getParameter("name");
String password = request.getParameter("password");
name = HtmlUtils.htmlEscape(name);
System.out.println(name);
boolean exist = userDAO.isExist(name);
if(exist){
request.setAttribute("msg", "用户名已经被使用,不能使用");
return "register.jsp";
}
User user = new User();
user.setName(name);
user.setPassword(password);
System.out.println(user.getName());
System.out.println(user.getPassword());
userDAO.add(user);
return "@registerSuccess.jsp";
}
public String login(HttpServletRequest request, HttpServletResponse response, Page page) {
String name = request.getParameter("name");
name = HtmlUtils.htmlEscape(name);
String password = request.getParameter("password");
User user = userDAO.get(name,password);
if(null==user){
request.setAttribute("msg", "账号密码错误");
return "login.jsp";
}
request.getSession().setAttribute("user", user);
return "@forehome";
}
public String product(HttpServletRequest request, HttpServletResponse response, Page page) {
int pid = Integer.parseInt(request.getParameter("pid"));
Product p = productDAO.get(pid);
List<ProductImage> productSingleImages = productImageDAO.list(p, ProductImageDAO.type_single);
List<ProductImage> productDetailImages = productImageDAO.list(p, ProductImageDAO.type_detail);
p.setProductSingleImages(productSingleImages);
p.setProductDetailImages(productDetailImages);
List<PropertyValue> pvs = propertyValueDAO.list(p.getId());
List<Review> reviews = reviewDAO.list(p.getId());
productDAO.setSaleAndReviewNumber(p);
request.setAttribute("reviews", reviews);
request.setAttribute("p", p);
request.setAttribute("pvs", pvs);
return "product.jsp";
}
public String logout(HttpServletRequest request, HttpServletResponse response, Page page) {
request.getSession().removeAttribute("user");
return "@forehome";
}
public String checkLogin(HttpServletRequest request, HttpServletResponse response, Page page) {
User user =(User) request.getSession().getAttribute("user");
if(null!=user)
return "%success";
return "%fail";
}
public String loginAjax(HttpServletRequest request, HttpServletResponse response, Page page) {
String name = request.getParameter("name");
String password = request.getParameter("password");
User user = userDAO.get(name,password);
if(null==user){
return "%fail";
}
request.getSession().setAttribute("user", user);
return "%success";
}
public String category(HttpServletRequest request, HttpServletResponse response, Page page) {
int cid = Integer.parseInt(request.getParameter("cid"));
Category c = new CategoryDAO().get(cid);
new ProductDAO().fill(c);
new ProductDAO().setSaleAndReviewNumber(c.getProducts());
String sort = request.getParameter("sort");
if(null!=sort){
switch(sort){
case "review":
Collections.sort(c.getProducts(),new ProductReviewComparator());
break;
case "date" :
Collections.sort(c.getProducts(),new ProductDateComparator());
break;
case "saleCount" :
Collections.sort(c.getProducts(),new ProductSaleCountComparator());
break;
case "price":
Collections.sort(c.getProducts(),new ProductPriceComparator());
break;
case "all":
Collections.sort(c.getProducts(),new ProductAllComparator());
break;
}
}
request.setAttribute("c", c);
return "category.jsp";
}
public String search(HttpServletRequest request, HttpServletResponse response, Page page){
String keyword = request.getParameter("keyword");
List<Product> ps= new ProductDAO().search(keyword,0,20);
productDAO.setSaleAndReviewNumber(ps);
request.setAttribute("ps",ps);
return "searchResult.jsp";
}
public String buyone(HttpServletRequest request, HttpServletResponse response, Page page) {
int pid = Integer.parseInt(request.getParameter("pid"));
int num = Integer.parseInt(request.getParameter("num"));
Product p = productDAO.get(pid);
int oiid = 0;
User user =(User) request.getSession().getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemDAO.listByUser(user.getId());
for (OrderItem oi : ois) {
if(oi.getProduct().getId()==p.getId()){
oi.setNumber(oi.getNumber()+num);
orderItemDAO.update(oi);
found = true;
oiid = oi.getId();
break;
}
}
if(!found){
OrderItem oi = new OrderItem();
oi.setUser(user);
oi.setNumber(num);
oi.setProduct(p);
orderItemDAO.add(oi);
oiid = oi.getId();
}
return "@forebuy?oiid="+oiid;
}
public String buy(HttpServletRequest request, HttpServletResponse response, Page page){
String[] oiids=request.getParameterValues("oiid");
List<OrderItem> ois = new ArrayList<>();
float total = 0;
for (String strid : oiids) {
int oiid = Integer.parseInt(strid);
OrderItem oi= orderItemDAO.get(oiid);
total +=oi.getProduct().getPromotePrice()*oi.getNumber();
ois.add(oi);
}
request.getSession().setAttribute("ois", ois);
request.setAttribute("total", total);
return "buy.jsp";
}
public String addCart(HttpServletRequest request, HttpServletResponse response, Page page) {
int pid = Integer.parseInt(request.getParameter("pid"));
Product p = productDAO.get(pid);
int num = Integer.parseInt(request.getParameter("num"));
User user =(User) request.getSession().getAttribute("user");
boolean found = false;
List<OrderItem> ois = orderItemDAO.listByUser(user.getId());
for (OrderItem oi : ois) {
if(oi.getProduct().getId()==p.getId()){
oi.setNumber(oi.getNumber()+num);
orderItemDAO.update(oi);
found = true;
break;
}
}
if(!found){
OrderItem oi = new OrderItem();
oi.setUser(user);
oi.setNumber(num);
oi.setProduct(p);
orderItemDAO.add(oi);
}
return "%success";
}
public String cart(HttpServletRequest request, HttpServletResponse response, Page page) {
User user =(User) request.getSession().getAttribute("user");
List<OrderItem> ois = orderItemDAO.listByUser(user.getId());
request.setAttribute("ois", ois);
return "cart.jsp";
}
}
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@include file="include/header.jsp"%>
<%@include file="include/top.jsp"%>
<%@include file="include/simpleSearch.jsp"%>
<%@include file="include/cart/cartPage.jsp"%>
<%@include file="include/footer.jsp"%>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" isELIgnored="false"%> <%@include file="include/header.jsp"%> <%@include file="include/top.jsp"%> <%@include file="include/simpleSearch.jsp"%> <%@include file="include/cart/cartPage.jsp"%> <%@include file="include/footer.jsp"%>
增值内容,请先登录
完整的J2EE模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE一整套技术栈, 从无到有涵盖全部147个知识点,475个开发步骤, 充实J2EE项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<script>
var deleteOrderItem = false;
var deleteOrderItemid = 0;
$(function(){
$("a.deleteOrderItem").click(function(){
deleteOrderItem = false;
var oiid = $(this).attr("oiid")
deleteOrderItemid = oiid;
$("#deleteConfirmModal").modal('show');
});
$("button.deleteConfirmButton").click(function(){
deleteOrderItem = true;
$("#deleteConfirmModal").modal('hide');
});
$('#deleteConfirmModal').on('hidden.bs.modal', function (e) {
if(deleteOrderItem){
var page="foredeleteOrderItem";
$.post(
page,
{"oiid":deleteOrderItemid},
function(result){
if("success"==result){
$("tr.cartProductItemTR[oiid="+deleteOrderItemid+"]").hide();
}
else{
location.href="login.jsp";
}
}
);
}
})
$("img.cartProductItemIfSelected").click(function(){
var selectit = $(this).attr("selectit")
if("selectit"==selectit){
$(this).attr("src","img/site/cartNotSelected.png");
$(this).attr("selectit","false")
$(this).parents("tr.cartProductItemTR").css("background-color","#fff");
}
else{
$(this).attr("src","img/site/cartSelected.png");
$(this).attr("selectit","selectit")
$(this).parents("tr.cartProductItemTR").css("background-color","#FFF8E1");
}
syncSelect();
syncCreateOrderButton();
calcCartSumPriceAndNumber();
});
$("img.selectAllItem").click(function(){
var selectit = $(this).attr("selectit")
if("selectit"==selectit){
$("img.selectAllItem").attr("src","img/site/cartNotSelected.png");
$("img.selectAllItem").attr("selectit","false")
$(".cartProductItemIfSelected").each(function(){
$(this).attr("src","img/site/cartNotSelected.png");
$(this).attr("selectit","false");
$(this).parents("tr.cartProductItemTR").css("background-color","#fff");
});
}
else{
$("img.selectAllItem").attr("src","img/site/cartSelected.png");
$("img.selectAllItem").attr("selectit","selectit")
$(".cartProductItemIfSelected").each(function(){
$(this).attr("src","img/site/cartSelected.png");
$(this).attr("selectit","selectit");
$(this).parents("tr.cartProductItemTR").css("background-color","#FFF8E1");
});
}
syncCreateOrderButton();
calcCartSumPriceAndNumber();
});
$(".orderItemNumberSetting").keyup(function(){
var pid=$(this).attr("pid");
var stock= $("span.orderItemStock[pid="+pid+"]").text();
var price= $("span.orderItemPromotePrice[pid="+pid+"]").text();
var num= $(".orderItemNumberSetting[pid="+pid+"]").val();
num = parseInt(num);
if(isNaN(num))
num= 1;
if(num<=0)
num = 1;
if(num>stock)
num = stock;
syncPrice(pid,num,price);
});
$(".numberPlus").click(function(){
var pid=$(this).attr("pid");
var stock= $("span.orderItemStock[pid="+pid+"]").text();
var price= $("span.orderItemPromotePrice[pid="+pid+"]").text();
var num= $(".orderItemNumberSetting[pid="+pid+"]").val();
num++;
if(num>stock)
num = stock;
syncPrice(pid,num,price);
});
$(".numberMinus").click(function(){
var pid=$(this).attr("pid");
var stock= $("span.orderItemStock[pid="+pid+"]").text();
var price= $("span.orderItemPromotePrice[pid="+pid+"]").text();
var num= $(".orderItemNumberSetting[pid="+pid+"]").val();
--num;
if(num<=0)
num=1;
syncPrice(pid,num,price);
});
$("button.createOrderButton").click(function(){
var params = "";
$(".cartProductItemIfSelected").each(function(){
if("selectit"==$(this).attr("selectit")){
var oiid = $(this).attr("oiid");
params += "&oiid="+oiid;
}
});
params = params.substring(1);
location.href="forebuy?"+params;
});
})
function syncCreateOrderButton(){
var selectAny = false;
$(".cartProductItemIfSelected").each(function(){
if("selectit"==$(this).attr("selectit")){
selectAny = true;
}
});
if(selectAny){
$("button.createOrderButton").css("background-color","#C40000");
$("button.createOrderButton").removeAttr("disabled");
}
else{
$("button.createOrderButton").css("background-color","#AAAAAA");
$("button.createOrderButton").attr("disabled","disabled");
}
}
function syncSelect(){
var selectAll = true;
$(".cartProductItemIfSelected").each(function(){
if("false"==$(this).attr("selectit")){
selectAll = false;
}
});
if(selectAll)
$("img.selectAllItem").attr("src","img/site/cartSelected.png");
else
$("img.selectAllItem").attr("src","img/site/cartNotSelected.png");
}
function calcCartSumPriceAndNumber(){
var sum = 0;
var totalNumber = 0;
$("img.cartProductItemIfSelected[selectit='selectit']").each(function(){
var oiid = $(this).attr("oiid");
var price =$(".cartProductItemSmallSumPrice[oiid="+oiid+"]").text();
price = price.replace(/,/g, "");
price = price.replace(/¥/g, "");
sum += new Number(price);
var num =$(".orderItemNumberSetting[oiid="+oiid+"]").val();
totalNumber += new Number(num);
});
$("span.cartSumPrice").html("¥"+formatMoney(sum));
$("span.cartTitlePrice").html("¥"+formatMoney(sum));
$("span.cartSumNumber").html(totalNumber);
}
function syncPrice(pid,num,price){
$(".orderItemNumberSetting[pid="+pid+"]").val(num);
var cartProductItemSmallSumPrice = formatMoney(num*price);
$(".cartProductItemSmallSumPrice[pid="+pid+"]").html("¥"+cartProductItemSmallSumPrice);
calcCartSumPriceAndNumber();
var page = "forechangeOrderItem";
$.post(
page,
{"pid":pid,"number":num},
function(result){
if("success"!=result){
location.href="login.jsp";
}
}
);
}
</script>
<title>购物车</title>
<div class="cartDiv">
<div class="cartTitle pull-right">
<span>已选商品 (不含运费)</span>
<span class="cartTitlePrice">¥0.00</span>
<button class="createOrderButton" disabled="disabled">结 算</button>
</div>
<div class="cartProductList">
<table class="cartProductTable">
<thead>
<tr>
<th class="selectAndImage">
<img selectit="false" class="selectAllItem" src="img/site/cartNotSelected.png">
全选
</th>
<th>商品信息</th>
<th>单价</th>
<th>数量</th>
<th width="120px">金额</th>
<th class="operation">操作</th>
</tr>
</thead>
<tbody>
<c:forEach items="${ois }" var="oi">
<tr oiid="${oi.id}" class="cartProductItemTR">
<td>
<img selectit="false" oiid="${oi.id}" class="cartProductItemIfSelected" src="img/site/cartNotSelected.png">
<a style="display:none" href="#nowhere"><img src="img/site/cartSelected.png"></a>
<img class="cartProductImg" src="img/productSingle_middle/${oi.product.firstProductImage.id}.jpg">
</td>
<td>
<div class="cartProductLinkOutDiv">
<a href="foreproduct?pid=${oi.product.id}" class="cartProductLink">${oi.product.name}</a>
<div class="cartProductLinkInnerDiv">
<img src="img/site/creditcard.png" title="支持信用卡支付">
<img src="img/site/7day.png" title="消费者保障服务,承诺7天退货">
<img src="img/site/promise.png" title="消费者保障服务,承诺如实描述">
</div>
</div>
</td>
<td>
<span class="cartProductItemOringalPrice">¥${oi.product.orignalPrice}</span>
<span class="cartProductItemPromotionPrice">¥${oi.product.promotePrice}</span>
</td>
<td>
<div class="cartProductChangeNumberDiv">
<span class="hidden orderItemStock " pid="${oi.product.id}">${oi.product.stock}</span>
<span class="hidden orderItemPromotePrice " pid="${oi.product.id}">${oi.product.promotePrice}</span>
<a pid="${oi.product.id}" class="numberMinus" href="#nowhere">-</a>
<input pid="${oi.product.id}" oiid="${oi.id}" class="orderItemNumberSetting" autocomplete="off" value="${oi.number}">
<a stock="${oi.product.stock}" pid="${oi.product.id}" class="numberPlus" href="#nowhere">+</a>
</div>
</td>
<td >
<span class="cartProductItemSmallSumPrice" oiid="${oi.id}" pid="${oi.product.id}" >
¥<fmt:formatNumber type="number" value="${oi.product.promotePrice*oi.number}" minFractionDigits="2"/>
</span>
</td>
<td>
<a class="deleteOrderItem" oiid="${oi.id}" href="#nowhere">删除</a>
</td>
</tr>
</c:forEach>
</tbody>
</table>
</div>
<div class="cartFoot">
<img selectit="false" class="selectAllItem" src="img/site/cartNotSelected.png">
<span>全选</span>
<!-- <a href="#">删除</a> -->
<div class="pull-right">
<span>已选商品 <span class="cartSumNumber" >0</span> 件</span>
<span>合计 (不含运费): </span>
<span class="cartSumPrice" >¥0.00</span>
<button class="createOrderButton" disabled="disabled" >结 算</button>
</div>
</div>
</div>
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2020-04-24
提问
1 个答案
how2j 跳转到问题位置 答案时间:2020-04-25 1. 是bs 中的模态窗口的知识,可前往学习:
https://how2j.cn/k/boostrap/boostrap-modal/502.html
2. 根据数据库设计的第三范式, 数据源应该只有一个,当有多个的时候,就容易造成数据冗余和脏数据。
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2018-10-18
复制站长的代码也出错Uncaught TypeError: $(...).text is not a function
3 个答案
how2j 跳转到问题位置 答案时间:2018-10-20 这个就蛮奇怪了。。。。虽然已经解决了,再试试用 f12 看看加载本地 库是否有报错呢
czwbig 跳转到问题位置 答案时间:2018-10-19 比较过了,不是代码的问题,我用的idea。也不清楚什么原因,最后我删除掉本地应用的jquery和boostrap,直接引用百度的CDN公共库就正常了。
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script> how2j 跳转到问题位置 答案时间:2018-10-19 那你比较一下 会出错与不会出错的代码有什么区别呢
你这样,用diffmerge软件,进行文件夹比较。把你自己做的项目文件夹,和我的可运行项目文件夹进行比较
这个软件很牛逼的,可以知道文件夹里哪两个文件不对,而且很明显地标记出来
我这里就有下载和教程:
http://how2j.cn/k/18/18-1583/1583.html
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2018-08-26
立即购买也会加入购物车
2018-08-23
全选商品后删除订单项,总金额不变
2017-08-29
站长,这个不需要做个登录验证么?
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|