步骤 2 : 模仿和排错 步骤 3 : 界面效果 步骤 4 : ForeAction.cart() 步骤 5 : cart.jsp 步骤 6 : cartPage.jsp
增值内容,请先登录
完整的SSH模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE,SSH一整套技术栈, 从无到有涵盖全部133个知识点,571个开发步骤, 充实SSH项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的SSH模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE,SSH一整套技术栈, 从无到有涵盖全部133个知识点,571个开发步骤, 充实SSH项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的SSH模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE,SSH一整套技术栈, 从无到有涵盖全部133个知识点,571个开发步骤, 充实SSH项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
增值内容,请先登录
完整的SSH模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE,SSH一整套技术栈, 从无到有涵盖全部133个知识点,571个开发步骤, 充实SSH项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
@Action("forecart")
public String cart() {
User user =(User) ActionContext.getContext().getSession().get("user");
orderItems = orderItemService.list("user",user,"order", null);
for (OrderItem orderItem : orderItems)
productImageService.setFirstProdutImage(orderItem.getProduct());
return "cart.jsp";
}
package com.how2java.tmall.action;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.struts2.convention.annotation.Action;
import org.springframework.web.util.HtmlUtils;
import com.how2java.tmall.comparator.ProductAllComparator;
import com.how2java.tmall.comparator.ProductDateComparator;
import com.how2java.tmall.comparator.ProductPriceComparator;
import com.how2java.tmall.comparator.ProductReviewComparator;
import com.how2java.tmall.comparator.ProductSaleCountComparator;
import com.how2java.tmall.pojo.OrderItem;
import com.how2java.tmall.pojo.Product;
import com.how2java.tmall.pojo.User;
import com.how2java.tmall.service.ProductImageService;
import com.opensymphony.xwork2.ActionContext;
public class ForeAction extends Action4Result {
@Action("forecart")
public String cart() {
User user =(User) ActionContext.getContext().getSession().get("user");
orderItems = orderItemService.list("user",user,"order", null);
for (OrderItem orderItem : orderItems)
productImageService.setFirstProdutImage(orderItem.getProduct());
return "cart.jsp";
}
@Action("foreaddCart")
public String addCart() {
User user =(User) ActionContext.getContext().getSession().get("user");
boolean found = false;
List<OrderItem> ois = orderItemService.list("user",user,"order", null);
for (OrderItem oi : ois) {
if(oi.getProduct().getId()==product.getId()){
oi.setNumber(oi.getNumber()+num);
orderItemService.update(oi);
found = true;
break;
}
}
if(!found){
OrderItem oi = new OrderItem();
oi.setUser(user);
oi.setNumber(num);
oi.setProduct(product);
orderItemService.save(oi);
}
return "success.jsp";
}
@Action("forebuy")
public String buy() {
orderItems = new ArrayList<>();
for (int oiid : oiids) {
OrderItem oi= (OrderItem) orderItemService.get(oiid);
total +=oi.getProduct().getPromotePrice()*oi.getNumber();
orderItems.add(oi);
productImageService.setFirstProdutImage(oi.getProduct());
}
ActionContext.getContext().getSession().put("orderItems", orderItems);
return "buy.jsp";
}
@Action("forebuyone")
public String buyone() {
User user =(User) ActionContext.getContext().getSession().get("user");
boolean found = false;
List<OrderItem> ois = orderItemService.list("user",user,"order", null);
for (OrderItem oi : ois) {
if(oi.getProduct().getId()==product.getId()){
oi.setNumber(oi.getNumber()+num);
orderItemService.update(oi);
found = true;
oiid = oi.getId();
break;
}
}
if(!found){
OrderItem oi = new OrderItem();
oi.setUser(user);
oi.setNumber(num);
oi.setProduct(product);
orderItemService.save(oi);
oiid = oi.getId();
}
return "buyPage";
}
@Action("foresearch")
public String search(){
products= productService.search(keyword,0,20);
productService.setSaleAndReviewNumber(products);
for (Product product : products)
productImageService.setFirstProdutImage(product);
return "searchResult.jsp";
}
@Action("forecategory")
public String category(){
t2p(category);
productService.fill(category);
productService.setSaleAndReviewNumber(category.getProducts());
if(null!=sort){
switch(sort){
case "review":
Collections.sort(category.getProducts(),new ProductReviewComparator());
break;
case "date" :
Collections.sort(category.getProducts(),new ProductDateComparator());
break;
case "saleCount" :
Collections.sort(category.getProducts(),new ProductSaleCountComparator());
break;
case "price":
Collections.sort(category.getProducts(),new ProductPriceComparator());
break;
case "all":
Collections.sort(category.getProducts(),new ProductAllComparator());
break;
}
}
return "category.jsp";
}
@Action("foreloginAjax")
public String loginAjax() {
user.setName(HtmlUtils.htmlEscape(user.getName()));
User user_session = userService.get(user.getName(),user.getPassword());
if(null==user_session)
return "fail.jsp";
ActionContext.getContext().getSession().put("user", user_session);
return "success.jsp";
}
@Action("forecheckLogin")
public String checkLogin() {
User u =(User) ActionContext.getContext().getSession().get("user");
if(null==u)
return "fail.jsp";
else
return "success.jsp";
}
@Action("foreproduct")
public String product() {
t2p(product);
productImageService.setFirstProdutImage(product);
productSingleImages = productImageService.list("product",product,"type", ProductImageService.type_single);
productDetailImages = productImageService.list("product",product,"type", ProductImageService.type_detail);
product.setProductSingleImages(productSingleImages);
product.setProductDetailImages(productDetailImages);
propertyValues = propertyValueService.listByParent(product);
reviews = reviewService.listByParent(product);
productService.setSaleAndReviewNumber(product);
return "product.jsp";
}
@Action("forelogout")
public String logout() {
ActionContext.getContext().getSession().remove("user");
return "homePage";
}
@Action("forelogin")
public String login() {
user.setName(HtmlUtils.htmlEscape(user.getName()));
User user_session = userService.get(user.getName(),user.getPassword());
if(null==user_session){
msg= "账号密码错误";
return "login.jsp";
}
ActionContext.getContext().getSession().put("user", user_session);
return "homePage";
}
@Action("foreregister")
public String register() {
user.setName(HtmlUtils.htmlEscape(user.getName()));
boolean exist = userService.isExist(user.getName());
if(exist){
msg = "用户名已经被使用,不能使用";
return "register.jsp";
}
userService.save(user);
return "registerSuccessPage";
}
@Action("forehome")
public String home() {
categorys = categoryService.list();
productService.fill(categorys);
productService.fillByRow(categorys);
return "home.jsp";
}
}
增值内容,请先登录
完整的SSH模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE,SSH一整套技术栈, 从无到有涵盖全部133个知识点,571个开发步骤, 充实SSH项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
<%@ 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"%>
增值内容,请先登录
完整的SSH模仿天猫项目,使用J2SE、前端技术(包含所有前端jsp文件)、J2EE,SSH一整套技术栈, 从无到有涵盖全部133个知识点,571个开发步骤, 充实SSH项目经验,为简历加上一个有吸引力的砝码.
增值内容,点击购买
使用爬虫已经被系统记录,请勿使用爬虫,增大封号风险。 如果是误封 ,请联系站长,谢谢
<%@ 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,
{"orderItem.id":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 += "&oiids="+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,
{"product.id":pid,"num":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="${orderItems }" 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?product.id=${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.originalPrice}</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公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2019-09-14
@Action("forecart")方法设置的orderItems没有传到cart.jsp页面
7 个答案
我是傻逼 跳转到问题位置 答案时间:2020-07-09 这里不用ActionContext.getContext().getSession().put("orderItems", orderItems);也可以正常访问 设置session是为了待会创建订单用的
Ivan2019 跳转到问题位置 答案时间:2019-12-21 1.为什么: ActionContext.getContext().getSession().put("orderItems", orderItems);
因为:
点击产品页(立即购买)-》buy.jsp中间隔着一页(name = "buyPage", type = "redirect", location = "forebuy?oiids=${oiid}")
实际:立即购买-》buyPage-》buy.jsp
2.加入购物箱页面
就是所谓的注入orderItems = orderItemService.list("user",user,"order", null);
直接跳转cart.jsp
---------------------------------------
其实这里主要是涉及到作用域的问题,并且主要是两个功能页面,独立分开来。
JavaCBird 跳转到问题位置 答案时间:2019-10-21 Action4Pojo里你是不是忘记写orderItems的set,get方法了?
how2j 跳转到问题位置 答案时间:2019-09-17 这里是在不同的页面之间共享 orderItems数据,所以只能通过 session 来共享了呀
俗人 跳转到问题位置 答案时间:2019-09-16 然后我就参考了前面@Action("forebuy")映射函数中的方法:
ActionContext.getContext().getSession().put("orderItems", orderItems);
将orderItems设置到session上,然后才能从cart.jsp中显示订单项数据。同理,为什么buy() 方法中:
orderItems = new ArrayList<>();
for (int oiid : oiids) {
OrderItem oi= (OrderItem) orderItemService.get(oiid);
total +=oi.getProduct().getPromotePrice()*oi.getNumber();
orderItems.add(oi);
productImageService.setFirstProdutImage(oi.getProduct());
}
改段代码已经将数据填充到orderItems中了,为啥还要ActionContext.getContext().getSession().put("orderItems", orderItems);
将orderItems绑定到session上,难道跳转到buy.jsp后orderItems中的数据会丢失?
俗人 跳转到问题位置 答案时间:2019-09-16 就是orderItems不是已经在
orderItems = orderItemService.list("user",user,"order", null);
这段代码中完成数据的注入了嘛,而且return "cart.jsp";是服务端跳转到cart.jsp页面,应该可以直接拿出来显示了才对,可是代码运行后发现cart.jsp页面orderItems没有任何值在里面
how2j 跳转到问题位置 答案时间:2019-09-16 “orderItems不应该已经注入了么”
请阐述一下,你理解的注入是什么意思呢
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2018-07-26
productImageService的setFirstProdutImage()方法名漏了一个c
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|