xml解析
1、ActionModel.java
package com.huangyucan.model; import java.util.HashMap; import java.util.Map; public class ActionModel { private String path; private String type; private Map<String, ForwardModel> fmap=new HashMap<>(); public String getPath() { return path; } public void setPath(String path) { this.path = path; } public String getType() { return type; } public void setType(String type) { this.type = type; } public void push(ForwardModel forwardmodel) { fmap.put(forwardmodel.getName(), forwardmodel); } public ForwardModel pop(String name) { return fmap.get(name); } }
2、ConfigModel.java
package com.huangyucan.model; import java.util.HashMap; import java.util.Map; public class ConfigModel { private Map<String, ActionModel> amap=new HashMap<>(); public void push(ActionModel actionmdodel) { amap.put(actionmdodel.getPath(), actionmdodel); } public ActionModel pop(String path) { return amap.get(path); } public static void main(String[] args) { ConfigModel configModel=new ConfigModel(); ActionModel actionModel=configModel.pop("/loginAction"); System.out.println(actionModel.getType()); } }
3、ForwardModel.java
package com.huangyucan.model; public class ForwardModel { private String name; private String path; private boolean redirect; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPath() { return path; } public void setPath(String path) { this.path = path; } public boolean isRedirect() { return redirect; } public void setRedirect(boolean redirect) { this.redirect = redirect; } }
4、ConfigModelFactory.java
注意导包
WebContent路径下的lib中放入
WebContent/WEB-INF/lib/dom4j-1.6.1.jar
WebContent/WEB-INF/lib/jaxen-1.1-beta-6.jar
package com.huangyucan.model; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; /** * 23设计模式之一 * 设计模式是一种解决方案,是为了处理Java中搜遇到特定的一问题 * 工厂模式 * 解决什么问题? * 它是用来将资源文件生产指定的实体类 * 好处: * 提高了代码的复用性 * @author Admin * */ public class ConfigModelFactory { public static ConfigModel build() throws DocumentException { return ConfigModel("/config.xml"); } /** * 生产出有类容的实体类configModel * @param xmlPath * @return * @throws DocumentException */ private static ConfigModel ConfigModel(String xmlPath) throws DocumentException { ConfigModel configModel=new ConfigModel(); ActionModel actionModel=null; ForwardModel forwardModel=null; InputStream in=ConfigModelFactory.class.getResourceAsStream(xmlPath); SAXReader saxReader=new SAXReader(); Document doc= saxReader.read(in); List<Element> actionEles =doc.selectNodes("/config/action"); for (Element actionEle : actionEles) { actionModel=new ActionModel(); //给actionModel对象填充xml中的action标签的类容 actionModel.setPath(actionEle.attributeValue("path")); actionModel.setType(actionEle.attributeValue("type")); List<Element> forwardEles=actionEle.selectNodes("forward"); for (Element forwardEle : forwardEles) { forwardModel=new ForwardModel(); //给forwardModel对象填充xml中的action标签的类容 forwardModel.setName(forwardEle.attributeValue("name")); forwardModel.setPath(forwardEle.attributeValue("path")); forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("Redirect"))); // redirect默认是true 重定向 // 只有填了false才是转发 //forwardEle.attributeValue("redirect")拿到的是XML中你所填的值 // 不填 重定向 "false".equals(forwardEle.attributeValue("Redirect"))是false // 填 true 重定向 "false".equals(forwardEle.attributeValue("Redirect"))是false // 填 false 转发 "false".equals(forwardEle.attributeValue("Redirect"))是true actionModel.push(forwardModel); } configModel.push(actionModel); } return configModel; } public static void main(String[] args) throws DocumentException { ConfigModel configModel=ConfigModelFactory.build(); ActionModel actionModel=configModel.pop("/loginAction"); ForwardModel forwardModel=actionModel.pop("success"); System.out.println(actionModel.getType()); System.out.println(forwardModel.getPath()) ; } }
5、config.xml
<?xml version="1.0" encoding="UTF-8"?><!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 type:字符串,非空 --> <!-- config标签:可以包含0~N个action标签 --> <config> <action path="/regAction" type="test.RegAction"> <!-- forward标签:没有子标签; name:字符串,同一action标签下的forward标签name值不能相同 ; path:以/开头的字符串 redirect:只能是false|true,允许空,默认值为false --> <forward name="failed" path="/reg.jsp" redirect="false" /> <forward name="success" path="/login.jsp" redirect="true" /> </action> <action path="/loginAction" type="test.LoginAction"> <forward name="failed" path="/login.jsp" redirect="false" /> <forward name="success" path="/main.jsp" redirect="true" /> </action> </config>