1.使用XML传输数据的原因:
因为项目发布后xml基本不会变了,如果不建模每次都要去读资源,这样会造成系统资源的浪费、服务器的压力、同时造成了不必要的麻烦
建模的话只把读取文件的那一步只读取一遍,然后放到静态块里面,把每个xml标签转为java对象,去内存里面读取java对象,通过工厂方法生产最大java对象(这里相当于xml里面的根元素)
2.建模实际上就是根据XML文件来定义一个或多个对象
3.建模步骤
3.1XML文件<?xml version="1.0" encoding="UTF-8"?> <!-- 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>
3.2按子标签-->父标签-->父标签的父标签...的顺序开始创建模型对象
标签:forwardpackage com.JavaEE_Day06.Entity; import java.io.Serializable; public class ForwardModel implements Serializable { //属性 模型对象的属性应对应标签的属性,若是文本标签,则还要设置一个属性来保存标签内文本 private String name; private String path; private boolean redirect; //构造函数(无参) public ForwardModel() { // TODO Auto-generated constructor stub } //构造函数(有参) public ForwardModel(String name, String path, boolean redirect) { super(); this.name = name; this.path = path; this.redirect = redirect; } //get/set方法 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; } public void setRedirect(String redirect) { this.redirect = Boolean.parseBoolean(redirect); } //toString @Override public String toString() { return "forward [name=" + name + ", path=" + path + ", redirect=" + redirect + "]"; } }
标签:Action
package com.JavaEE_Day06.Entity; import java.io.Serializable; import java.util.HashMap; import java.util.Map; public class ActionModel implements Serializable { private String path; private String type; private String sex; //使用Map集合进行存储 private Map<String,ForwardModel> ForwardModels = new HashMap<String, ForwardModel>(); //构造函数(无参) public ActionModel(); //get/set 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 String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } //添加forward对象 public void put(ForwardModel forwardModel) { if(this.ForwardModels.containsKey(forwardModel.getName())){ throw new RuntimeException("键位["+forwardModel.getName()+"]已存在"); } this.ForwardModels.put(forwardModel.getName(), forwardModel); } //获取forward对象 public ForwardModel get(String name) { if(null==this.ForwardModels.get(name)) { throw new java.lang.RuntimeException("键位["+name+"]没有值"); } return this.ForwardModels.get(name); } //toString @Override public String toString() { return "ActionModel [path=" + path + ", type=" + type + ", sex=" + sex + ", ForwardModels=" + ForwardModels + "]"; } }
注:使用map集合是因为,标签action的path属性必须唯一
标签:config
package com.JavaEE_Day06.Entity; import java.io.InputStream; import java.io.Serializable; import java.util.HashMap; import java.util.List; import java.util.Map; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class ConfigModel implements Serializable { private Map<String,ActionModel> ConfigModel = new HashMap<String, ActionModel>(); //默认的读取路径 private static final String PATH = "/XML/config.xml"; //添加ActionModel对象 public void put(ActionModel actionModel) { if(this.ConfigModel.containsKey(actionModel.getPath())) { throw new RuntimeException("键位["+actionModel.getPath()+"]已存在"); } this.ConfigModel.put(actionModel.getPath(), actionModel); } //获取ActionModel对象 public ActionModel get(String path) { if(null==this.ConfigModel.get(path)) { throw new RuntimeException("键位["+path+"]不存在"); } return this.ConfigModel.get(path); } //封装构造函数 private ConfigModel() { // TODO Auto-generated constructor stub } //获取ConfigModel的方法,保证每次创建的ConfigModel中都有其子标签及子标签中的数据 //path:读取路径 public static ConfigModel createConfigModel(String path) throws DocumentException { ConfigModel configModel = new ConfigModel(); SAXReader sax = new SAXReader(); if(null==path) { InputStream is = ConfigModel.class.getResourceAsStream(PATH); Document ConfigModel = sax.read(is); //得到config标签中所有的的action标签 List <Element> ActionModel = ConfigModel.selectNodes("/config/action"); //遍历action标签 for (Element e : ActionModel) { //取出属性 ActionModel actionModel = new ActionModel(); actionModel.setPath(e.attributeValue("path")); actionModel.setType(e.attributeValue("type")); actionModel.setSex(e.attributeValue("sex")); //得到此action标签下所有的forward标签 List<Element> forward = e.selectNodes("forward"); //遍历forward标签 for (Element f : forward) { //取出属性值 ForwardModel forwardModel = new ForwardModel(); forwardModel.setPath(f.attributeValue("path")); forwardModel.setName(f.attributeValue("name")); forwardModel.setRedirect(f.attributeValue("redirect")); //将此forward加入对应的ActionModel对象中 actionModel.put(forwardModel); } //将此ActionMoel对象加入configModel中 configModel.put(actionModel); } return configModel;//放回加载数据后的configModel }else { InputStream is = ConfigModel.class.getResourceAsStream(path); Document ConfigModel = sax.read(is); List <Element> ActionModel = ConfigModel.selectNodes("/config/action"); //遍历action标签 for (Element e : ActionModel) { ActionModel actionModel = new ActionModel(); actionModel.setPath(e.attributeValue("path")); actionModel.setType(e.attributeValue("type")); actionModel.setSex(e.attributeValue("sex")); List<Element> forward = e.selectNodes("forward"); //遍历forward标签 for (Element f : ActionModel) { ForwardModel forwardModel = new ForwardModel(); forwardModel.setPath(f.attributeValue("path")); forwardModel.setName(f.attributeValue("name")); forwardModel.setRedirect(f.attributeValue("redirect")); actionModel.put(forwardModel); } configModel.put(actionModel); } return configModel; } } }
注:action的构造函数也要封装,这里不封装是因为我懒得封
最后每次调用createConfigModel的时候,都会直接将XMl解析完成