xml建模

1.建模的由来java

就是将指定的xml字符串看成对象来操做
若是说当对一个指定的xml格式字符串完成了建模操做,
好处在于,只须要调用指定的方法就能够完成预约的字符串获取web

 

2.建模的思路
一、分析须要被建模的文件中有那几个对象
二、每一个对象拥有的行为以及属性
三、定义对象从小到大(从里到外)
四、经过23种的设计模式中的工厂模式,解析xml生产出指定对象

好处:
提升代码的复用性设计模式

 

分析XML文件(有几个对象以及属性,定义对象从小到大(从里到外))jsp

<?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>

 

定义XML模型对象ui

1.ForwardModelthis

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; } 

  

2.ActionModelspa

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 Map<String, ForwardModel> getFmap() { return fmap; } public void setFmap(Map<String, ForwardModel> 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 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); } 

 

ConfigModelFactory(工厂模式用来将资源文件生产指定的实体类)
code

 

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); 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中的forward标签的内容 forwardModel.setName(forwardEle.attributeValue("name")); forwardModel.setPath(forwardEle.attributeValue("path")); forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue(("redirect")))); // <forward name="failed" path="/reg.jsp" redirect="false" /> // 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进行建模,连接 https://i.cnblogs.com/Files.aspxxml

相关文章
相关标签/搜索