<form id="myForm" class="form-horizontal" th:object="${role}"> <input type="hidden" th:field="*{roleId}"> <fieldset> <div class="control-group"> <label class="control-label" for="name">角色名稱</label> <div class="controls"> <input type="text" class="input-xlarge" th:field="*{name}"> </div> </div> <div class="control-group"> <label class="control-label" for="description">角色描述</label> <div class="controls"> <textarea class="input-xlarge" th:field="*{description}" rows="3"></textarea> </div> </div> <div class="control-group"> <label class="control-label" for="select01">Client</label> <div class="controls"> <select id="select01"> <option>-- Select client --</option> <option>Bad Robot</option> <option>Evil Genius</option> <option>Monsters Inc</option> </select> </div> </div> <div class="form-actions"> <button type="submit" class="btn btn-primary">保存</button> <button class="btn">返回列表</button> </div> </fieldset> </form>
<script type="text/javascript"> $.validator.setDefaults({ submitHandler: function(form) { $(form).ajaxSubmit({ url:"[[@{/role/save}]]", type:"post", dataType:"json", success:function(json){ if(json!=null&&json.returncode==0){ window.location.href="[[@{/role/list}]]"; }else{ alert("保存失敗"); } } }); } }); $().ready(function () { $("#myForm").validate({ rules: { name: "required" }, messages: { name: "請輸入角色名稱" } }); }); </script>
@RequestMapping("/save") @ResponseBody public JsonResult save(CmsRole role){ JsonResult jsonResult=new JsonResult(){{ setReturncode(-1); setMessage("保存失敗"); }}; int result; if(role.getRoleId()==null||role.getRoleId()==0){ result=roleService.insert(role); }else{ result=roleService.update(role); } if(result>0){ jsonResult.setReturncode(0); jsonResult.setMessage("保存成功"); } return jsonResult; }