步骤 2 : 模仿和排错 步骤 3 : 界面效果 步骤 4 : ForeServlet.buy() 步骤 5 : buy.jsp 步骤 6 : buyPage.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 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";
}
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 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 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";
}
}
增值内容,请先登录
完整的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/cart/buyPage.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/cart/buyPage.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"%>
<div class="buyPageDiv">
<form action="forecreateOrder" method="post">
<div class="buyFlow">
<img class="pull-left" src="img/site/simpleLogo.png">
<img class="pull-right" src="img/site/buyflow.png">
<div style="clear:both"></div>
</div>
<div class="address">
<div class="addressTip">输入收货地址</div>
<div>
<table class="addressTable">
<tr>
<td class="firstColumn">详细地址<span class="redStar">*</span></td>
<td><textarea name="address" placeholder="建议您如实填写详细收货地址,例如接到名称,门牌好吗,楼层和房间号等信息"></textarea></td>
</tr>
<tr>
<td>邮政编码</td>
<td><input name="post" placeholder="如果您不清楚邮递区号,请填写000000" type="text"></td>
</tr>
<tr>
<td>收货人姓名<span class="redStar">*</span></td>
<td><input name="receiver" placeholder="长度不超过25个字符" type="text"></td>
</tr>
<tr>
<td>手机号码 <span class="redStar">*</span></td>
<td><input name="mobile" placeholder="请输入11位手机号码" type="text"></td>
</tr>
</table>
</div>
</div>
<div class="productList">
<div class="productListTip">确认订单信息</div>
<table class="productListTable">
<thead>
<tr>
<th colspan="2" class="productListTableFirstColumn">
<img class="tmallbuy" src="img/site/tmallbuy.png">
<a class="marketLink" href="#nowhere">店铺:天猫店铺</a>
<a class="wangwanglink" href="#nowhere"> <span class="wangwangGif"></span> </a>
</th>
<th>单价</th>
<th>数量</th>
<th>小计</th>
<th>配送方式</th>
</tr>
<tr class="rowborder">
<td colspan="2" ></td>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</thead>
<tbody class="productListTableTbody">
<c:forEach items="${ois}" var="oi" varStatus="st" >
<tr class="orderItemTR">
<td class="orderItemFirstTD"><img class="orderItemImg" src="img/productSingle_middle/${oi.product.firstProductImage.id}.jpg"></td>
<td class="orderItemProductInfo">
<a href="foreproduct?pid=${oi.product.id}" class="orderItemProductLink">
${oi.product.name}
</a>
<img src="img/site/creditcard.png" title="支持信用卡支付">
<img src="img/site/7day.png" title="消费者保障服务,承诺7天退货">
<img src="img/site/promise.png" title="消费者保障服务,承诺如实描述">
</td>
<td>
<span class="orderItemProductPrice">¥<fmt:formatNumber type="number" value="${oi.product.promotePrice}" minFractionDigits="2"/></span>
</td>
<td>
<span class="orderItemProductNumber">${oi.number}</span>
</td>
<td><span class="orderItemUnitSum">
¥<fmt:formatNumber type="number" value="${oi.number*oi.product.promotePrice}" minFractionDigits="2"/>
</span></td>
<c:if test="${st.count==1}">
<td rowspan="5" class="orderItemLastTD">
<label class="orderItemDeliveryLabel">
<input type="radio" value="" checked="checked">
普通配送
</label>
<select class="orderItemDeliverySelect" class="form-control">
<option>快递 免邮费</option>
</select>
</td>
</c:if>
</tr>
</c:forEach>
</tbody>
</table>
<div class="orderItemSumDiv">
<div class="pull-left">
<span class="leaveMessageText">给卖家留言:</span>
<span>
<img class="leaveMessageImg" src="img/site/leaveMessage.png">
</span>
<span class="leaveMessageTextareaSpan">
<textarea name="userMessage" class="leaveMessageTextarea"></textarea>
<div>
<span>还可以输入200个字符</span>
</div>
</span>
</div>
<span class="pull-right">店铺合计(含运费): ¥<fmt:formatNumber type="number" value="${total}" minFractionDigits="2"/></span>
</div>
</div>
<div class="orderItemTotalSumDiv">
<div class="pull-right">
<span>实付款:</span>
<span class="orderItemTotalSumSpan">¥<fmt:formatNumber type="number" value="${total}" minFractionDigits="2"/></span>
</div>
</div>
<div class="submitOrderDiv">
<button type="submit" class="submitOrderButton">提交订单</button>
</div>
</form>
</div>
HOW2J公众号,关注后实时获知最新的教程和优惠活动,谢谢。
问答区域
2021-01-25
这里的ois在退出账号时也要remove吧,不然退出,登录其他账号,访问forecreateOrder还是使用的上一个账号的ois
2 个答案
Pipi_pika 跳转到问题位置 答案时间:2021-12-25 直接清空好了,使当前session失效
request.getSession().invalidate();
how2j 跳转到问题位置 答案时间:2021-02-02 e? 你这个说的很对,应该吧session里的数据都摧毁。
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2019-12-12
点击立即购买之后,然后返回,依然会生成订单项
2 个答案
shengchaohua 跳转到问题位置 答案时间:2020-06-11 好像还有确认订单的页面
how2j 跳转到问题位置 答案时间:2019-12-13 在天猫官网,点返回,也会生成订单吧。
回答已经提交成功,正在审核。 请于 我的回答 处查看回答记录,谢谢
2019-04-20
站长你写的时候有什么办法区分不同div里面的内容吗
2019-02-20
关于不登录直接访问forebuy?oiid=xxx显示问题的解决方法
2018-10-01
ois为什么要放在session里而不是request中
提问太多,页面渲染太慢,为了加快渲染速度,本页最多只显示几条提问。还有 7 条以前的提问,请 点击查看
提问之前请登陆
提问已经提交成功,正在审核。 请于 我的提问 处查看提问记录,谢谢
|