前台实现将复选框的值保存到数组中,后台用list进行传值,mybatis中用foreach遍历循环添加或更新.
前台jsp代码:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jquery-easyui-1.5.1/themes/default/easyui.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jquery-easyui-1.5.1/themes/icon.css">
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/jquery-easyui-1.5.1/demo.css">
<script type="text/javascript" src="<%=request.getContextPath() %>/jquery-easyui-1.5.1/jquery.min.js"></script>
<script type="text/javascript" src="<%=request.getContextPath() %>/jquery-easyui-1.5.1/jquery.easyui.min.js"></script>
<title>安馨之家</title>
<script>
$(function(){
/* //加载数据库的大礼包信息
$.ajax({
}) */
//表单提交
$("#commit").click(function(){
//alert("方法调用")
//将数据收集到数组中
var codes = new Array();
$("input[name='code']").each(function(){
codes.push($(this).val());
})
var names = new Array();
$("input[name='name']").each(function(){
names.push($(this).val());
})
var counts = new Array();
$("input[name='count']").each(function(){
counts.push($(this).val());
})
var prices = new Array();
$("input[name='price']").each(function(){
prices.push($(this).val());
})
var reasons = new Array();
$("input[name='reason']").each(function(){
reasons.push($(this).val());
})
//将所有数据放入整个数组
var list = new Array();//存放对象的数组
for(var i=0;i<codes.length;i++){
/* alert("codes[i]"+codes[i]); */
//过滤条件根据需求自行修改
if(codes[i]!=""){ //为填数据为空字符串,此处过滤条件开启后,可实现只填入有用数据,不会将所有数据都保存
var prize = {};//注意:此处的对象是java实体类要有的对象
prize.cus_code = $("#cus_code").val();
prize.addr_code = $("#addr_code").val();
prize.name = names[i];
prize.count = counts[i];
prize.price = prices[i];
prize.reason = reasons[i];
list.push(prize);
}
}
将数组转换为json格式
var plist = JSON.stringify(list);
$.ajax({
url:"<%=request.getContextPath() %>/advisory/makePrize.action",
type:"post",
dataType:"json",
data:{
"plist":plist
},
success:function(data){
if(data){
alert("大礼包录入成功!!!");
location.href="<%=request.getContextPath() %>/work/toWork.action?cus_code="+$("#cus_code").val();
}else{
alert("大礼包录入失败!!!");
}
}
})
})
//清空表单
$("#clear").click(function(){
$('#prize').form('clear');
})
//返回
$("#back").click(function(){
location.href="<%=request.getContextPath() %>/work/toWork.action?cus_code="+$("#cus_code").val();
})
})
</script>
</head>
<body border="1" width="1000px" height="500px">
<div>
<form id="prize" method="post">
<div>
<table border="1" width="100%" height="500px">
<tr align="center" height="50px">
<th colspan="5"><h4 align="center">适老化大礼包配置建议</h4></th>
</tr>
<tr align="center" height="50px">
<th>客户地址信息:</th>
<th colspan="4">
<h4 align="center"><span>${address }</span></h4>
<input type="hidden" id="cus_code" value="${cus_code }">
<input type="hidden" id="addr_code" value="${addr_code }">
</th>
</tr>
<tr align="center" height="50px">
<th>序号</th>
<th>用品名称</th>
<th>数量</th>
<th>公司零售价</th>
<th>配置理由</th>
</tr>
<tr>
<th><input type="text" name="code"></th>
<th><input type="text" name="name"></th>
<th><input type="text" name="count"></th>
<th><input type="text" name="price"></th>
<th><input type="text" name="reason"></th>
</tr>
<tr>
<th><input type="text" name="code"></th>
<th><input type="text" name="name"></th>
<th><input type="text" name="count"></th>
<th><input type="text" name="price"></th>
<th><input type="text" name="reason"></th>
</tr>
<tr>
<th><input type="text" name="code"></th>
<th><input type="text" name="name"></th>
<th><input type="text" name="count"></th>
<th><input type="text" name="price"></th>
<th><input type="text" name="reason"></th>
</tr>
<tr>
<th><input type="text" name="code"></th>
<th><input type="text" name="name"></th>
<th><input type="text" name="count"></th>
<th><input type="text" name="price"></th>
<th><input type="text" name="reason"></th>
</tr>
<tr>
<th><input type="text" name="code"></th>
<th><input type="text" name="name"></th>
<th><input type="text" name="count"></th>
<th><input type="text" name="price"></th>
<th><input type="text" name="reason"></th>
</tr>
<tr>
<th colspan="5" height="50px">大礼包总金额为: <input type="text" id="subtotal">元</th>
</tr>
<tr>
<th colspan="5" height="50px">
<input type="button" value="保存" id="commit">
<input type="button" value="返回" id="back">
<input type="button" value="清空" id="clear">
</th>
</tr>
</table>
</div>
</form>
</div>
</body>
</html>
后台java代码:
/**
* 功能:配置大礼包
* */
@RequestMapping("makePrize")
@ResponseBody
public boolean makePrize(String plist){
//此处需要将格式在转换回来
ObjectMapper objectMapper = new ObjectMapper();
JavaType javaType = objectMapper.getTypeFactory().constructParametricType(List.class, Prize.class);
List<Prize> list = null;
try {
list = objectMapper.readValue(plist, javaType);
//此处开始,MVC层只需要传list集合即可
int i = makeAdvisoryService.addPrize(list);
if(i>0){
return true;
}else{
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
mybatis.xml的映射文件(具体的xml文件名自定义)
<insert id="addPrize" parameterType="java.util.List">
insert into prize (cus_code,addr_code,name,count,price,reason) values
<foreach collection="list" item="p" separator="," index="index">
( #{p.cus_code},
#{p.addr_code},
#{p.name},
#{p.count},
#{p.price},
#{p.reason}
)
</foreach>
</insert>