Java struts2编程
答案:3 悬赏:60 手机版
解决时间 2021-05-13 07:17
- 提问者网友:未信
- 2021-05-12 06:59
用struts2实现一个简单的购物车,将物品放到session中,具体实现方法 步骤,要详细
最佳答案
- 五星知识达人网友:渡鹤影
- 2021-05-12 08:33
ShopCart.java:
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import com.ascent.po.Product;
public class ShopCart {
private Hashtable<Integer, CarItem> car = null;
private Collection<CarItem> carItems = null;
public ShopCart() {
car = new Hashtable<Integer, CarItem>();
carItems = car.values();// 初始化生成购物车项
}
public Hashtable<Integer, CarItem> getCar() {
return car;
}
public void setCar(Hashtable<Integer, CarItem> car) {
this.car = car;
}
public Collection<CarItem> getCarItems() {
return carItems;
}
public void setCarItems(Collection<CarItem> carItems) {
this.carItems = carItems;
}
public void addProduct(Product product) {
if (!car.containsKey(product.getPid())) {// 第一次购买,当car中没有的时候
// ,准备key和value的值
// 往map集合中放,然后生成了购物车项carItems
CarItem item = new CarItem(product);
car.put(product.getPid(), item);
carItems = car.values();
} else { // 第二次购买
CarItem item = car.get(product.getPid());
item.increase();
}
}
public void updateProductNumber(Integer pid, Integer number) {
CarItem item = car.get(pid);
item.setNumber(number);
carItems = car.values();
}
public void deletProduct(Integer pid) {
if (car.containsKey(pid)) {
car.remove(pid);
carItems = car.values();
}
}
public double getTotalPrice() {
Iterator<CarItem> it = carItems.iterator();
double total = 0.0;
while (it.hasNext()) {
CarItem carItem = it.next();
total += carItem.getCarItemPrice();
}
return total;
}
}
CarItem.java
import com.ascent.po.Product;
public class CarItem {
private Product product;
private int number;
public CarItem(Product product){
this.product=product;
this.number=1;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public void increase(){
number++;
}
public double getCarItemPrice(){
return new Double(product.getPrice1()).doubleValue()*number;
}
}
以上这两个类是 实现购物车功能的工具类,以前就是用这两个类开发过购物车的
以下是 action类 这个是问题关键所在
@SuppressWarnings("unchecked")
public String addProduct() {
ShoppingCart car = (ShoppingCart) ActionContext.getContext().getSession().get("ShoppingCar");
System.out.println("-----------------"+car);
if (car == null) {
car = new ShoppingCart();
ActionContext.getContext().getSession().put("ShoppingCar", car);
}
ProductDao productdao = new ProductDao();
Product product = productdao.getProductByPid(pid);
System.out.println("-----------------"+product);
car.addProduct(product);
return SUCCESS;
}
public String deletProduct() {
ShoppingCart car = (ShoppingCart) ActionContext.getContext()
.getSession().get("ShoppingCar");
car.deletProduct(pid);
return SUCCESS;
}
public String updateProduct() {
ShoppingCart car = (ShoppingCart) ActionContext.getContext().getSession().get("ShoppingCar");
int n = new Integer(number);
car.updateProductNumber(pid, n);
return SUCCESS;
}
public String getToalePrices() {
ShoppingCart car = (ShoppingCart) ActionContext.getContext()
.getSession().get("ShoppingCar");
totalPrice = car.getTotalPrice();
return SUCCESS;
}
import java.util.Collection;
import java.util.Hashtable;
import java.util.Iterator;
import com.ascent.po.Product;
public class ShopCart {
private Hashtable<Integer, CarItem> car = null;
private Collection<CarItem> carItems = null;
public ShopCart() {
car = new Hashtable<Integer, CarItem>();
carItems = car.values();// 初始化生成购物车项
}
public Hashtable<Integer, CarItem> getCar() {
return car;
}
public void setCar(Hashtable<Integer, CarItem> car) {
this.car = car;
}
public Collection<CarItem> getCarItems() {
return carItems;
}
public void setCarItems(Collection<CarItem> carItems) {
this.carItems = carItems;
}
public void addProduct(Product product) {
if (!car.containsKey(product.getPid())) {// 第一次购买,当car中没有的时候
// ,准备key和value的值
// 往map集合中放,然后生成了购物车项carItems
CarItem item = new CarItem(product);
car.put(product.getPid(), item);
carItems = car.values();
} else { // 第二次购买
CarItem item = car.get(product.getPid());
item.increase();
}
}
public void updateProductNumber(Integer pid, Integer number) {
CarItem item = car.get(pid);
item.setNumber(number);
carItems = car.values();
}
public void deletProduct(Integer pid) {
if (car.containsKey(pid)) {
car.remove(pid);
carItems = car.values();
}
}
public double getTotalPrice() {
Iterator<CarItem> it = carItems.iterator();
double total = 0.0;
while (it.hasNext()) {
CarItem carItem = it.next();
total += carItem.getCarItemPrice();
}
return total;
}
}
CarItem.java
import com.ascent.po.Product;
public class CarItem {
private Product product;
private int number;
public CarItem(Product product){
this.product=product;
this.number=1;
}
public Product getProduct() {
return product;
}
public void setProduct(Product product) {
this.product = product;
}
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public void increase(){
number++;
}
public double getCarItemPrice(){
return new Double(product.getPrice1()).doubleValue()*number;
}
}
以上这两个类是 实现购物车功能的工具类,以前就是用这两个类开发过购物车的
以下是 action类 这个是问题关键所在
@SuppressWarnings("unchecked")
public String addProduct() {
ShoppingCart car = (ShoppingCart) ActionContext.getContext().getSession().get("ShoppingCar");
System.out.println("-----------------"+car);
if (car == null) {
car = new ShoppingCart();
ActionContext.getContext().getSession().put("ShoppingCar", car);
}
ProductDao productdao = new ProductDao();
Product product = productdao.getProductByPid(pid);
System.out.println("-----------------"+product);
car.addProduct(product);
return SUCCESS;
}
public String deletProduct() {
ShoppingCart car = (ShoppingCart) ActionContext.getContext()
.getSession().get("ShoppingCar");
car.deletProduct(pid);
return SUCCESS;
}
public String updateProduct() {
ShoppingCart car = (ShoppingCart) ActionContext.getContext().getSession().get("ShoppingCar");
int n = new Integer(number);
car.updateProductNumber(pid, n);
return SUCCESS;
}
public String getToalePrices() {
ShoppingCart car = (ShoppingCart) ActionContext.getContext()
.getSession().get("ShoppingCar");
totalPrice = car.getTotalPrice();
return SUCCESS;
}
全部回答
- 1楼网友:由着我着迷
- 2021-05-12 10:22
session中存一个List,list里面可以装物品对象,也可以装主见Id,添加一个物品,就在list.add一个,删除就list.remove就行了
- 2楼网友:神也偏爱
- 2021-05-12 09:04
用JSP 就能了 还用那么复杂?
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯