1.
html +="<td class='text-right'><input type='text' name='mcode' value='"+$mcode+"' class='material-input'></td>"; html +="<td class='text-right'><input type='text' name='materialName' value='"+$materialName+"' class='material-input'></td>"; html +="<td class='text-right'><input type='text' name='properInventory' value='"+$properInventory+"' class='material-input'></td>";
var formdata = $("#materialRegistrationSaveForm").serializeArray(); console.log("formdata : "+ formdata2); var len = $(formdata).length; var jsonData='{"list":['; $(formdata ).each(function(index, obj){ var comma=""; if(obj.name=="mcode"){ jsonData +="{" } if(obj.name=="properInventory"){ comma=""; jsonData +='"'+obj.name+'" : "'+obj.value+'"'+comma; jsonData +="}"; if (index != len - 1) { //마지막 인덱스 값이 아니면 jsonData +=","; } }else{ comma=","; jsonData +='"'+obj.name+'" : "'+obj.value+'"'+comma; } }); jsonData +="]}"; console.log(jsonData);
@PostMapping(value="write3.do") public String write3(@RequestBody MaterialVO materialVO, Model model) throws Exception{ log.info(materialVO.toString()); return "success"; }
2. 배열 형식 html
list["+count+"].mcode
html +="<td class='text-right'><input type='text' name='list["+count+"].mcode' value='"+$mcode+"' class='material-input'></td>"; html +="<td class='text-right'><input type='text' name='list["+count+"].materialName' value='"+$materialName+"' class='material-input'></td>"; html +="<td class='text-right'><input type='text' name='list["+count+"].properInventory' value='"+$properInventory+"' class='material-input'></td>";
var formdata2 = $("#materialRegistrationSaveForm").serialize(); $.ajax({ url: $("#materialRegistrationSaveForm").attr("action"), type: 'POST', data:formdata2, dataType: 'text', success: function(msg) { alert(msg); } });
private List<MaterialVO> list;
@Data public class MaterialVO { private String mcode; //자재코드 private String materialName; //자재명 private Integer properInventory; //적정재고량 private List<MaterialVO> list; }
@PostMapping(value="write.do") @ResponseBody public String write(MaterialVO materialVO, Model model) throws Exception{ try { if(!managementService.insertMaterial(materialVO.getList())) return "blank"; return "success"; }catch(Exception e) { if(e.getMessage().contains("Duplicate")) return "duplicate"; else return e.getMessage(); } }
/** * 자재 등록 */ public boolean insertMaterial(List<MaterialVO> list) throws Exception { for(MaterialVO vo : list) { if(vo.getMcode()==null || StringUtils.isEmpty(vo.getMcode())) { return false; } if(vo.getMaterialName()==null || StringUtils.isEmpty(vo.getMaterialName())) { return false; } if(vo.getProperInventory()==null || StringUtils.isEmpty(vo.getProperInventory())) { return false; } } managementDao.insertMaterial(list); return true; }
<insert id="insertMaterial" parameterType="java.util.List"> INSERT INTO tbl_material( mcode ,material_name ,proper_inventory ) VALUES <foreach item="item" index="index" collection="list" separator=" , "> ( #{item.mcode} ,#{item.materialName} ,#{item.properInventory} ) </foreach> </insert>
댓글 ( 4)
댓글 남기기