EASONLEELEE 2015-05-23 00:22 采纳率: 22.2%
浏览 2961

JSP购物车功能不能修改数量

在servlet做了修改数量的几个验证,原本修改为正确的数量值都是没问题的,但现在不知道怎么回事,突然就不可以了,修改数量后,弹出是否确认修改的窗口,点击确认后,值还是之前的值,发现并没有把值提交给servlet,。后天要交,好急呀。。以下是代码:
第一块代码块是jsp的修改页面,数量修改后,提交到servlet

 <form action="${pageContext.request.contextPath}/servlet/ManagerServlet?operation=addDetails " method="post" class="form-horizontal">
 <div style="padding: 10px 200px 10px;">
    <c:if test="${!empty sessionScope.cart.items}">
        <table class="table table-hover" width="1024">
            <thead>
                <tr>
                    <th>序号</th>
                    <th>商品编码</th>
                    <th>商品全称</th>
                    <th>品牌</th>
                    <th>计量单位</th>
                    <th>数量</th>
                    <th>单价</th>
                    <th>总价</th>
                    <th>操作</th>
                </tr>
            </thead>

            <c:forEach items="${sessionScope.cart.items}" var="me" varStatus="vs">
                <tr>
                    <td>${vs.count}</td>
                    <td>${me.value.product.code}</td>
                    <td>${me.value.product.name}</td>
                    <td>${me.value.product.brand}</td>
                    <td>${me.value.product.unit}</td>
                    <td>
                    <input type="text" name="num" value="${me.value.num}" size="1" onchange="changeNum(this,'${me.key}',${me.value.num})"/></td>
                    <td>${me.value.product.outprice}</td>
                    <td>${me.value.price}</td>
                    <td>
                        <a href="javascript:delOne('${me.key}')">移除</a>
                    </td>
                </tr>
            </c:forEach>
        </table>
        <p style="text-align:right">
                    总数量:${sessionScope.cart.num}&nbsp;&nbsp;
                    付款金额:${sessionScope.cart.price}&nbsp;&nbsp;
                    <a href="javascript:delAll()">清空</a>&nbsp;&nbsp;
                    <a href="${pageContext.request.contextPath}/servlet/ManagerServlet?operation=showAllProducts">继续采购</a>&nbsp;&nbsp;
                    <a href="${pageContext.request.contextPath}/servlet/ManagerServlet?operation=genOrders">生成订单</a>
        </p>
    </c:if>

    </div>
    </form>
        <script type="text/javascript">
            function changeNum(inputObj,productId,oldNum){
                var newnum = inputObj.value;//新的数量
                var sure = window.confirm("确定要把数量改为"+newnum+"吗?");//返回值 :点确定就返回true
                if(sure){
                    //alert("提交服务器去处理");
                    window.location.href="${pageContext.request.contextPath}/servlet/ManagerServlet?operation=ChangeNumServlet&productId="+productId+"&newnum="+newnum;
                }else{
                    //改回原来的值
                    inputObj.value=oldNum;
                }
            }
            //删除一个购物项
            function delOne(productId){
                var sure = window.confirm("确定要删除该项吗?");
                if(sure){
                    window.location.href="${pageContext.request.contextPath}/servlet/ManagerServlet?operation=DelOneCartItemServlet&productId="+productId;
                }
            }
            function delAll(){
                var sure = window.confirm("确定要清空所有采购项吗?");
                if(sure){
                    window.location.href="${pageContext.request.contextPath}/servlet/ManagerServlet?operation=DelAllCartItemServlet";
                }
            }
        </script>

servlet如下:

    //修改采购项的数量
    private void ChangeNumServlet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            String productId = request.getParameter("productId");
            String newnum = request.getParameter("newnum");

            //找出购物车
            Cart cart = (Cart)request.getSession().getAttribute("cart");
            int num = Integer.parseInt(newnum);
            System.out.println("num=="+num);
            System.out.println("id=="+productId);
            if(num<1){
                request.setAttribute("message","<script type='text/javascript'>alert('请输入正确的数量值');</script>" );
            }else{
            //修改数量
            s.changeCartItemNum(productId,cart,num);
            System.out.println("hhhhhh");
            }
        } catch (NumberFormatException e) {
            request.setAttribute("message","<script type='text/javascript'>alert('请输入正确的数字');</script>" );
        }
        //转发到显示购物车的页面
        request.getRequestDispatcher("/manager/showCart.jsp").forward(request, response);

    }
  • 写回答

2条回答 默认 最新

  • 逝_去_的_光_阴 2015-05-23 01:56
    关注

    数量判断为什么不写在页面吗?
    为什么改数量还要确认?如果有个加1减1按钮,我要10个从1点到10,你就弹9次确认吗?就去后台9次改9次sql?
    s.changeCartItemNum(productId,cart,num);检查这里写的有没有问题

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料