📄 caraction.java
字号:
package org.itfuture.www.action;
import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.itfuture.www.po.Signon;
import org.itfuture.www.service.impl.CarImpl;
//这是一个处理购物车所有事务的action
public class CarAction extends BaseAction {
//以下的全局属性变量
private String itemid;
private Integer quantity = 1;
public CarAction() {
}
//处理购物商品的方法,此方法用于处理商品信息(购物车信息)
public String execute() throws Exception {
HttpSession session = this.getSession();
CarImpl car = null;
//如果购物车中已经有该物品,则直接修改数量
if (session.getAttribute("car") != null) {
car = (CarImpl) session.getAttribute("car");
car.addItem(itemid, quantity);
} else {
//如果购物车中没有该物品,则生成一个实例,用session存起来
car = new CarImpl();
car.addItem(itemid, quantity);
session.setAttribute("car", car);
}
return this.SUCCESS;
}
//该方法用于删除购物车中的商品
public String delete() {
HttpSession session = this.getSession();
CarImpl car = (CarImpl)session.getAttribute("car");
car.getMap().remove(itemid);
return this.SUCCESS;
}
//该方法用于修改购物车中商品的数量
public String update(){
HttpServletRequest request = this.getRequest();
Enumeration en = request.getParameterNames();
CarImpl car = (CarImpl)request.getSession().getAttribute("car");
double aa = car.totalPrice();
while(en.hasMoreElements()){
String name = en.nextElement().toString();
String value = request.getParameter(name);
car.update(name, Integer.parseInt(value));
}
return this.SUCCESS;
}
//校验用户所要买的商品,即校验购物车
public String checkOrder(){
HttpSession session = this.getSession();
//得到用户信息
Signon signon = (Signon)session.getAttribute("sigon");
//如果用户信息为空,说明还没有登录,
if(signon!=null){
return this.SUCCESS;
}
//请用户登录
return this.INPUT;
}
//以下为全局属性的setter getter方法
public String getItemid() {
return itemid;
}
public void setItemid(String itemid) {
this.itemid = itemid;
}
public Integer getQuantity() {
return quantity;
}
public void setQuantity(Integer quantity) {
this.quantity = quantity;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -