jsp页面 将数据以Json 格式保存到数据库
1:jsp页面
<div class="control-group form-group all_activity"> <c:choose> <c:when test="${fullCutList !=null && fullCutList.size() >0}"> <c:forEach items="${fullCutList }" var="fullCut" begin="0" varStatus="status"> <c:set var="num" value="${status.count-1}"></c:set> <div class="activity_class" > <label for="fullMoney" class="col-md-2 control-label" style="width: 50px">满</label> <div class="col-md-1"> <input type="text" id="fullCutList[${num}].fullMoney" name="fullCutList[${num}].fullMoney"value="${fullCut.fullMoney }" class="form-control full_money" style="width: 150%" /> </div> <label for="subtractMoney" class="col-md-2 control-label" style="width: 50px">减</label> <div class="col-md-1"> <input type="text" id="fullCutList[${num}].subtractMoney" name="fullCutList[${num}].subtractMoney"value="${fullCut.subtractMoney }" class="form-control subtract_money" style="width: 150%" /> </div> </div> </c:forEach> </c:when> <c:otherwise> <div class="activity_class" style="margin-top:-26px;"> <label for="fullMoney" class="col-md-2 control-label" style="width: 50px">满</label> <div class="col-md-1"> <input type="text" id="fullCutList[0].fullMoney" name="fullCutList[0].fullMoney"value="" class="form-control full_money" style="width: 150%" /> </div> <label for="subtractMoney" class="col-md-2 control-label" style="width: 50px">减</label> <div class="col-md-1"> <input type="text" id="fullCutList[0].subtractMoney" name="fullCutList[0].subtractMoney"value="" class="form-control subtract_money" style="width: 150%" /> </div> </div> </c:otherwise> </c:choose> </div> <div class="activity_add"> <span class="title_name add_coupon">添加条件</span> <span class="konck_icon konck_h"></span> </div>
下面是js
//点击“加”事件 $(".activity_add").click(function(e) { addFullSubtract();//调用满减优惠动态添加方法 sort();//排序方法 }); //点击“删除”事件 $('body').on('click', '.dell_all', function() { $(this).parent().remove(); sort();//排序方法 }); //满减优惠动态添加方法 function addFullSubtract() { var fullSubtract = '<div class="activity_class">' + '<label for="fullMoney" class="col-md-2 control-label" style="width: 50px">满</label>' + '<div class="col-md-1">' + '<input type="text" value="" class="form-control full_money" style="width: 150%" />' + '</div>' + '<label for="subtractMoney" class="col-md-2 control-label" style="width: 50px">减</label>' + '<div class="col-md-1">' + '<input type="text" value="" class="form-control subtract_money" style="width: 150%" />' + '</div>' + '<div class="dell_all" style="margin-left: 28px;" >' + '<span class="title_name dell_coupon">删除</span>' + '<span class="konck_icon konck_i"></span>' + '</div>' + '</div>'; $(".all_activity").append(fullSubtract); } //排序 var sort = function(){ $(".activity_class").each(function(i){ $(this).find(".full_money").attr("name","fullCutList["+i+"].fullMoney"); $(this).find(".subtract_money").attr("name","fullCutList["+i+"].subtractMoney"); }); }
下面是java 后台代码
/** * 保存 */ @Override public String execute() throws Exception { JSONObject result = new JSONObject(); try { List<FullCut> fullCutList = activityFullSubtractParams.getFullCutList(); Stall stall = stallService.load(activityFullSubtractParams.getStall().getId()); activityFullSubtract = new ActivityFullSubtract(); BeanUtils.copyProperties(activityFullSubtractParams, activityFullSubtract); if (fullCutList != null && fullCutList.size() > 0) { Object fullCutJson = JSON.toJSON(fullCutList); activityFullSubtract.setConditions(fullCutJson.toString());// 满多少减多少,以JSON格式保存 } activityFullSubtract.setStall(stall); //判断是否合法新建活动 boolean whether = activityStallService.verificationActivity(activityFullSubtract); if(whether){ result.put("success", "保存失败。该档口在在当前有效日期内已经存在一个活动了"); }else{ //保存数据前刷新一下 String url = orderDomain+"/activity/refreshActivity?stallId="+activityFullSubtract.getStall().getId(); String data = HttpClientUtil.getCloseableHttpResponse(url); System.out.println(data); activityFullSubtractService.addActivityFullSubtract(activityFullSubtract);// 保存 result.put("success", "保存成功"); } } catch (Exception e) { e.printStackTrace(); } Struts2Util.renderJson(getResponse(), result.toString()); return null; }