spring的例子jpetstore,是怎样创建购物车的?请高手解答!200分相送

挑大梁 2006-11-15 06:25:52
如题:

看了jpetstore用struts做control例子,不知道是在哪为不同用户
(即使没有登录的用户)创建的购物车。向购物车量加商品我知道。
请解答是在哪或什么机制创建的购物车,最好贴出详细代码,给予明示。

以下为AddItemToCartAction代码:
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
CartActionForm cartForm = (CartActionForm) form;
Cart cart = cartForm.getCart();
String workingItemId = cartForm.getWorkingItemId();
if (cart.containsItemId(workingItemId)) {
cart.incrementQuantityByItemId(workingItemId);
}
else {
// isInStock is a "real-time" property that must be updated
// every time an item is added to the cart, even if other
// item details are cached.
boolean isInStock = getPetStore().isItemInStock(workingItemId);
Item item = getPetStore().getItem(workingItemId);
cartForm.getCart().addItem(item, isInStock);
}
return mapping.findForward("success");
}

cart.java文件

public class Cart implements Serializable {

/* Private Fields */

private final Map itemMap = Collections.synchronizedMap(new HashMap());

private final PagedListHolder itemList = new PagedListHolder();

/* JavaBeans Properties */

public Cart() {
this.itemList.setPageSize(4);
}

public Iterator getAllCartItems() { return itemList.getSource().iterator(); }
public PagedListHolder getCartItemList() { return itemList; }
public int getNumberOfItems() { return itemList.getSource().size(); }

/* Public Methods */

public boolean containsItemId(String itemId) {
System.out.println("itemMap----> "+itemMap);
return itemMap.containsKey(itemId);
}

public void addItem(Item item, boolean isInStock) {
CartItem cartItem = (CartItem) itemMap.get(item.getItemId());
if (cartItem == null) {
cartItem = new CartItem();
cartItem.setItem(item);
cartItem.setQuantity(0);
cartItem.setInStock(isInStock);
itemMap.put(item.getItemId(), cartItem);
itemList.getSource().add(cartItem);
}
cartItem.incrementQuantity();
}
...全文
500 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
雪狐 2006-11-29
  • 打赏
  • 举报
回复
学习一下:
挑大梁 2006-11-28
  • 打赏
  • 举报
回复
谢谢各位,有问题大家一同讨论,再允许我稍晚一些结贴
flyIce945 2006-11-28
  • 打赏
  • 举报
回复
@@
guileen 2006-11-23
  • 打赏
  • 举报
回复
楼主我支持你 ----------------- 松自萧萧云自飘 风中独酌亦逍遥 抚却凡愁与尘念 琴韵未解恨已销
yaospring 2006-11-23
  • 打赏
  • 举报
回复
Caused by: com.ibatis.dao.client.DaoException: Failed to execute queryForObject - id [getItem], parameterObject [EST-4]. Cause: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/ibatis/jpetstore/persistence/sqlmapdao/sql/Item.xml.
--- The error occurred while applying a parameter map.
--- Check the getItem-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'ITEMID' in field list is ambiguous
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'ITEMID' in field list is ambiguous
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/ibatis/jpetstore/persistence/sqlmapdao/sql/Item.xml.
--- The error occurred while applying a parameter map.
--- Check the getItem-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'ITEMID' in field list is ambiguous
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'ITEMID' in field list is ambiguous
at com.ibatis.dao.client.template.SqlMapDaoTemplate.queryForObject(SqlMapDaoTemplate.java:164)
at com.ibatis.jpetstore.persistence.sqlmapdao.ItemSqlMapDao.getItem(ItemSqlMapDao.java:42)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.ibatis.dao.engine.impl.DaoProxy.invoke(DaoProxy.java:72)
at $Proxy0.getItem(Unknown Source)
at com.ibatis.jpetstore.service.CatalogService.getItem(CatalogService.java:59)
at com.ibatis.jpetstore.presentation.CartBean.addItemToCart(CartBean.java:60)
... 39 more
Caused by: com.ibatis.common.jdbc.exception.NestedSQLException:
--- The error occurred in com/ibatis/jpetstore/persistence/sqlmapdao/sql/Item.xml.
--- The error occurred while applying a parameter map.
--- Check the getItem-InlineParameterMap.
--- Check the statement (query failed).
--- Cause: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'ITEMID' in field list is ambiguous
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'ITEMID' in field list is ambiguous
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:185)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryForObject(GeneralStatement.java:104)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:561)
at com.ibatis.sqlmap.engine.impl.SqlMapExecutorDelegate.queryForObject(SqlMapExecutorDelegate.java:536)
at com.ibatis.sqlmap.engine.impl.SqlMapSessionImpl.queryForObject(SqlMapSessionImpl.java:93)
at com.ibatis.sqlmap.engine.impl.SqlMapClientImpl.queryForObject(SqlMapClientImpl.java:70)
at com.ibatis.dao.client.template.SqlMapDaoTemplate.queryForObject(SqlMapDaoTemplate.java:162)
... 48 more
Caused by: com.mysql.jdbc.exceptions.MySQLIntegrityConstraintViolationException: Column 'ITEMID' in field list is ambiguous
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:931)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2870)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:1665)
at com.mysql.jdbc.Connection.execSQL(Connection.java:3124)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1149)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:790)
at com.ibatis.sqlmap.engine.execution.SqlExecutor.executeQuery(SqlExecutor.java:180)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.sqlExecuteQuery(GeneralStatement.java:205)
at com.ibatis.sqlmap.engine.mapping.statement.GeneralStatement.executeQueryWithCallback(GeneralStatement.java:173)
... 54 more

yaospring 2006-11-23
  • 打赏
  • 举报
回复
(StandardValveContext.java:104)
at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:520)
at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:929)
at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:160)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:799)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:705)
at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:577)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:534)
Caused by: org.apache.struts.beanaction.BeanActionException: Error invoking Action. Cause: java.lang.reflect.InvocationTargetException
at org.apache.struts.beanaction.ActionInvoker.invoke(ActionInvoker.java:18)
at org.apache.struts.beanaction.DefaultActionInterceptor.intercept(DefaultActionInterceptor.java:5)
at org.apache.struts.beanaction.BeanAction.execute(BeanAction.java:246)
... 32 more
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at org.apache.struts.beanaction.ActionInvoker.invoke(ActionInvoker.java:16)
... 34 more
yaospring 2006-11-23
  • 打赏
  • 举报
回复
我在tomcat下部署jpetstore的时候出现问题,大家帮我分析一下吧,就是在加入购物车这一步,浏览器出现异常:
吐司vivi 2006-11-16
  • 打赏
  • 举报
回复
学习。。。。。。。。。。
挑大梁 2006-11-16
  • 打赏
  • 举报
回复
那struts创建session的机制是怎样的,它是怎样为不同的购物车创建不同的session呢,
jobs002 2006-11-16
  • 打赏
  • 举报
回复
就是同一个购物车,放入东西就可以了.Session不同的客户端是不一样的
jobs002 2006-11-16
  • 打赏
  • 举报
回复
放入了session,不过我的Session放在了index.html里面.
hbwhwang 2006-11-16
  • 打赏
  • 举报
回复
actionform在session范围内,无论第一次请求,还是第二次请求,只要这两个请求处于同一个HTTP会话中,这些web组件就始终共用同一个ActionForm实例
==========
对了!
只要是同一个session,就始终用这个ActionForm实例
挑大梁 2006-11-16
  • 打赏
  • 举报
回复
感谢hbwhwang(catmiw的ID已经停用,现在用这个)

明白了,马上结贴。
挑大梁 2006-11-16
  • 打赏
  • 举报
回复
actionform在session范围内,无论第一次请求,还是第二次请求,只要这两个请求处于同一个HTTP会话中,这些web组件就始终共用同一个ActionForm实例
hbwhwang 2006-11-16
  • 打赏
  • 举报
回复
那struts创建session的机制是怎样的,它是怎样为不同的购物车创建不同的session呢
====
1、session不是被struts管理的,而是被servlet容器管理的。
2、当某浏览器连上服务器的时候,容器会根据request的Cookie判断这个连接是新的还是旧的。如果是新的,那么创建session,并在response的head中设置名为JSESSIONID的Cookie。
hbwhwang 2006-11-15
  • 打赏
  • 举报
回复
我不知道我的jpetstore跟你是不是一个版本。我的是这样的:
1、在struts-config.xml中定义了cartBean。
<form-bean name="cartBean" type="com.ibatis.jpetstore.presentation.CartBean"/>
2、这个formbean被3个action用到了。
<!-- CART ACTIONS -->

<action path="/shop/addItemToCart" type="org.apache.struts.beanaction.BeanAction"
name="cartBean" scope="session"
validate="false">
<forward name="success" path="/cart/Cart.jsp"/>
</action>

<action path="/shop/removeItemFromCart" type="org.apache.struts.beanaction.BeanAction"
name="cartBean" scope="session"
validate="false">
<forward name="success" path="/cart/Cart.jsp"/>
</action>

<action path="/shop/updateCartQuantities" type="org.apache.struts.beanaction.BeanAction"
name="cartBean" scope="session"
validate="false">
<forward name="success" path="/cart/Cart.jsp"/>
</action>

请注意:3个action的bean的scope都是session。
3、CartBean有一个数据成员Cart cart:
public class CartBean extends AbstractBean {
private Cart cart = new Cart();
... ...
}

整个过程是这样的:
当有任何请求访问到这3个Action(任何一个)的时候,Struts就会创建一个CartBean对象,并以“cartBean”的名字放到session中。而这个CartBean对象中就包括一个Cart。

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧