页面采用回调函数function(data) 处理后台返回的结果
a标签onclick事件触发
<a href ="javascript:void(0);" class="btn btn-default" id ="add" onclick = "return addproduct(${s.id});">加入秒杀</a>
前台
function addproduct(id){ var mprice=document.getElementById("mprice_"+id).value; var number=document.getElementById("number_"+id).value; var sid=document.getElementById("special.id").value; if (mprice==""){ alert("请输入特价价格"); return false; }else if (number==""){ alert("请输入特价数量 "); return false; }else { //重点在这儿 $.get("${ctx}/special/addProduct.action?specialVo.quantity="+number+"&specialVo.memberPrice="+mprice+"&specialVo.id="+id+"&special.id="+sid, function(data){ if(data=="true"){ alert("添加成功"); window.location.reload(); } }) /* window.location.href="${ctx}/special/addProduct.action?specialVo.quantity="+number+"&specialVo.memberPrice="+mprice+"&specialVo.id="+id+"&special.id="+sid; */ } }
后台
public void addProduct(){ PrintWriter out=null; try { System.out.println(specialVo.getQuantity()); System.out.println(specialVo.getMemberPrice()); System.out.println(specialVo.getId()); System.out.println(special.getId()); HttpServletResponse response=ServletActionContext.getResponse(); out=response.getWriter(); out.print(true); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); out.flush(); out.close(); out.println(0); } }
struts配置action无需result
<action name="addProduct" class="specialAction" method="addProduct" > </action>
参考文章:
http://bbs.csdn.net/topics/390470284
http://blog.csdn.net/xuzhuang2008/article/details/6928304
本文出自 “一无所有-天行者” 博客,请务必保留此出处http://tianxingzhe.blog.51cto.com/3390077/1678174
原文地址:http://tianxingzhe.blog.51cto.com/3390077/1678174