先看看效果:
结算
list.jsp
<a href="javascript:jiesuan();" id="jiesuan" class="jiesuan"></a>
<form id="jiesuanform" action="<c:url value='/cartitemservlet'/>" method="post">
<input type="hidden" name="cartitemids" id="cartitemids"/>
<input type="hidden" name="total" id="hiddentotal"/>
<input type="hidden" name="method" value="loadcartitems"/>
</form>
function jiesuan() {
// 1. 获取所有被选择的条目的id,放到数组中
var cartitemidarray = new array();
$(":checkbox[name=checkboxbtn][checked=true]").each(function() {
cartitemidarray.push($(this).val());//把复选框的值添加到数组中
});
// 2. 把数组的值tostring(),然后赋给表单的cartitemids这个hidden
$("#cartitemids").val(cartitemidarray.tostring());
// 把总计的值,也保存到表单中
$("#hiddentotal").val($("#total").text());
// 3. 提交这个表单
$("#jiesuanform").submit();
}
servlet
public string loadcartitems(httpservletrequest req, httpservletresponse resp)
throws servletexception, ioexception {
/*
* 1. 获取cartitemids参数
*/
string cartitemids = req.getparameter("cartitemids");
double total = double.parsedouble(req.getparameter("total"));
/*
* 2. 通过service得到list<cartitem>
*/
list<cartitem> cartitemlist = cartitemservice.loadcartitems(cartitemids);
/*
* 3. 保存,然后转发到/cart/showitem.jsp
*/
req.setattribute("cartitemlist", cartitemlist);
req.setattribute("total", total);
req.setattribute("cartitemids", cartitemids);
return "f:/jsps/cart/showitem.jsp";
}
dao
加载多个cartitem
public list<cartitem> loadcartitems(string cartitemids) throws sqlexception {
/*
* 1. 把cartitemids转换成数组
*/
object[] cartitemidarray = cartitemids.split(",");
/*
* 2. 生成wehre子句
*/
string wheresql = towheresql(cartitemidarray.length);
/*
* 3. 生成sql语句
*/
string sql = "select * from t_cartitem c, t_book b where c.bid=b.bid and " + wheresql;
/*
* 4. 执行sql,返回list<cartitem>
*/
return tocartitemlist(qr.query(sql, new maplisthandler(), cartitemidarray));
}
showitem.jsp
<c:choose>
<c:when test="${empty cartitemlist }">嘻嘻~</c:when>
<c:otherwise>
<form id="form1" action="<c:url value='/orderservlet'/>" method="post">
<input type="hidden" name="cartitemids" value="${cartitemids }"/>
<input type="hidden" name="method" value="createorder"/>
<table width="95%" align="center" cellpadding="0" cellspacing="0">
<tr bgcolor="#efeae5">
<td width="400px" colspan="5"><span style="font-weight: 900;">生成订单</span></td>
</tr>
<tr align="center">
<td width="10%"> </td>
<td width="50%">图书名称</td>
<td>单价</td>
<td>数量</td>
<td>小计</td>
</tr>
<c:foreach items="${cartitemlist }" var="cartitem">
<tr align="center">
<td align="right">
<a class="linkimage" href="<c:url value='/jsps/book/desc.jsp'/>"><img border="0" width="54" align="top" src="<c:url value='/${cartitem.book.image_b }'/>"/></a>
</td>
<td align="left">
<a href="<c:url value='/jsps/book/desc.jsp'/>"><span>${cartitem.book.bname }</span></a>
</td>
<td>¥${cartitem.book.currprice }</td>
<td>${cartitem.quantity }</td>
<td>
<span class="price_n">¥<span class="subtotal">${cartitem.subtotal }</span></span>
</td>
</tr>
</c:foreach>
<tr>
<td colspan="6" align="right">
<span>总计:</span><span class="price_t">¥<span id="total">${total }</span></span>
</td>
</tr>
<tr>
<td colspan="5" bgcolor="#efeae5"><span style="font-weight: 900">收货地址</span></td>
</tr>
<tr>
<td colspan="6">
<input id="addr" type="text" name="address" value="北京市 昌平区 西三旗 金燕龙办公楼1层 传智播客 张三爷"/>
</td>
</tr>
<tr>
<td style="border-top-width: 4px;" colspan="5" align="right">
<a id="linksubmit" href="javascript:$('#form1').submit();">提交订单</a>
</td>
</tr>
</table>
</form>
</c:otherwise>
</c:choose>