一.xml建模
1.1什么是建模
以面向对象的思想 ,XML配置文件中的元素、属性、文本信息转换成对象的过程其实就是建模。
1.2建模
- ForwardModel类:xml文件中的Forward节点模型
- ActionModel类:xml文件中的Action节点模型
- ConfigModel类:xml文件中的Config节点模型
建模思路:
1.将config.xml进行解析
2.对应标签的内容将其封装赋值给相应的对象
forward标签值 赋值给forwardmodel对象
action标签值 赋值给actionmodel对象
config标签值 赋值给configmodel对象
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; } }
ActionModel类:
public class ActionModel { private String path; private String type; private Map<String,ForwardModel> fmap=new HashMap<String,ForwardModel>(); 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; } // 将forward往action中放压栈 public void push(ForwardModel forwardModel) { fmap.put(forwardModel.getName(),forwardModel); } // 从action中通过name值取出foward public ForwardModel pop(String name) { return fmap.get(name); } }
ConfigModel类:
public class ConfigModel { private Map<String,ActionModel> amap=new HashMap<String, ActionModel>(); public void push(ActionModel actionModel) { amap.put(actionModel.getPath(),actionModel); } public ActionModel pop(String path) { return amap.get(path); } 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(forwardModel.getPath()); } }
二.工厂模式
ConfigModelFactory类:
23种设计模式--工厂模式--创建型模式
ConfigModelFactory就是用来生产configmodel内容的
public class ConfigModelFactory { public static ConfigModel build() throws Exception { ConfigModel configModel=new ConfigModel(); InputStream is = ConfigModelFactory.class.getResourceAsStream("/mvc.xml"); SAXReader sa=new SAXReader(); Document doc = sa.read(is); // System.out.println(doc.asXML()); List<Element> actionEls = doc.selectNodes("/config/action"); for (Element actionEl : actionEls) { // System.out.println(actionEl.asXML()); ActionModel actionModel=new ActionModel(); actionModel.setPath(actionEl.attributeValue("path")); actionModel.setType(actionEl.attributeValue("type")); List<Element> forwardEls = doc.selectNodes("/config/action/forward"); for (Element forwardEl : forwardEls) { System.out.println(forwardEl.asXML()); ForwardModel forwardModel=new ForwardModel(); forwardModel.setName(forwardEl.attributeValue("name")); forwardModel.setPath(forwardEl.attributeValue("path")); // 只有填false才能转发,其它都是重定向 forwardModel.setRedirect(!"false".equals(forwardEl.attributeValue("redirect"))); actionModel.push(forwardModel); } configModel.push(actionModel); } return configModel; } public static void main(String[] args) throws Exception { ConfigModelFactory.build(); } }