xml建模

简介: xml建模
  • 面向对象的方式去操作/获取xml文件里的内容

面向对象的方式去操作/获取xml文件里的内容

1.以面向对象的的思想形容/封装模型对象,形容的是xml文件

往config标签中添加了一个action

往action标签中添加了4个forward标签

将action标签的属性值path、type发生了更改

将forward标签的属性值name、path、redirect发生了更改

1.1以面向对象的思想思考上述操作

configModel对象中添加一个actionModel对象,因为actionModel在configModel中是唯一的

所以在configModel中必然有一个属性(容器Map),能够以一对一的方式存储不同个------------actionModel,还有添加方法,查询方法

actionMondel也有一个属性(容器Map),也有添加方法,查询方法

actionMondel还有属性path、type

forwardModel还有属性name、path、redirect

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <action path="/regAction" type="test.RegAction">
    <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="add" path="/bookAdd.jsp" redirect="false" />
    <forward name="del" path="/reg.jsp" redirect="true" />
    <forward name="list" path="/list.jsp" redirect="false" />
    <forward name="upd" path="/login.jsp" redirect="false" />
  </action>
</config>
package com.zhulinjun;
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;
}
}
package com.zhulinjun;
import java.util.HashMap;
import java.util.Map;
public class ConfigMondel {
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);
}
}
package com.zhulinjun;
import java.util.HashMap;
import java.util.Map;
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值取出forward
public ForwardModel pop(String name) {
  return fMap.get(name);
}
}

2.将xml的内容初始化到描述出的模型对象中(23种设计模式--工厂模式--创建型模式)

package com.zhulinjun;
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;
public class ConfigModelFactory {
  //动态
  public static ConfigModel build(String xmlPath) throws Exception {
    ConfigModel configModel=new ConfigModel();
    InputStream in = ConfigModelFactory.class.getResourceAsStream(xmlPath);
    SAXReader sr=new SAXReader();
    Document doc = sr.read(in);
//    System.out.println(doc.asXML());
  //  configModel要有类容,就意味着actionModel要有类容,然后放入到configModel中去
    List<Element> actionEles = doc.selectNodes("/config/action");
    for (Element actionEle : actionEles) {
//      System.out.println(actionEle.asXML());
      ActionModel actionModel = new ActionModel();
      actionModel.setPath(actionEle.attributeValue("path"));
      actionModel.setType(actionEle.attributeValue("type"));
  //  configModel要有forwardModel,就意味着forwardModel要有类容,然后放入到actionModel中去
      List<Element> forwardEles = actionEle.selectNodes("forward");
      for (Element forwardEle: forwardEles) {
        System.out.println(forwardEle.asXML());
        ForwardModel forwardModel = new ForwardModel();
        forwardModel.setName(forwardEle.attributeValue("name"));
        forwardModel.setPath(forwardEle.attributeValue("path"));
//        只有false才是转发,其他都为重定向
//        forwardModel.setRedirect(false);
        forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
        actionModel.push(forwardModel);
      }
      configModel.push(actionModel);
    }
    return configModel;
  }
  //静态
//public static ConfigModel build() throws DocumentException {
//  ConfigModel configModel=new ConfigModel();
//  InputStream in = ConfigModelFactory.class.getResourceAsStream("/mvc.xml");
//  SAXReader sr=new SAXReader();
//  Document doc = sr.read(in);
  System.out.println(doc.asXML());
  configModel要有类容,就意味着actionModel要有类容,然后放入到configModel中去
//  List<Element> actionEles = doc.selectNodes("/config/action");
//  for (Element actionEle : actionEles) {
    System.out.println(actionEle.asXML());
//    ActionModel actionModel = new ActionModel();
//    actionModel.setPath(actionEle.attributeValue("path"));
//    actionModel.setType(actionEle.attributeValue("type"));
  configModel要有forwardModel,就意味着forwardModel要有类容,然后放入到actionModel中去
//    List<Element> forwardEles = actionEle.selectNodes("forward");
//    for (Element forwardEle: forwardEles) {
//      System.out.println(forwardEle.asXML());
//      ForwardModel forwardModel = new ForwardModel();
//      forwardModel.setName(forwardEle.attributeValue("name"));
//      forwardModel.setPath(forwardEle.attributeValue("path"));
      只有false才是转发,其他都为重定向
      forwardModel.setRedirect(false);
//      forwardModel.setRedirect(!"false".equals(forwardEle.attributeValue("redirect")));
//      actionModel.push(forwardModel);
//    }
//    
//    configModel.push(actionModel);
//  }
//  
//  return configModel;
//}
public static void main(String[] args) throws DocumentException {
//  ConfigModelFactory.build();
}
}


目录
相关文章
|
4月前
|
XML 设计模式 开发框架
J2EE(xml建模)
J2EE(xml建模)
34 0
|
5月前
|
XML 存储 设计模式
XML系列(三)之 XML建模
XML系列(三)之 XML建模
|
5月前
|
XML 设计模式 开发框架
J2EE之XML建模(超详细)
J2EE之XML建模(超详细)
31 0
|
5月前
|
XML Java 数据格式
一.Xml建模之入门
一.Xml建模之入门
32 1
|
5月前
|
XML 存储 JSON
XML建模最详细介绍讲解
XML建模最详细介绍讲解
40 0
|
4月前
|
XML 存储 开发框架
J2EE之XML建模
J2EE之XML建模
23 0
|
6月前
|
XML 存储 设计模式
XML建模
XML建模
34 0
|
5月前
|
XML 数据采集 JSON
XML建模
XML建模
30 0
|
3月前
|
XML 存储 数据库
xml建模----详细完整,易懂结合代码分析
xml建模----详细完整,易懂结合代码分析
|
3月前
|
XML 数据格式
XML-----建模
XML-----建模
20 0

相关产品

  • 云迁移中心
  • 相关课程

    更多