1.建模的由来
就是将指定的xml字符串当作对象来操作
如果说当对一个指定的xml格式字符串完成了建模操作, 好处在于,只需要调用指定的方法就可以完成预定的字符串获取;
2.建模的思路
1、分析需要被建模的文件中有那几个对象 2、每个对象拥有的行为以及属性 3、定义对象从小到大(从里到外) 4、通过23种的设计模式中的工厂模式,解析xml生产出指定对象 好处: 提高代码的复用性
分析XML文件(有几个对象以及属性,定义对象从小到大(从里到外))
定义XML模型对象
1.ForwardModel
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; }
2.ActionModel
public class ActionModel {//private String path; private String type; private Map 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 Map getFmap() { return fmap; } public void setFmap(Map fmap) { this.fmap = fmap; } //添加 public void push(ForwardModel forwardModel) { fmap.put(forwardModel.getName(), forwardModel); } //通过name获取对象 public ForwardModel pop(String name) { return fmap.get(name); }}
ConfigModel
private Mapamap=new HashMap(); public void push(ActionModel actionModel) { amap.put(actionModel.getPath(), actionModel); } public ActionModel pop(String path) { return amap.get(path); }
ConfigModelFactory(工厂模式用来将资源文件生产指定的实体类)
public class ConfigModelFactory { public static ConfigModel build() throws Exception { return configModel("config.xml"); } /** * 生产出有内容的实体类configModel * @param xmlPath * @return * @throws Exception */ public static ConfigModel configModel(String xmlPath) throws Exception { ConfigModel configModel=new ConfigModel(); ActionModel actionModel=null; ForwardModel forwardModel=null; InputStream in=ConfigModelFactory.class.getResourceAsStream(xmlPath); SAXReader sax=new SAXReader(); Document doc=sax.read(in); ListactionEles= 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 forwardEles=actionEle.selectNodes("forward"); for (Element forwardEle : forwardEles) { forwardModel=new ForwardModel(); //给forwardModel对象填充xml中的forward标签的内容 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 // 填flase 转发 "false".equals(forwardEle.attributeValue(("redirect")))true; actionModel.push(forwardModel); } configModel.push(actionModel); } return configModel; } public static void main(String[] args) throws Exception { ConfigModel configModel=ConfigModelFactory.build(); ActionModel actionModel=configModel.pop("/loginAction"); ForwardModel forwardModel=actionModel.pop("success"); System.out.println( actionModel.getType()); System.out.println(forwardModel.getPath()); }}
对web.xml进行建模,链接