建模分两步:
1、以面向对象的编程思想,描述xml资源文件。
2、将xml文件中内容封装进model实体对象。
导入文件:config.xml
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE config[ <!ELEMENT config (action*)> <!ELEMENT action (forward*)> <!ELEMENT forward EMPTY> <!ATTLIST action path CDATA #REQUIRED type CDATA #REQUIRED > <!ATTLIST forward name CDATA #REQUIRED path CDATA #REQUIRED redirect (true|false) "false" > ]> <!-- config标签:可以包含0~N个action标签 --> <config> <!-- action标签:可以饱含0~N个forward标签 path:以/开头的字符串,并且值必须唯一 非空 type:字符串,非空 --> <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>
新建类: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 actionmodel) { amap.put(actionmodel.getPath(), actionmodel); } public Actionmodel pop(String path) { return amap.get(path); } }
新建类:Actionmodel.java
package com.zking.model; import java.util.HashMap; import java.util.Map; public class Actionmodel { // <action path="/loginAction" type="test.LoginAction"> 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); } }
新建类:ForwardModel.java
package com.zking.model; public class ForwardModel { // <forward name="failed" path="/login.jsp" redirect="false" /> 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; } }
新建类:ConfigModelFactory.java
package com.zking.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中所遇到的特定的一些问题 * 解决什么问题呢? * 它是用来将资源文件生产指定的实体类 * 好处: * 提高了代码的复用性 */ public class ConfigModelFactory { public static ConfigModel build() throws DocumentException { return ConfigModel("/config.xml"); } /* * 生产出有类内容的实体类configmodel */ private static ConfigModel ConfigModel(String string) throws DocumentException { ConfigModel configModel=new ConfigModel(); Actionmodel actionmodel=null; ForwardModel forwardModel=null; InputStream in= ConfigModelFactory.class.getResourceAsStream(string); SAXReader reader=new SAXReader(); Document document=reader.read(in); List<Element> list= document.selectNodes("/config/action"); for (Element element : list) { actionmodel=new Actionmodel(); //给actionmodel对象填充xml中的action标签的内容 actionmodel.setPath(element.attributeValue("path")); actionmodel.setType(element.attributeValue("type")); List<Element> list2= element.selectNodes("forward"); for (Element element2 : list2) { forwardModel =new ForwardModel(); //给forwardmodel对象填充xml中的action标签的内容 forwardModel.setName(element2.attributeValue("name")); forwardModel.setPath(element2.attributeValue("path")); forwardModel.setRedirect(!"false".equals(element2.attributeValue("redirect"))); //<forward name="failed" path="/reg.jsp" redirect="false" /> //resirect默认是true //只有填了false才是转发 //element2.attributeValue("redirect")拿到的是xml中你所填的值 //不填 重定向 "false".equals(element2.attributeValue("redirect"))是false // 填 true 定向 "false".equals(element2.attributeValue("redirect"))是false //填false 转发 false".equals(element2.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"); // System.out.println(actionmodel.getType()); ForwardModel forwardModel=actionmodel.pop("success"); System.out.println(actionmodel.getType()); System.out.println(forwardModel.getPath()); } }
注释:
本项目一共导用了两个jar包:dom4j-1.6.1.jar jaxen-1.1-beta-6.jar