java 我想问各位我做的购物车怎么在jsp页面取不到值是不是那里出了点问题,请指教 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 //学的太差写的太乱了,请见谅 //这是计算总价格public double totol(){ double amount = 0.0f; int quantity = 0; double unitprice = 0.0f; Iterator t = listItem.iterator(); while(t.hasNext()){ ShopDTO cartItem = (ShopDTO)t.next(); unitprice = cartItem.getShop().getPrice(); quantity = cartItem.getNum(); amount += unitprice * quantity; } return amount; }//保存购物车 public void buy(HttpServletRequest request,HttpServletResponse response){ HttpSession session = request.getSession(); if (session.getAttribute("map") == null) { session.setAttribute("map", new HashMap()); } int id = Integer.parseInt(request.getParameter("shop")); HashMap map = (HashMap) session.getAttribute("map"); if (map.containsKey(id)) { ShopDTO dto = (ShopDTO) map.get(id); dto.setNum(dto.getNum() + 1); map.put(id, dto); } else { shopDAO dao = new shopDAO(); ShopPO po = dao.getById(id); ShopDTO dto = new ShopDTO(); dto.setNum(1); map.put(id, dto); listItem.add(dto); } double subTotal=this.totol(); request.getSession().setAttribute("total", subTotal); session.setAttribute("map", map); session.setAttribute("listItem", listItem); try { request.getRequestDispatcher("/buyShop.jsp").forward(request,response); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } }//读取购物车</body><% List list = (List) request.getSession().getAttribute("listItem"); out.print("您的购物车有如下商品:<br>"); out.print("<div style='text-align:center'><label>商品名</label><label>单价</label><label>购买数量</label><label>总价</label><label>删除</label>"); for (int i = 0; i < list.size(); i++) { ShopDTO dto = (ShopDTO) list.get(i); %> <table border=1> <tr> <td> <%=dto.getShop().getShopname()%> </td> <td> ¥ <%=dto.getShop().getPrice()%> </td> <td> <input type="text" name="num" value="<%=dto.getNum()%>"> </td> <td> ¥ <%=dto.getTotalPrice()%> </td> <td> <input value="删除" onclick="delete()"> </td> </tr> </table> <% } %> <div align="center"> <label style="background-color: #aaaedd;width:150px;height:30px;font-size:16px;"> 您的总金额为:¥ </label> <label id="labelTotal" style="background-color: #aaaedd;width:80px;height:30px;font: italic ;color: #0000FF;font-size:20px;"> <% Double str = Double.parseDouble(request.getSession().getAttribute( "total").toString()); out.print(str.toString()); %> </label> <br /> <br /> </div> </body> List list = (List) request.getSession().getAttribute("listItem"); out.println(list.size()); // 增加一句如果输出为0, 你去看看你的session.setAttribute("listItem", listItem);System.out.println(listItem.size()); // 增加一句这一句怎么回事吧如果这里也是0,甚至根本没到这里呵呵! 我不说了! 这里啊第一次运行是可以取去来的,但刷新以后又恢复到了以前的样子了。请高手指教怎么回事啊private List listItem = new ArrayList(); //这是计算总价格 public double totol(){ double amount = 0.0f; int quantity = 0; double unitprice = 0.0f; Iterator t = listItem.iterator(); while(t.hasNext()){ ShopDTO cartItem = (ShopDTO)t.next(); unitprice = cartItem.getShop().getPrice(); quantity = cartItem.getNum(); amount += unitprice * quantity; } return amount; } //保存购物车 public void buy(HttpServletRequest request,HttpServletResponse response){ HttpSession session = request.getSession(); if (session.getAttribute("map") == null) { session.setAttribute("map", new HashMap()); } int id = Integer.parseInt(request.getParameter("shop")); HashMap map = (HashMap) session.getAttribute("map"); if (map.containsKey(id)) { ShopDTO dto = (ShopDTO) map.get(id); dto.setNum(dto.getNum() + 1); map.put(id, dto); } else { shopDAO dao = new shopDAO(); ShopPO po = dao.getById(id); ShopDTO dto = new ShopDTO(); dto.setNum(1); map.put(id, dto); listItem.add(dto); } double subTotal=this.totol(); request.getSession().setAttribute("total", subTotal); session.setAttribute("map", map); session.setAttribute("listItem", listItem); try { request.getRequestDispatcher("/buyShop.jsp").forward(request,response); } catch (Exception e) { // TODO 自动生成 catch 块 e.printStackTrace(); } } //读取购物车 </body> <% List list = (List) request.getSession().getAttribute("listItem"); out.print("您的购物车有如下商品: <br> "); out.print(" <div style='text-align:center'> <label> 商品名 </label> <label> 单价 </label> <label> 购买数量 </label> <label> 总价 </label> <label> 删除 </label> "); for (int i = 0; i < list.size(); i++) { ShopDTO dto = (ShopDTO) list.get(i); %> <table border=1> <tr> <td> <%=dto.getShop().getShopname()%> </td> <td> ¥ <%=dto.getShop().getPrice()%> </td> <td> <input type="text" name="num" value=" <%=dto.getNum()%> "> </td> <td> ¥ <%=dto.getTotalPrice()%> </td> <td> <input value="删除" onclick="delete()"> </td> </tr> </table> <% } %> <div align="center"> <label style="background-color: #aaaedd;width:150px;height:30px;font-size:16px;"> 您的总金额为:¥ </label> <label id="labelTotal" style="background-color: #aaaedd;width:80px;height:30px;font: italic ;color: #0000FF;font-size:20px;"> <% Double str = Double.parseDouble(request.getSession().getAttribute( "total").toString()); out.print(str.toString()); %> </label> <br /> <br /> </div> listItem 这个变量有问题,你是把购物车里的货物用 listItem 来保存,并且存到了Session中;但你每次在保存购物车的时候,忽略了购物车原本的listItem 数据,也就要增加一句 session.getAttribute("listItem"); 否则,你每次都是new 一个空的listItem对象;再有map存储的好像也是购物车里的商品信息吧?我就是不清楚商品信息为什么要用map和listItem都存一遍呢? 因为我要计算总价,map在这里迭代不出啊,所以我要加到list里面去啊 各位高手,有谁参加过国信蓝点杯的比赛,给我传点经验,。谢谢 关于乱码的问题。 到底怎么回事 当DataInputStream 的readline方法不可用,怎样一行行读取数据? 小弟 初学Java 那位大哥帮帮忙啊!!!!!!! MD5 加密算法求助。 快帮帮忙吧,急!!!!!关于抓屏问题 大送分了!请看! weblogic生成的java放在哪里 啊。 java 串口 内存溢出 如何获取一组随机的,以屏幕为中心均匀分布的坐标组 在线等待,工作流问题,着急!!!!!!!!
//这是计算总价格
public double totol(){
double amount = 0.0f;
int quantity = 0;
double unitprice = 0.0f;
Iterator t = listItem.iterator();
while(t.hasNext()){
ShopDTO cartItem = (ShopDTO)t.next();
unitprice = cartItem.getShop().getPrice();
quantity = cartItem.getNum();
amount += unitprice * quantity;
}
return amount;
}//保存购物车
public void buy(HttpServletRequest request,HttpServletResponse response){
HttpSession session = request.getSession();
if (session.getAttribute("map") == null) {
session.setAttribute("map", new HashMap());
} int id = Integer.parseInt(request.getParameter("shop")); HashMap map = (HashMap) session.getAttribute("map");
if (map.containsKey(id)) {
ShopDTO dto = (ShopDTO) map.get(id);
dto.setNum(dto.getNum() + 1);
map.put(id, dto);
} else {
shopDAO dao = new shopDAO();
ShopPO po = dao.getById(id);
ShopDTO dto = new ShopDTO();
dto.setNum(1);
map.put(id, dto);
listItem.add(dto);
}
double subTotal=this.totol();
request.getSession().setAttribute("total", subTotal);
session.setAttribute("map", map);
session.setAttribute("listItem", listItem);
try {
request.getRequestDispatcher("/buyShop.jsp").forward(request,response);
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}//读取购物车
</body>
<%
List list = (List) request.getSession().getAttribute("listItem");
out.print("您的购物车有如下商品:<br>");
out.print("<div style='text-align:center'><label>商品名</label><label>单价</label><label>购买数量</label><label>总价</label><label>删除</label>");
for (int i = 0; i < list.size(); i++) {
ShopDTO dto = (ShopDTO) list.get(i);
%>
<table border=1>
<tr>
<td>
<%=dto.getShop().getShopname()%>
</td>
<td>
¥
<%=dto.getShop().getPrice()%>
</td>
<td>
<input type="text" name="num" value="<%=dto.getNum()%>">
</td>
<td>
¥
<%=dto.getTotalPrice()%>
</td>
<td>
<input value="删除" onclick="delete()">
</td>
</tr> </table>
<%
}
%>
<div align="center">
<label
style="background-color: #aaaedd;width:150px;height:30px;font-size:16px;">
您的总金额为:¥
</label>
<label id="labelTotal"
style="background-color: #aaaedd;width:80px;height:30px;font: italic ;color: #0000FF;font-size:20px;">
<%
Double str = Double.parseDouble(request.getSession().getAttribute(
"total").toString());
out.print(str.toString());
%>
</label>
<br />
<br />
</div> </body>
out.println(list.size()); // 增加一句如果输出为0, 你去看看你的
session.setAttribute("listItem", listItem);
System.out.println(listItem.size()); // 增加一句这一句怎么回事吧如果这里也是0,甚至根本没到这里呵呵! 我不说了!
第一次运行是可以取去来的,但刷新以后又恢复到了以前的样子了。请高手指教怎么回事啊private List listItem = new ArrayList();
//这是计算总价格
public double totol(){
double amount = 0.0f;
int quantity = 0;
double unitprice = 0.0f;
Iterator t = listItem.iterator();
while(t.hasNext()){
ShopDTO cartItem = (ShopDTO)t.next();
unitprice = cartItem.getShop().getPrice();
quantity = cartItem.getNum();
amount += unitprice * quantity;
}
return amount;
} //保存购物车
public void buy(HttpServletRequest request,HttpServletResponse response){
HttpSession session = request.getSession();
if (session.getAttribute("map") == null) {
session.setAttribute("map", new HashMap());
} int id = Integer.parseInt(request.getParameter("shop")); HashMap map = (HashMap) session.getAttribute("map");
if (map.containsKey(id)) {
ShopDTO dto = (ShopDTO) map.get(id);
dto.setNum(dto.getNum() + 1);
map.put(id, dto); } else {
shopDAO dao = new shopDAO();
ShopPO po = dao.getById(id);
ShopDTO dto = new ShopDTO();
dto.setNum(1);
map.put(id, dto);
listItem.add(dto); } double subTotal=this.totol();
request.getSession().setAttribute("total", subTotal);
session.setAttribute("map", map);
session.setAttribute("listItem", listItem);
try {
request.getRequestDispatcher("/buyShop.jsp").forward(request,response);
} catch (Exception e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
} //读取购物车
</body>
<%
List list = (List) request.getSession().getAttribute("listItem");
out.print("您的购物车有如下商品: <br> ");
out.print(" <div style='text-align:center'> <label> 商品名 </label> <label> 单价 </label> <label> 购买数量 </label> <label> 总价 </label> <label> 删除 </label> ");
for (int i = 0; i < list.size(); i++) {
ShopDTO dto = (ShopDTO) list.get(i);
%>
<table border=1>
<tr>
<td>
<%=dto.getShop().getShopname()%>
</td>
<td>
¥
<%=dto.getShop().getPrice()%>
</td>
<td>
<input type="text" name="num" value=" <%=dto.getNum()%> ">
</td>
<td>
¥
<%=dto.getTotalPrice()%>
</td>
<td>
<input value="删除" onclick="delete()">
</td>
</tr> </table>
<%
}
%>
<div align="center">
<label
style="background-color: #aaaedd;width:150px;height:30px;font-size:16px;">
您的总金额为:¥
</label>
<label id="labelTotal"
style="background-color: #aaaedd;width:80px;height:30px;font: italic ;color: #0000FF;font-size:20px;">
<%
Double str = Double.parseDouble(request.getSession().getAttribute(
"total").toString());
out.print(str.toString());
%>
</label>
<br />
<br />
</div>
但你每次在保存购物车的时候,忽略了购物车原本的listItem 数据,也就要增加一句 session.getAttribute("listItem");
否则,你每次都是new 一个空的listItem对象;再有map存储的好像也是购物车里的商品信息吧?我就是不清楚商品信息为什么要用map和listItem都存一遍呢?