1.什么是XML建模
将XML配置文件中的元素,属性,文本信息 转换成对象的过程叫XML建模。
2.XML建模
根据XML配置文件元素节点创建元素,节点,实体类
ConfigModel ActionModel ForwardModel
利用dom4j+xpath技术实现XML建模ConfigModelFactory(提高代码的复用性)
建模思路
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、根据XML中元素节点 情况 ConfigModel , ActionModel , ForwardModel 对象模型。 (定义对象从里到外)
ConfigModelFactory.java
1
2 package com.zking.model; 3 import java.io.InputStream; 4 import java.util.List; 5 6 import org.dom4j.Document; 7 import org.dom4j.DocumentException; 8 import org.dom4j.Element; 9 import org.dom4j.io.SAXReader; 10 11 /** 12 * 23设计模式之一 13 * 设计模式是一种解决方案,是为了处理Java中搜遇到特定的一问题 14 * 工厂模式 15 * 解决什么问题? 16 * 它是用来将资源文件生产指定的实体类 17 * 好处: 18 * 提高了代码的复用性 19 * 20 * @author Admin 21 * 22 */ 23 public class ConfigModelFactory { 24 25 public static ConfigModel build() throws DocumentException { 26 return ConfigModel("config.xml"); 27 } 28 29 /** 30 * 生产出有类容的实体类configModel 31 * @param xmlPath 32 * @return 33 * @throws DocumentException 34 */ 35 private static ConfigModel ConfigModel(String xmlPath) throws DocumentException { 36 ConfigModel configModel=new ConfigModel(); 37 ActionModel actionModel=null; 38 ForwardModel forwardModel=null; 39 InputStream in=ConfigModelFactory.class.getResourceAsStream(xmlPath); 40 SAXReader saxReader=new SAXReader(); 41 Document doc= saxReader.read(in); 42 List<Element> actionEles =doc.selectNodes("/config/action"); 43 for (Element actionEle : actionEles) { 44 actionModel=new ActionModel(); 45 //给actionModel对象填充xml中的action标签的类容 46 actionModel.setPath(actionEle.attributeValue("path")); 47 actionModel.setPath(actionEle.attributeValue("type")); 48 List<Element> forwardEles=actionEle.selectNodes("forward"); 49 for (Element forwardEle : forwardEles) { 50 forwardModel=new ForwardModel(); 51 //给forwardModel对象填充xml中的action标签的类容 52 forwardModel.setName(forwardEle.attributeValue("name")); 53 forwardModel.setPath(forwardEle.attributeValue("path")); 54 forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("Redirect"))); 55 // redirect默认是true 重定向 56 // 只有填了false才是转发 57 //forwardEle.attributeValue("redirect")拿到的是XML中你所填的值 58 // 不填 重定向 "false".equals(forwardEle.attributeValue("Redirect"))是false 59 // 填 true 重定向 "false".equals(forwardEle.attributeValue("Redirect"))是false 60 // 填 false 转发 "false".equals(forwardEle.attributeValue("Redirect"))是true 61 62 63 actionModel.push(forwardModel); 64 } 65 configModel.push(actionModel); 66 } 67 return configModel; 68 } 69 70 public static void main(String[] args) throws DocumentException { 71 ConfigModel configModel=ConfigModelFactory.build(); 72 ActionModel actionModel=configModel.pop("/loginAction"); 73 System.out.println(actionModel.getType()); 74 75 } 76 77 78 }
ConfigModel.java
package com.zking.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()); } }
ForwardModel.java
package com.zking.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; } }
ActionModel.java
package com.zking.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); } }
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>
1.什么是XML建模
将XML配置文件中的元素,属性,文本信息 转换成对象的过程叫XML建模。
2.XML建模
根据XML配置文件元素节点创建元素,节点,实体类
ConfigModel ActionModel ForwardModel
利用dom4j+xpath技术实现XML建模ConfigModelFactory(提高代码的复用性)
建模思路
1、分析需要被建模的文件中有那几个对象
2、每个对象拥有的行为以及属性
3、根据XML中元素节点 情况 ConfigModel , ActionModel , ForwardModel 对象模型。 (定义对象从里到外)