购物车功能(一)
一、门户书籍搜索后台功能实现
实现界面上的搜索功能,可以根据书名查询,也可以根据类别查询,名字查询已经做过,所以只需要在查询的方法上加一个条件。
public List<Book> list(Book book,PageBean pageBean) throws InstantiationException, IllegalAccessException, SQLException{
String name = book.getName();
String sql = "select * from t_easyui_book where true ";
long cid=book.getCid();
if(StringUtils.isNotBlank(name)) {
sql += " and name like '%"+name+"%'";
}
if(StringUtils.isNotBlank(name)) {
sql += " and cid ="+cid;
}
return super.executeQuery(sql, Book.class, pageBean);
}
然而在业务逻辑层里的datagrid方法不能用了,因为我们用forEach遍历做信息展示就直接加载数据、跳转页面,就不能用线程的方式了。所以我们要重新写一个方法
public String search(HttpServletRequest req,HttpServletResponse resp) {
PageBean pageBean = new PageBean();
pageBean.setRequest(req);
try {
List<Book> list = this.bookDao.list(book, pageBean);
// 因为search做遍历
req.setAttribute("books", list);
} catch (InstantiationException | IllegalAccessException | SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "search";
}
再配置一下
<action path="/book" type="com.meng.web.BookAction">
<forward name="search" path="/search.jsp" redirect="false" />
</action>
新建一个js,这个是分类列表
<script src="${pageContext.request.contextPath}/static/js/common.js"></script>
添加事件,
function search(){
location.href="${pageContext.request.contextPath }/book.action?methodName=search&name="+$("#book_name").val();
}
index.js
function searchByType(cid){
var ctx = $("#ctx").val();
location.href=ctx+"/book.action?methodName=search&cid="+cid;
}
二、加入购物车前端页面
需要准备购物车的字段(实体类ShopGoodsVo),放在vo包里
package com.meng.vo;
import java.util.Objects;
public class ShopGoodsVo {
// 购物车列表订单项所需数据
private String name;
private float price;
private int num;
private float total;
// 提交订单所需数据
private String consignee;
private String phone;
private String postalcode;
private String address;
private int sendType;
// 页面的所有传参字符串
private String pageStr;
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
ShopGoodsVo that = (ShopGoodsVo) o;
return Float.compare(that.price, price) == 0 &&
num == that.num &&
Float.compare(that.total, total) == 0 &&
Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name, price, num, total);
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public float getPrice() {
return price;
}
public void setPrice(float price) {
this.price = price;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public float getTotal() {
return total;
}
public void setTotal(float total) {
this.total = total;
}
public String getConsignee() {
return consignee;
}
public void setConsignee(String consignee) {
this.consignee = consignee;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getPostalcode() {
return postalcode;
}
public void setPostalcode(String postalcode) {
this.postalcode = postalcode;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public int getSendType() {
return sendType;
}
public void setSendType(int sendType) {
this.sendType