private ShoppingBean bean=new ShoppingBean();//提交后数据都保存在beans中
private CartItemsBean cartitems=new CartItemsBean();
List<CartItemsBean> listcart;//作为购物车的集合
public String cart()
{
System.out.println("Id+---sdfsdf-----------------"+bean.getId());
System.out.println("Number+--------fsdfsd------------"+bean.getNumber());
System.out.println("Title+-------fsdf-------------"+bean.getTitle());
//判断购物车是否存在
if(listcart==null)
{
listcart=new ArrayList<CartItemsBean>();
}
//判断书籍是否在购物车中
if(listcart.contains(bean.getId()))
{
cartitems.setQuantity(cartitems.getQuantity()+1);
}
else
{
listcart.add(new CartItemsBean(bean,1));
}
return SUCCESS;
}
public class CartItemsBean implements Serializable {
private ShoppingBean bean;
private int quantity;
public CartItemsBean(){}
public CartItemsBean(ShoppingBean beanadd,int number)
{
bean=beanadd;
quantity=number;
}
get set 省略.... jsp页面
<s:iterator value="listcart" id="list">
<table>
<tr>
<td>编号</td>
<td>商品名称</td>
<td>单价</td>
<td>数量</td>
<td>金额</td>
</tr>
<tr>
<td><s:property alue="#list.bean.number"/></td>
<td><s:property value="#list.bean.title"/></td>
<td><s:property value="#list.bean.price"/></td>
<td><s:property value="quantity"/></td>
<td><s:property value="#list.bean.price"/>*<s:property value="quantity"/>元</td>
</tr>
</table>
</s:iterator>
Action中有问题!! 请各位高手一起帮帮忙啊!! 小弟感谢了!!
private CartItemsBean cartitems=new CartItemsBean();
List<CartItemsBean> listcart;//作为购物车的集合
public String cart()
{
System.out.println("Id+---sdfsdf-----------------"+bean.getId());
System.out.println("Number+--------fsdfsd------------"+bean.getNumber());
System.out.println("Title+-------fsdf-------------"+bean.getTitle());
//判断购物车是否存在
if(listcart==null)
{
listcart=new ArrayList<CartItemsBean>();
}
//判断书籍是否在购物车中
if(listcart.contains(bean.getId()))
{
cartitems.setQuantity(cartitems.getQuantity()+1);
}
else
{
listcart.add(new CartItemsBean(bean,1));
}
return SUCCESS;
}
public class CartItemsBean implements Serializable {
private ShoppingBean bean;
private int quantity;
public CartItemsBean(){}
public CartItemsBean(ShoppingBean beanadd,int number)
{
bean=beanadd;
quantity=number;
}
get set 省略.... jsp页面
<s:iterator value="listcart" id="list">
<table>
<tr>
<td>编号</td>
<td>商品名称</td>
<td>单价</td>
<td>数量</td>
<td>金额</td>
</tr>
<tr>
<td><s:property alue="#list.bean.number"/></td>
<td><s:property value="#list.bean.title"/></td>
<td><s:property value="#list.bean.price"/></td>
<td><s:property value="quantity"/></td>
<td><s:property value="#list.bean.price"/>*<s:property value="quantity"/>元</td>
</tr>
</table>
</s:iterator>
Action中有问题!! 请各位高手一起帮帮忙啊!! 小弟感谢了!!
解决方案 »
- JSP+Servlet和filter在使用时一些困惑的问题,用一种什么样的方式去处理会更好吧
- fusioncharts setDataURL方法可以使用绝对位置么?
- spring javamail 如何发送动态生成的附件?
- svn每次update的时候
- 有使过jgroups的朋友吗,进来帮帮忙
- 同比环比分析报表问题
- 读取xml的时候报DTD出错,请教高人
- JAVA编写资源管理器
- 单行子查询返回多个行
- 关于连接池的使用!
- java程序员(会struts、hibernate、ajax、spring等)要对数据库掌握很多吗,如果要掌握,一般要掌握到什么程度呢,那些方面?
- hibernate需要的jar包有哪些?
if(listcart.contains(bean.getId()))
这个判断有问题么?listcart 里面存在的都是实体类,你这是不是在问它是否包含ID?假如ID=1
那将是这样:if(listcart.contains(3)),看看吧,我就感觉这有点问题。
还有就是可能判断出错了
各位大虾能提出个解决方案麽??!
if(listcart==null)
{
listcart=new ArrayList<CartItemsBean>();
}每次执行这里 都被重新创建了 要怎么改?? 指教 指教!!
if(listcart.contains(bean.getId()))
{
cartitems.setQuantity(cartitems.getQuantity()+1);
}你这里的判断好像也错了。
listcart 存的是CartItemsBean这个对象,但是你判断是否存在却用的bean.getId()这样肯定是不存在的。
可以将listcart放入用户session中,从session 获取listcart
改为从seesion里面取。
HttpSession session =request.getSession();
session.setAttribute("listuser",user);
你用hibernate了吧?
private ShoppingBean bean=new ShoppingBean();//提交后数据都保存在beans中
private CartItemsBean cartitems=new CartItemsBean();
private HttpServletRequest request;
public String cart()
{
HttpSession session=request.getSession(false);
//从购物车中取出书籍
List<CartItemsBean> listcart=(List<CartItemsBean>)session.getAttribute("listcart");//作为购物车的集合
if(listcart==null)
{
listcart=new ArrayList<CartItemsBean>();
session.setAttribute("listcart", listcart);
}
..................
return SUCCESS;
}是这样么? 那如何判断购物车中是否存在该书籍??
如果不想用循环比较的话,你可以重写CartItemsBean的equals方法,根据Id判断2个对象是否相等
然后调用的list的indexOf(Object o)方法,获取该对象的索引,判断list中是否存在该本书
@Override
public boolean equals(Object obj) {
if(this == obj){
return true;
}
if(obj == null){
return false;
}
if(!(obj instanceof CartItemsBean)){
return false;
}
CartItemsBean bean = (CartItemsBean)obj;
return (this.getId() != null && bean.getId() != null
&& this.getId().equals(bean.getId())) ||
(this.getId() == bean.getId());
}
}if(listcart.index(cartitems) != -1) {
// 已存在
}
List<CartItemsBean> cartBean=(List<CartItemsBean>) session.getAttribute("listcart");
cy_gogo 的方法蛮好的,一般都那样做的 就是你在action中写的就从session中去取在判断是否存在时
就现在实体类中重写一下equals方法 连代码都写给你了然后只要在action中调用该方法进行判断就可以了
public class TbShopInfoEntity implements java.io.Serializable { // Fields private Integer id; private String shopName; private Float price;
}
/**
* 商品项,多数量
*/
public class ShopItem {
private TbShopInfoEntity shop; private int quantity; public ShopItem() {
super();
} // 第一次购买这本书
// 构造函数,用于书籍信息的初始化,并把书籍数量初始化为1
public ShopItem(TbShopInfoEntity t) {
this.shop = t;
this.quantity = 1;
} // 重复购买同一本书
public void addQuantity() {
++quantity;
} public void subQuantity() {
--quantity;
}
}/**
* 购物车的具体操作类 还可以保存订购书籍信息(多种类)
*/
public class ShopCarUtil { private int total; private float totalPrice; /**
* 保存书籍 map key :bookId map value : BookItem对象 例如:map.put(bookid, new
* BookItem());
*/
private Map map = new HashMap(); /**
* 添加书籍信息
* 用于第一次和重复订购时使用
*/
public void addBook(TbShopInfoEntity shop) {
// 通过书籍ID从map中查找是否有相同的书籍
ShopItem item = (ShopItem) map.get(shop.getId());
if (item == null) {
// 第一次购买
map.put(shop.getId(), new ShopItem(shop));
} else {
// 重复购买
item.addQuantity();
}
this.total++;
} /**
* 清空购物车
*/
public void remAll() {
if (map != null) {
map.clear();
}
this.total = 0;
} /**
* 移除一件商品
* @param id
*/
public void rem(Integer id) {
ShopItem item = (ShopItem) this.map.get(id);
if (item != null) {
map.remove(id);
this.total -= item.getQuantity();
}
}
/**
* 重复添加商品
* @param id
*/
public void add(Integer id) {
ShopItem item = (ShopItem) this.map.get(id);
if (item != null) {
item.addQuantity();
this.total++;
}
}
/**
* 修改数量
* @param id
* @param quty
*/
public void update(Integer id, int quty) {
ShopItem item = (ShopItem) this.getMap().get(id);
if(item != null) {
int temp = quty - item.getQuantity();
item.setQuantity(quty);
total += temp;
}
} /**
* 减少商品数量
* @param id
*/
public void sub(Integer id) {
ShopItem item = (ShopItem) this.map.get(id);
if (item.getQuantity() == 1) {
this.rem(id);
} else {
item.subQuantity();
this.total--;
}
}
/**
* 计算商品总价
* @return
*/
public float getTotalPrice() {
float p = 0.0f;
Iterator it = this.getMap().values().iterator();
ShopItem item = null;
while (it.hasNext()) {
item = (ShopItem) it.next();
p += item.getQuantity() * item.getShop().getPrice();
}
return p;
}
}
/**
* Servlet 你可以改成Action,改法你应该会
*/
public class ShopCarServlet extends HttpServlet { protected void service(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
String type = req.getParameter("type");
if ("addBook".equals(type)) { //添加商品
Integer id = Integer.valueOf(req.getParameter("bid"));
TbShopInfoEntity shop = new ShopInfoDAO().findById(id);
this.makeCar(req).addBook(shop);
} else if ("remAll".equals(type)) { //移除所有
this.makeCar(req).remAll();
} else if ("rem".equals(type)) { //移除一个
Integer id = Integer.valueOf(req.getParameter("bid"));
this.makeCar(req).rem(id);
} else if ("add".equals(type)) { //重复添加
Integer id = Integer.valueOf(req.getParameter("bid"));
this.makeCar(req).add(id);
} else if ("sub".equals(type)) { //减少商品
Integer id = Integer.valueOf(req.getParameter("bid"));
this.makeCar(req).sub(id);
} else if ("update".equals(type)) { //修改商品
Integer id = Integer.valueOf(req.getParameter("bid"));
int quty = Integer.parseInt(req.getParameter("quty").toString());
this.makeCar(req).update(id, quty);
}
// 跳转
res.sendRedirect("xxx.jsp");
} /*
* 是一个session的初始化方法 1. 为程序返回ShopCarUtil 2. 为了创建session会话
*/
public ShopCarUtil makeCar(HttpServletRequest req) {
HttpSession session = req.getSession();
// 先获得会话中的car对象
ShopCarUtil shopCar = (ShopCarUtil) session.getAttribute("car");
// 创建购物车(在第一次添加购物车时使用)
if (shopCar == null) {
shopCar = new ShopCarUtil();
session.setAttribute("car", shopCar);
return shopCar;
} else { // 当重复使用购物车时
return shopCar;
}
}
}