这是jsp页面,树开菜单可以有两种方式显示,字符流和解析xml 文件.
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <html> <head> <mce:script type="text/javascript" src="${pageContext.request.contextPath}/js/dhtmlXCommon.js" mce_src="${pageContext.request.contextPath}/js/dhtmlXCommon.js" charset="UTF-8"></mce:script> <mce:script type="text/javascript" src="${pageContext.request.contextPath}/js/dhtmlXTree.js" mce_src="${pageContext.request.contextPath}/js/dhtmlXTree.js" charset="UTF-8"></mce:script> <mce:script type="text/javascript" src="${pageContext.request.contextPath}/js/dhtmlXTreeExtend.js" mce_src="${pageContext.request.contextPath}/js/dhtmlXTreeExtend.js" charset="UTF-8"></mce:script> <link href="<%=request.getContextPath() %>/css/dhtmlXTree.css" rel="stylesheet" type="text/css"> </head> <body> <table width="100%" id="apTable" border="0" cellpadding="0" cellspacing="1" bgcolor="#E6E6E6" class="ttd"> <tr> <td> <div id="treeboxbox_tree" style="width:370; height:238;background-color:#f5f5f5;border :1px solid Silver;OVERFLOW-Y:auto;OVERFLOW-X:hidden;"></div> </td> </tr> </table> <mce:script language="javascript" charset="UTF-8"><!-- tree=new dhtmlXTreeObject("treeboxbox_tree","100%","100%",0); tree.setImagePath("${pageContext.request.contextPath}/images/imgs/"); tree.enableCheckBoxes(1); tree.enableThreeStateCheckboxes(true); tree.loadXML("<%=request.getContextPath()%>/ap/p39type.action?parmValue=<%=selectValue%>"); //tree.loadXML("dhtmlTree.txt"); // --></mce:script> </body></html>
这是xml文件内容:
<?xml version='1.0' encoding='iso-8859-1'?> <tree id="0"> <item text="Books" id="books"> <item text="Mystery & Thrillers" id="mystery" im0="book.gif" im1="books_open.gif" im2="books_close.gif"> <item text="Lawrence Block" id="lb" im0="book.gif" im1="books_open.gif" im2="book.gif"> <item text="All the Flowers Are Dying" id="lb_1" im0="book_titel.gif" im1="book_titel.gif" im2="book_titel.gif"/> <item text="The Burglar on the Prowl" id="lb_2" im0="book_titel.gif" im1="book_titel.gif" im2="book_titel.gif"/> <item text="The Plot Thickens" id="lb_3" im0="book_titel.gif" im1="book_titel.gif" im2="book_titel.gif"/> <item text="Grifter's Game" id="lb_4" im0="book_titel.gif" im1="book_titel.gif" im2="book_titel.gif"/> <item text="The Burglar Who Thought He Was Bogart" id="lb_5" im0="book_titel.gif" im1="book_titel.gif" im2="book_titel.gif"/> </item> <item text="Ian Rankin" id="ir" im0="book.gif" im1="books_open.gif" im2="book.gif"></item> <item text="James Patterson" id="jp" im0="book.gif" im1="books_open.gif" im2="book.gif"></item> <item text="Nancy Atherton" id="na" im0="book.gif" im1="books_open.gif" im2="book.gif"></item> </item> <item text="Horror" id="horror" open="1" im0="book.gif" im1="books_open.gif" im2="books_close.gif"> <item text="Stephen King" id="sk" im0="book.gif" im1="books_open.gif" im2="book.gif"></item> <item text="Mary Janice Davidson" id="mjd" im0="book.gif" im1="books_open.gif" im2="book.gif"></item> <item text="Katie Macalister" id="km" im0="book.gif" im1="books_open.gif" im2="book.gif"></item> </item> <item text="Sport" id="sport" im0="book.gif" im1="books_open.gif" im2="books_close.gif"> <item text="Bill Reynolds" id="br" im0="book.gif" im1="books_open.gif" im2="book.gif"></item> </item> </item> <item text="Magazines" id="magazines" im0="tombs_mag.gif" im1="tombs_mag.gif" im2="tombs_mag.gif"> <item text="Sport" id="mag_sp" im0="magazines_open.gif" im1="magazines_open.gif" im2="magazines_close.gif"></item> </item> </tree>
java形式:
action代码:
package gov.cnca.fd.pfd.action; import gov.cnca.fd.framework.system.GlobalConsts; import gov.cnca.fd.pfd.service.IP39typeQueryService; import java.io.IOException; import org.apache.struts2.ServletActionContext; import com.caf.web.struts2.ActionSupport; public class P39typeQueryAction extends ActionSupport{ private String parmValue; //private APractitionerQueryDTO quertDTO; private IP39typeQueryService p39typeService; public void setP39typeService(IP39typeQueryService service) { p39typeService = service; } public String getXMLfile() { System.out.println("参数————————>>>>parmValue:"+parmValue); if(GlobalConsts.P39TYPE_CODE_N3_V.equals(parmValue)){ parmValue = GlobalConsts.P39TYPE_CODE_N3; } String result = p39typeService.get39TypeCodeToXML(parmValue); try { ServletActionContext.getResponse().setContentType("text/xml;charset=UTF-8"); ServletActionContext.getResponse().setHeader("Cache-Control","no-cache"); ServletActionContext.getResponse().getWriter().print(result); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } public String getParmValue() { return parmValue; } public void setParmValue(String parmValue) { this.parmValue = parmValue; } }
service层代码:
package gov.cnca.fd.pfd.service; import gov.cnca.fd.framework.system.GlobalConsts; import gov.cnca.fd.pfd.util.DhtmlTreeNode; import gov.cnca.fd.pfd.util.TreeUtil; import gov.cnca.fd.pfd.vo.P39TypeCodeVO; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.OutputStreamWriter; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List; import com.caf.orm.IDAO; public class P39typeQueryService implements IP39typeQueryService{ private IDAO p_39typeCodeDAO ; //取出39大类写到XML文件中 public String get39TypeCodeToXML(String code){ String reString = ""; List<P39TypeCodeVO> p39TypeList = (List<P39TypeCodeVO>)p_39typeCodeDAO.execute("getList39TypeByCode",code); //System.out.println("---->>1111:::p39TypeList:"+p39TypeList.size()); DhtmlTreeNode tnall = null; tnall = this.getDhtmlTreeNode(p39TypeList,code); //URL pathURL = Thread.currentThread().getContextClassLoader().getResource(""); //String path = pathURL.getPath(); //System.out.println("---->>1111:::path:"+path); //int end = path.indexOf("WEB-INF/"); //path = path.substring(1, end) + "practitioner/ap/"; //System.out.println("---->>:::path:"+path); reString = TreeUtil.buildTreeXml(tnall); //reString = this.writeXml(path+"dhtmlTree.txt", tnall); return reString; } /** * 将集合List对象构造一个DhtmlTreeNode对象返回 */ private DhtmlTreeNode getDhtmlTreeNode(List<P39TypeCodeVO> p39TypeList,String codeType){ List<P39TypeCodeVO> list1 =new ArrayList<P39TypeCodeVO>(),list2 = new ArrayList<P39TypeCodeVO>(),list3 = new ArrayList<P39TypeCodeVO>(); P39TypeCodeVO p39Type1 = new P39TypeCodeVO(),p39Type2 = new P39TypeCodeVO(),p39Type3 = new P39TypeCodeVO(); String [] otherType = GlobalConsts.P39TYPE_CODE_N; for(int v=0; v < otherType.length; v++){ if(otherType[v].equals(codeType)){ list1 = p39TypeList; } } if(list1.size() == 0) for(int i=0;i<p39TypeList.size();i++){ p39Type1 = (P39TypeCodeVO)p39TypeList.get(i); //System.out.println("---->service::"+p39Type1.getCode()+"::"+p39Type1.getCodeType()+"::"+p39Type1.getName()); if(p39Type1.getCode().length() == 2){ list1.add(p39Type1); }else if(p39Type1.getCode().length() == 5){ list2.add(p39Type1); }else if(p39Type1.getCode().length() == 8){ list3.add(p39Type1); } } List<DhtmlTreeNode> lst = new ArrayList<DhtmlTreeNode>(); DhtmlTreeNode tn = null; DhtmlTreeNode tn2 = null; DhtmlTreeNode tn3 = null; for(int i = 0;i<list1.size();i++){ p39Type1 = (P39TypeCodeVO)list1.get(i); tn = new DhtmlTreeNode(); tn.setId(p39Type1.getCode()); tn.setName(p39Type1.getName()); List<DhtmlTreeNode> lst01 = new ArrayList<DhtmlTreeNode>(); for(int j = 0;j<list2.size();j++){ p39Type2 = (P39TypeCodeVO)list2.get(j); List<DhtmlTreeNode> lst02 = new ArrayList<DhtmlTreeNode>(); if(p39Type1.getCode().equals(p39Type2.getCode().substring(0,2))){ tn2 = new DhtmlTreeNode(); tn2.setId(p39Type2.getCode()); tn2.setName(p39Type2.getName()); lst01.add(tn2); for(int k=0;k<list3.size();k++){ p39Type3 = (P39TypeCodeVO)list3.get(k); if(p39Type2.getCode().equals(p39Type3.getCode().substring(0,5))){ tn3 = new DhtmlTreeNode(); tn3.setId(p39Type3.getCode()); tn3.setName(p39Type3.getName()); lst02.add(tn3); } } } if(lst02.size()>0) tn2.setChildren(lst02); } if(lst01.size()>0) tn.setChildren(lst01); lst.add(tn); } DhtmlTreeNode tnall = new DhtmlTreeNode(); tnall.setId(""); tnall.setName(""); if(lst.size()>0) tnall.setChildren(lst); return tnall; } //把DhtmlTreeNode对象写到指定的xml文件中. private String writeXml(String path, DhtmlTreeNode tnall){ String reStr = ""; try { //String path="D:/newbook.xml"; OutputStreamWriter fs = new OutputStreamWriter(new FileOutputStream(path),"UTF-8"); reStr = TreeUtil.buildTreeXml(tnall); fs.write(reStr); fs.flush(); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return reStr; } public IDAO getP_39typeCodeDAO() { return p_39typeCodeDAO; } public void setP_39typeCodeDAO(IDAO codeDAO) { p_39typeCodeDAO = codeDAO; } }
package gov.cnca.fd.pfd.util; import java.util.List; public class TreeUtil { /** * 根据 DhtmlTreeNode对象 创建xml格式字符串 * * @param tree * @return */ public static String buildTreeXml(DhtmlTreeNode tree) { StringBuffer sb = new StringBuffer(); int level=1; List<DhtmlTreeNode> levelChildren = null;// 树下属大类节点list sb.append("<?xml version='1.0' encoding='utf-8'?>/n"); sb.append("<tree id='0'>/n"); if (tree == null) { return null; } levelChildren = tree.getChildren(); bulidItems(levelChildren, sb,level); sb.append("</tree> "); return sb.toString(); } /** * 创建子节点 * * @param levelChildren */ private static void bulidItems(List<DhtmlTreeNode> levelChildren, StringBuffer sb,int level) { if (levelChildren != null && levelChildren.size() > 0) { for (DhtmlTreeNode levelTreeNode : levelChildren) { int levelcount=level+1; if (levelTreeNode != null && levelTreeNode.getChildren() != null && levelTreeNode.getChildren().size() > 0) { setItem(levelTreeNode,level,sb); sb.append(" >/n"); bulidItems(levelTreeNode.getChildren(), sb,levelcount); for(int i=0;i<level;i++){ sb.append("/t"); } sb.append("</item>/n"); } else if(levelTreeNode != null) { setItem(levelTreeNode,level,sb); sb.append(" />/n "); } } } } /** * 设置节点的字符串 * @param levelTreeNode * @param level * @param sb */ private static void setItem(DhtmlTreeNode levelTreeNode,int level,StringBuffer sb){ for(int i=0;i<level;i++){ sb.append("/t"); } sb.append("<item id='" + levelTreeNode.getId() + "' text='" + levelTreeNode.getName().trim() + "' "); //是否选中 if(levelTreeNode.isChecked()){ sb.append(" checked='true' "); } //是否展开节点 if(levelTreeNode.isOpened()){ sb.append(" select='yes' "); } } }
package gov.cnca.fd.pfd.util; import com.caf.util.workflow.bo.TreeNode; public class DhtmlTreeNode extends TreeNode { //是否选中 private boolean checked; //是否展开 private boolean opened; public boolean isOpened() { return opened; } public void setOpened(boolean opened) { this.opened = opened; } public boolean isChecked() { return checked; } public void setChecked(boolean checked) { this.checked = checked; this.opened = checked; //选中时 ,默认为展开 } }
package com.caf.util.workflow.bo; import java.util.List; /** * 为了通用于树状结构,加了冗余的属性 * 使用时可根据需要使用其中的一部分。 * * @author zhangzb * */ public class TreeNode { private String name; private String id; private String description; private String url; private String picturePath; private String parentId; private String type; private List children ; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getPicturePath() { return picturePath; } public void setPicturePath(String picturePath) { this.picturePath = picturePath; } public String getParentId() { return parentId; } public void setParentId(String parentId) { this.parentId = parentId; } public String getType() { return type; } public void setType(String type) { this.type = type; } public List getChildren() { return children; } public void setChildren(List children) { this.children = children; } }