/* * 处理增加/修改 */ public String editSubmit() { // 处理用户绑定 List<User> userList = new LinkedList<User>(); for (User u : model.getUsers()) { if (u != null && u.getId() != null) { User user = userService.findUser(u.getId()); userList.add(user); } } model.setUsers(userList); // 处理权限绑定 List<Permission> permissionList = new LinkedList<Permission>(); for (Permission it : model.getPermissions()) { if (it != null && it.getId() != null) { Permission permission = userService.findPermission(it.getId()); permissionList.add(permission); } } model.setPermissions(permissionList); // 是否有关联类操作(只在一对一情况下会进入) boolean flag = false; if (refClass != null && refId != null && !refClass.trim().equals("") && !refId.trim().equals("")) { Object object = null; try { Method method = userService.getClass().getDeclaredMethod("find" + refClass, String.class); object = method.invoke(userService, refId.trim()); } catch (Exception e) { System.err.println("UserService中找不到find" + refClass + "方法!"); } try { Class<?> clazz = Class.forName("com.dickey.domain." + refClass.trim()); Method method = model.getClass().getDeclaredMethod("set" + refClass.trim(), clazz); method.invoke(model, object); } catch (Exception e) { System.err.println("Application中找不到set" + refClass + "方法!"); } flag = true; } if (model.getId().equals("")) { // 处理新建 // model.setId(model.getName()); System.out.println(model); userService.addRole(model); } else { // 处理更新 userService.updateRole(model); } return flag ? queryByRef() : query(); }
/** @param args */ public static void main(String[] args) { // TODO Auto-generated method stub ApplicationContext act = new FileSystemXmlApplicationContext("WebContent/WEB-INF/applicationContext.xml"); UserService userService = act.getBean("userService", UserService.class); // 先处理permission Permission permission = new Permission(); permission.setPermission("TEST_PERMISSION"); // userService.addPermission(permission); // 再处理role Role role = new Role(); role.setName("ROLE_TEST"); role.getPermissions().add(permission); userService.addRole(role); // 最后处理user }