日期:2014-05-19 浏览次数:20841 次
@Entity @Table(name = "tbl_role_header") public class Role { private String id; private String code; private String name; private RoleType roleType; //getter setter } public enum RoleType { /** * 1.role. */ ROLE(1), /** * 2.group. */ GROUP(2); private int roleType; private RoleType(int roleType){ this.setRoleType(roleType); } public int getRoleType() { return roleType; } public void setRoleType(int roleType) { this.roleType = roleType; } }
function saveRole() { var roleNo = $('#roleNo').val(); var roleType = $('#roleType').val(); //roleType的值为1或2 var roleName = $('#roleName').val(); var roleActive = $('#roleActive').val(); if(roleNo == null || roleName == null) { $.messager.alert('提示','角色代號和角色名稱不能為空!','info'); return; } var active = 0; if(roleActive == 'on') { active = 1; } var roleDesc = $('#roleDesc').val(); var rows = $('#tblRoles').datagrid('getRows'); var roleMembers = new Array(); for(var i=0; i<rows.length; i++) { var employee = {'employeeId':rows[i].empNo, 'name':rows[i].empName}; var member = {'member':employee,'remark':rows[i].remark}; roleMembers.push(member); } var data = {'code':roleNo,'roleType':roleType,'name':roleName, 'active':active,'remark':roleDesc,'roleMembers':roleMembers}; $.ajax({ type:'post', url:'save_role', data:JSON.stringify(data), contentType: "application/json", success:function(data){ $.messager.alert('提示',data.message,'info'); window.location.href= "list"; }, error:function(data){ $.messager.alert('提示',data.responseText,'info'); } }); }
@RequestMapping(value="/save_role", method=RequestMethod.POST) @ResponseBody public Map<String, String> addRole(@RequestBody Role role){ Map<String, String> result = new HashMap<String, String>(); try { List<Role> rs = testGetRoles(); //List<Role> rs = roleService.getAll(Role.class); for(Role r : rs) { if(r.getCode().equals(role.getCode())) { result.put("message", "已存在同樣的角色代號,不可重覆新增!"); return result; } } roleService.save(role); result.put("message", "操作成功!"); } catch (Exception ex){ ex.printStackTrace(); result.put("message", "操作失败!"); } return result; }