在第一篇的基础上更改,在实际项目中的使用。
一:HTML 代码:
发送给谁: | |
/** * 点击树节点请求此接口 * @param id :Department的ID * @param response * @param model * @throws Exception */ @RequestMapping(value="/threeLesson/ztreeAjax",method=RequestMethod.POST) public void ztreeTestpost(String id,HttpServletRequest request,HttpServletResponse response,ModelMap model) throws Exception{ request.setCharacterEncoding("gb2312"); response.setContentType("text/html;charset=utf-8"); //查找组织下的用户 ListuserList=userService.getUserListByOrganizationId(Long.valueOf(id)); //获取部门 List departmentList=departmentService.getDepartmentByParentId(Long.valueOf(id)); StringBuffer strbuf=new StringBuffer("["); if (departmentList.size()>0) { for (int i = 0; i < departmentList.size(); i++) { strbuf.append("{\"id\":"+departmentList.get(i).getId()+",\"pId\":"+departmentList.get(i).getParentId()+",\"name\":\"" +departmentList.get(i).getName()+"\",isParent:true},"); } } if (userList.size()>0) { for (int i = 0; i < userList.size(); i++) { String uid="u"+Long.toString(userList.get(i).getId()); // 加U区分用户ID和组织ID strbuf.append("{\"id\":\""+uid+"\",\"pId\":"+userList.get(i).getOrganizationId()+",\"name\":\"" +userList.get(i).getName()+"\"},"); } } String ss=strbuf.toString(); ss=ss.substring(0, ss.length()-1); ss=ss+"]"; PrintWriter pw = response.getWriter(); pw.write(ss); }