在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}
付款金额:${sessionScope.cart.price}
<a href="javascript:delAll()">清空</a>
<a href="${pageContext.request.contextPath}/servlet/ManagerServlet?operation=showAllProducts">继续采购</a>
<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);
}