@Override public void doTag() throws JspException, IOException { if (org == null) { // 如果标签的org属性没有值则读取数据库取得组织结构信息 ServletContext servletContext = ((PageContext) this.getJspContext()).getServletContext(); WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); OrgService orgService = (OrgService) ctx.getBean("orgService"); this.setOrg(orgService.findRoot()); } // 生成第一层组织结构 List<OrgProfile> orgList = this.getOrg().getChildren(); StringBuffer html = new StringBuffer(); if (this.getTopOrgClass() == null) { html.append("<ul>"); } else { html.append("<ul class=\"").append(this.getTopOrgClass()).append("\">"); } for (OrgProfile orgItem : orgList) { html.append("<li>"); html.append("<a id=\"") .append(orgItem.getId()) .append("\" href=\"#\">") .append(orgItem.getName()) .append("</a>"); // 递归生成组织结构 html.append(this.writeHTML(orgItem)); html.append("</li>"); } html.append("</ul>"); try { getJspContext().getOut().print(html); } catch (Exception ex) { log.error("orgtree's tag error.", ex); } }
/** * 递归组织结构生成HTML * * @param org * @return */ private String writeHTML(OrgProfile org) { List<OrgProfile> orgList = org.getChildren(); if (orgList.size() == 0) { return ""; } StringBuffer html = new StringBuffer(); if (this.getSubOrgClass() == null) { html.append("<ul>"); } else { html.append("<ul class=\"").append(this.getSubOrgClass()).append("\">"); } for (OrgProfile orgItem : orgList) { html.append("<li>"); html.append("<a id=\"") .append(orgItem.getId()) .append("\" href=\"#\">") .append(orgItem.getName()) .append("</a>"); html.append(this.writeHTML(orgItem)); html.append("</li>"); } html.append("</ul>"); return html.toString(); }