日期:2014-05-19 浏览次数:20841 次
<s:iterator value="cartItems"> <tr class='td_no_bord'> <td> <a href="#"><s:property value="#item.pro.productName" /></a> </td> <td class="buy_td_5"> <span class="c_gray"><s:property value="%{item.pro.fixedPrice}"/></span> </td> <td class="buy_td_4"> <span><s:property value="%{item.pro.dangPrice}"/></span> </td> </tr> </s:iterator>
================BuyAction.java(在struts.xml中有定义,根据私有属性获取值)====================== public class BuyAction extends BaseAction{ //input private int id; //output private List<CartItem> cartItems; private CartItem item; private boolean hasBook; ProductDAO pd = new JdbcProductDAO(); CartBean cb = new CartBean(); public String addBook(){ item = cb.buy(id);// <----调用CartBean.java的buy()方法 if(cartItems==null){ cartItems = new ArrayList<CartItem>(); } if(cartItems.isEmpty()){ hasBook = false; cartItems.add(0, item); return "addInCart"; } ====================CartBean.java====================== public class CartBean extends BaseAction implements Cart{ private CartItem item; public CartItem buy(int bid) { //根据商品pid,获取商品对象,封装成CartItem ProductDAO pd = new JdbcProductDAO(); Book book = new Book(); try { book = pd.getAbook(bid);//能够顺利通过JDBC获取数据库存储的数据 item = new CartItem(book,1,true); } catch (SQLException e) { e.printStackTrace(); } return item; }