需求:
在jsp页面上展示树形结构,当选中某个节点时,可以新增该节点的子节点、删除该节点、修改该节点信息。
操作节点的时候,既操作数据库,也操作xml文件。先操作数据库,后操作xml文件。
如果操作数据库失败,则不再操作xml文件。
说明:
1. menuNo 是唯一的;
2. 节点层次只有一层,但是各个节点之间有关联关系。如:menuNo=1 的子节点以“1-”(不包括双引号)
开头;
3. 在新增节点时,节点编号menuNo自动生成(查找数据库中menuNo的最大值,在此基础上加1);
4. 这里只写对xml文件的操作,不写数据库操作。
环境说明:
jdk-6u21;
jar 包:
dom4j-1.6.1.jar;
jaxen-1.1.4.jar;
log4j-1.2.16.jar;
slf4j-api-1.4.3.jar;
slf4j-log4j12-1.4.0.jar
XML 文件:
<?xml version="1.0" encoding="UTF-8"?> <root> <menu id="8a8080fd45d529340145d5294acf0000"> <menuId>8a8080fd45d529340145d5294acf0000</menuId> <menuNo>1</menuNo> <parentNo>123</parentNo> <menuName>首页</menuName> <menuUrl>/home</menuUrl> <menuLevel>1</menuLevel> <isLeaf>0</isLeaf> <expanded>1</expanded> <loaded>1</loaded> </menu> <menu id="8a8080fd45d52b520145d52b64c30000"> <menuId>8a8080fd45d52b520145d52b64c30000</menuId> <menuNo>1-1</menuNo> <parentNo>1</parentNo> <menuName>首页1</menuName> <menuUrl>index/index/homePage</menuUrl> <menuLevel>2</menuLevel> <isLeaf>1</isLeaf> <expanded>1</expanded> <loaded>1</loaded> </menu> <menu id="92e0b82fae0f4bbbaf7f478031d59b1f"> <menuId>92e0b82fae0f4bbbaf7f478031d59b1f</menuId> <menuNo>1-2</menuNo> <parentNo>1</parentNo> <menuName>首页2</menuName> <menuUrl>index/index1</menuUrl> <menuLevel>2</menuLevel> <isLeaf>1</isLeaf> <expanded>1</expanded> <loaded>1</loaded> </menu> <menu id="8a8080fd45dea7320145dea755cd0000"> <menuId>8a8080fd45dea7320145dea755cd0000</menuId> <menuNo>2</menuNo> <parentNo/> <menuName>系统管理</menuName> <menuUrl/> <menuLevel>1</menuLevel> <isLeaf>0</isLeaf> <expanded>1</expanded> <loaded>1</loaded> </menu> <menu id="8a8080fd45dea92f0145dea942bf0000"> <menuId>8a8080fd45dea92f0145dea942bf0000</menuId> <menuNo>2-1</menuNo> <parentNo>2</parentNo> <menuName>菜单配置管理</menuName> <menuUrl>hw/menu</menuUrl> <menuLevel>2</menuLevel> <isLeaf>1</isLeaf> <expanded>1</expanded> <loaded>1</loaded> </menu> </root>
其中,“首页1” 和 “首页2” 是 “首页” 的子节点;“菜单配置管理” 是 “系统管理” 的子节点;
需要用一个 JavaBean 来表示菜单节点:
HwMenu.java
public class HwMenu { private String menuId; // 菜单编号 (Unique Key) private String menuNo; // 菜单名称 private String menuName; // 菜单链接 private String menuUrl; // 菜单等级 private String menuLevel; // 父编号 (对应 menuNo) private String parentNo; // 是否子节点. 0 : false , 1 : true private Integer isLeaf; // 是否展开. 0 : false , 1 : true private Integer expanded; // 是否加载. 0 : false , 1 : true private Integer loaded; // 省略getter/setter }
xml节点处理类:
MenuTree.java
import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.DocumentHelper; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class MenuTree { private static final Logger log = LoggerFactory.getLogger(MenuTree.class); private static final String XML_FILE = "xml/menu.xml"; /** * @Title: addNode * @deprecated: 将节点menu添加到xml文件中 * @param menu * : 节点信息 * @throws Exception * @author * @date 2014-5-9 */ public void addNode(HwMenu menu) throws Exception { XMLWriter xmlwriter = null; InputStream is = null; OutputStream out = null; try { SAXReader sax = new SAXReader(); // 创建一个 menu 元素 Element elt = DocumentHelper.createElement("menu"); // 主键id Element menuId = DocumentHelper.createElement("menuId"); // 菜单编号 Element menuNo = DocumentHelper.createElement("menuNo"); // 父编号 Element parentNo = DocumentHelper.createElement("parentNo"); // 菜单名称 Element menuName = DocumentHelper.createElement("menuName"); // 菜单链接 Element menuUrl = DocumentHelper.createElement("menuUrl"); // 菜单等级 Element menuLevel = DocumentHelper.createElement("menuLevel"); // 是否子节点 Element isLeaf = DocumentHelper.createElement("isLeaf"); // 是否展开 Element expanded = DocumentHelper.createElement("expanded"); // 是否加载 Element loaded = DocumentHelper.createElement("loaded"); menuId.setText(menu.getMenuId()); menuNo.setText(menu.getMenuNo()); parentNo.setText(menu.getParentNo()); menuName.setText(menu.getMenuName()); menuUrl.setText(menu.getMenuUrl()); menuLevel.setText(menu.getMenuLevel()); // 是否子节点. 0 : false , 1 : true isLeaf.setText(menu.getIsLeaf() + ""); // 是否展开. 0 : false , 1 : true expanded.setText(menu.getExpanded() + ""); // 是否加载. 0 : false , 1 : true loaded.setText(menu.getLoaded() + ""); is = Thread.currentThread().getContextClassLoader() .getResourceAsStream(XML_FILE); Document doc = sax.read(is); // 根节点 Element root = doc.getRootElement(); // 将子元素添加到 menu 元素中 elt.add(menuId); elt.add(menuNo); elt.add(parentNo); elt.add(menuName); elt.add(menuUrl); elt.add(menuLevel); elt.add(isLeaf); elt.add(expanded); elt.add(loaded); // 为 menu 添加 id 属性 elt.addAttribute("id", menu.getMenuId()); // 将menu元素添加到根节点下 root.add(elt); // 输出格式 OutputFormat outformat = new OutputFormat(); // 指定XML编码 outformat.setEncoding("UTF-8"); outformat.setNewlines(true); outformat.setIndent(true); outformat.setTrimText(true); String rootPath = System.getProperty("user.dir"); out = new FileOutputStream(rootPath + "/src-config/" + XML_FILE); xmlwriter = new XMLWriter(out, outformat); xmlwriter.write(doc); } catch (DocumentException e) { log.error("Add Node fail:" + e.getMessage(), e); throw e; } catch (Exception e) { log.error("Add Node fail:" + e.getMessage(), e); throw e; } finally { close(xmlwriter, out, is); } } /** * @Title: modifyNode * @deprecated: 修改节点元素值 * @param menu * 元素值 * @author * @date 2014-5-12 */ @SuppressWarnings("rawtypes") public void modifyNode(HwMenu menu) throws Exception { InputStream is = null; OutputStream out = null; XMLWriter xmlwriter = null; Document doc = null; try { SAXReader sax = new SAXReader(); is = Thread.currentThread().getContextClassLoader() .getResourceAsStream(XML_FILE); doc = sax.read(is); // 根节点 Element root = doc.getRootElement(); // 取得某节点下名为"menu"的所有字节点 List nodes = root.elements("menu"); // xml元素 Element element = null; for (Object obj : nodes) { element = (Element) obj; // 修改指定节点下的子节点的值 if (menu.getMenuId().equals(element.attributeValue("id"))) { element.element("menuId").setText(menu.getMenuId()); element.element("menuNo").setText(menu.getMenuNo()); element.element("parentNo").setText(menu.getParentNo()); element.element("menuName").setText(menu.getMenuName()); element.element("menuUrl").setText(menu.getMenuUrl()); element.element("menuLevel").setText(menu.getMenuLevel()); element.element("isLeaf").setText(menu.getIsLeaf() + ""); element.element("expanded") .setText(menu.getExpanded() + ""); element.element("loaded").setText(menu.getLoaded() + ""); } } // 输出格式 OutputFormat outformat = new OutputFormat(); // 指定XML编码 outformat.setEncoding("UTF-8"); outformat.setNewlines(true); outformat.setIndent(true); outformat.setTrimText(true); String rootPath = System.getProperty("user.dir"); out = new FileOutputStream(rootPath + "/src-config/" + XML_FILE); xmlwriter = new XMLWriter(out, outformat); xmlwriter.write(doc); } catch (DocumentException e) { log.error("Modify Node fail:" + e.getMessage(), e); throw e; } catch (Exception e) { log.error("Modify Node fail:" + e.getMessage(), e); throw e; } finally { close(xmlwriter, out, is); } } @SuppressWarnings("rawtypes") public void removeNode(String nodeId) throws Exception { InputStream is = null; OutputStream out = null; XMLWriter xmlwriter = null; Document doc = null; try { SAXReader sax = new SAXReader(); is = Thread.currentThread().getContextClassLoader() .getResourceAsStream(XML_FILE); doc = sax.read(is); // 根节点 Element root = doc.getRootElement(); // 取得某节点下名为"menu"的所有字节点 List nodes = root.elements("menu"); // xml元素 Element element = null; Map<String, String> map = new ConcurrentHashMap<String, String>(); for (Object obj : nodes) { element = (Element) obj; // 菜单编号 String menuNo = null; // 删除指定节点下的子节点的值 if (nodeId.equals(element.attributeValue("id"))) { menuNo = element.element("menuNo").getTextTrim(); map.put("p_menuNo", menuNo); // 删除nodeId对应的菜单节点 element.detach(); } // 节点编号 menuNo = element.element("menuNo").getTextTrim(); if (null != map.get("p_menuNo") && !"".equals(map.get("p_menuNo"))) { if (null != menuNo && menuNo.startsWith(map.get("p_menuNo") .concat("-"))) { /* menuNo是 p_menuNo 的子节点 */ // 删除子节点 element.detach(); } } } // 输出格式 OutputFormat outformat = new OutputFormat(); // 指定XML编码 outformat.setEncoding("UTF-8"); outformat.setNewlines(true); outformat.setIndent(true); outformat.setTrimText(true); String rootPath = System.getProperty("user.dir"); out = new FileOutputStream(rootPath + "/src-config/" + XML_FILE); xmlwriter = new XMLWriter(out, outformat); xmlwriter.write(doc); } catch (DocumentException e) { log.error("Modify Node fail:" + e.getMessage(), e); throw e; } catch (Exception e) { log.error("Modify Node fail:" + e.getMessage(), e); throw e; } finally { close(xmlwriter, out, is); } } private void close(XMLWriter xmlwriter, OutputStream out, InputStream is) { if (null != xmlwriter) { try { xmlwriter.close(); } catch (IOException e) { e.printStackTrace(); } xmlwriter = null; } if (null != out) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } out = null; } if (null != is) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } is = null; } } }
如果是放在 web 工程中,则代码:
String rootPath = System.getProperty("user.dir");
需要改成
HttpServletRequest req = ...... String rootPath = req.getSession().getServletContext().getRealPath("\\");
测试类(Junit 4):
MenuTreeTest.java
import org.junit.Test; public class MenuTreeTest { @Test public void testAddNode() { HwMenu menu = new HwMenu(); menu.setMenuId("8a8080fd45dea92f0145dea942bf0000"); menu.setMenuNo("2-1"); menu.setParentNo("2"); menu.setMenuName("菜单配置管理"); menu.setMenuUrl("hw/menu"); menu.setMenuLevel("2"); // 是否子节点. 0 : false , 1 : true menu.setIsLeaf(1); // 是否展开. 0 : false , 1 : true menu.setExpanded(1); // 是否加载. 0 : false , 1 : true menu.setLoaded(1); try { new MenuTree().addNode(menu); System.out.println("添加成功"); } catch (Exception e) { System.out.println("添加失败"); e.printStackTrace(); } } @Test public void testModifyNode() { HwMenu menu = new HwMenu(); menu.setMenuId("8a8080fd45d529340145d5294acf0000"); menu.setMenuNo("1"); menu.setParentNo("123"); menu.setMenuName("首页"); menu.setMenuUrl("/home"); menu.setMenuLevel("1"); // 是否子节点. 0 : false , 1 : true menu.setIsLeaf(0); // 是否展开. 0 : false , 1 : true menu.setExpanded(1); // 是否加载. 0 : false , 1 : true menu.setLoaded(1); try { new MenuTree().modifyNode(menu); System.out.println("更新成功"); } catch (Exception e) { System.out.println("更新失败"); e.printStackTrace(); } } @Test public void testRemoveNode() { String nodeId = "8a8080fd45dea92f0145dea942bf0000"; try { new MenuTree().removeNode(nodeId); System.out.println("删除成功"); } catch (Exception e) { System.out.println("删除失败"); e.printStackTrace(); } } }
代码结构: