1.什么是XML建模
XML建模是使用可扩展标记语言(XML)来定义和描述数据的结构的过程。
即:将XML配置文件中的元素,属性,文本信息转换成对象的过程叫做XML建模。
这种建模方法通过创建XML文档来描述数据模型。
1.1建模的思路:
1、将原有的config.xml进行解析
2、对应标签的内容,将其封装赋值给相应的对象
3、定义对象从小到大(从里到外)
2.封装实体类
案例:
config.xml文件:
<?xml version="1.0" encoding="UTF-8"?> <!-- config标签:可以包含0~N个action标签 --> <config> <!-- 以面向对象的思想形容/封装模型对象----即配置文件 .xml --> <!-- 1.往config标签中添加了一个action标签 2.往action标签中添加了4个forward标签 3.将action标签的属性值path,type发生了更改 4.将forward标签的属性值path,type,redirect发生了更改 --> <!-- 以面向对象的思想思考上述操作: 1.configModel对象中添加一个actionModel对象,因为actionModel在configModel中是唯一的,所以在 configModel中必然有个属性(容器),能够以一对一的方式存储不同个actionModel,还有添加方法. 2.actionModel也有一个属性(容器Map),也有个添加方法,查询方法 3.actionModel还有属性path,type. 4.forwardModel有属性name,path,redirect. --> <action path="/registerAction" type="test.action.RegisterAction"> <forward name="success" path="/index.jsp" redirect="true" /> <forward name="failed" path="/register.jsp" redirect="false" /> </action> <action path="/registerAction2" type="test.action.RegisterAction"> <forward name="success" path="/index.jsp" redirect="true" /> <forward name="failed" path="/register.jsp" redirect="false" /> </action> <action path="/bookAction" type="test.action.BookAction"> <forward name="add" path="/add.jsp" redirect="true" /> <forward name="del" path="/reg.jsp" redirect="true" /> <forward name="list" path="/list.jsp" redirect="true" /> <forward name="upd" path="/login.jsp" redirect="false" /> </action> <action path="/loginAction" type="test.action.LoginAction"> <forward name="a" path="/index.jsp" redirect="false" /> <forward name="b" path="/welcome.jsp" redirect="true" /> </action> </config>
首先我们可以看到案例里面有三个标签:1.config 2.action 和 3.forward 所以我们需要三个实体类,将其封装属性以及方法。分别如下:
2.1ForwardModel类
package Kissship.model; 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.2ActionModel类
package Kissship.model; 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.3ConfigModel类
package Kissship.model; import java.util.HashMap; import java.util.Map; 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); } }
当我们做好以上这一步后我们我们需要将XML的内容初始化到描述出的对象模型中去。
这就涉及到我们的第三个知识点:
3.工厂模式
23种设计模式————工厂模式,属于创建型模式
ConfigModelFactory(工厂类) :
package Kissship.model; import java.io.InputStream; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; //23种设计模式----工厂模式----创建型模式 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")); // ActionModel要有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(actionEle.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 Exception { return build("/mvc.xml"); } public static void main(String[] args) throws Exception { ConfigModelFactory.build(); } }
这里我在写代码的时候遇到个问题,代码如下:
package Kissship.model; import java.io.InputStream; import org.dom4j.Document; import org.dom4j.io.SAXReader; //23种设计模式----工厂模式----创建型模式 public class ConfigModelFactory { public static ConfigModel build() throws Exception { 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()); return configModel; } public static void main(String[] args) throws Exception{ ConfigModelFactory.build(); } }
我要拿到xml文件中的内容,但是控制台输出报错。如下:
报错的错误原因:
把mvc.xml放在conf中后就可以输出了:
最后J2EE之XML建模(超详细)就到这里,祝大家在敲代码的路上一路通畅!