目录
XML建模步骤
1.什么是建模?
建模就是从现实世界中抽象出我们的目标,在这一过程中,保留相关因素,剔除无关因素,从而直观地表示出问题。例如我们所编写的学生类,书本类....这些实体类,其实就是建模。
2.导入jar包
将下方这些jar包导入lib路径下
3. 创建XML文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[
<!ELEMENT config (action*)>
<!ELEMENT action (forward*)>
<!ELEMENT forward EMPTY>
<!ATTLIST action
path CDATA #REQUIRED
type CDATA #REQUIRED
>
<!ATTLIST forward
name CDATA #REQUIRED
path CDATA #REQUIRED
redirect (true|false) "false"
>
]>
<config>
<action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
<forward name="students" path="/students/studentList.jsp" redirect="false"/>
</action>
<action path="/bookAction" type="org.lisen.mvc.action.bookAction">
<forward name="books" path="/books/bookList.jsp" redirect="false"/>
</action>
</config>
4.根据XML文件中的元素创建模型类
- ConfigModel类:xml文件中的Config节点模型
- ActionModel类:xml文件中的Action节点模型
- ForwardModel类:xml文件中的Forward节点模型
-
ConfigModel类
我们观察xml文件中的Config该元素是没有属性的,但是它的下面有多个action节点,定义一个集合,该集合存放action。
put方法:put方法是将action加入map集合中,xml文件中action节点有一个属性path,将path的值为map集合中的key值,这样子我们根据path属性的值,就可以查找到是哪个action。使用path是不可以相同的。
find(String path)方法:根据传入的action的path属性的值,查找是哪个action。
package com.yjx.test; import java.util.HashMap; import java.util.Map; public class ConfigModel { //定义一个集合,该集合存放action数据 private Map<String, ActionModel> actionMap=new HashMap(); /** * 将action加入集合中 * @param path */ public void put(ActionModel action) { //判断集合中是否已经存在该action了,如果path存在就抛出异常,不存在就将action加入集合中 if(actionMap.containsKey(action.getPath())) { throw new ActionDuplicateDefinitionException("Action path = "+ action.getPath()+" duplicate definition"); } actionMap.put(action.getPath(), action); } /** * 根据path属性的值查询action * @param path * @return */ public ActionModel find(String path) { //判断是否存在,不存在就抛出异常 if(!actionMap.containsKey(path)) { throw new ActionNoutFoundException("Action path = "+ path +" not found"); } return actionMap.get(path); } }
在该类中我们所用到的抛出异常,是自定义异常。代码如下:
- ActionDuplicateDefinitionException异常
package com.yjx.test; public class ActionDuplicateDefinitionException extends RuntimeException{ public ActionDuplicateDefinitionException() { super(); } public ActionDuplicateDefinitionException(String msg) { super(msg); } public ActionDuplicateDefinitionException(String msg, Throwable c) { super(msg, c); } }
- ActionNoutFoundException异常
package com.yjx.test; public class ActionNoutFoundException extends RuntimeException{ public ActionNoutFoundException() { } public ActionNoutFoundException(String message) { super(message); } public ActionNoutFoundException(String message, Throwable cause) { super(message, cause); } }
-
ActionModel类
观察xml文件action节点具备的属性在该类中定义,action节点下可以具备多个Forward节点,所以需要一个集合将Forward节点全部存入。
package com.yjx.test; import java.util.HashMap; import java.util.Map; public class ActionModel { private String path; private String type; private Map<String, ForwardModel> foreardMpdel=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 void put(ForwardModel forward) { if(foreardMpdel.containsKey(forward.getName())) { throw new ForwardDuplicateDefinitionException("forward name="+forward.getName()+ " duplicate definition"); } foreardMpdel.put(forward.getName(), forward); } public ForwardModel find(String name) { if(!foreardMpdel.containsKey(name)) { throw new ForwardNotFoundException("forward name "+name+"not found"); } return foreardMpdel.get(name); } @Override public String toString() { return "ActionModel [path=" + path + ", type=" + type + ", foreardMpdel=" + foreardMpdel + "]"; } }
在actionModel中自定义了两个异常,代码如下:
- ForwardDuplicateDefinitionException异常
package com.yjx.test; public class ForwardDuplicateDefinitionException extends RuntimeException{ public ForwardDuplicateDefinitionException() { // TODO Auto-generated constructor stub } public ForwardDuplicateDefinitionException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public ForwardDuplicateDefinitionException(String message) { super(message); // TODO Auto-generated constructor stub } }
- ForwardNotFoundException异常
package com.yjx.test; public class ForwardNotFoundException extends RuntimeException{ public ForwardNotFoundException() { // TODO Auto-generated constructor stub } public ForwardNotFoundException(String message, Throwable cause) { super(message, cause); // TODO Auto-generated constructor stub } public ForwardNotFoundException(String message) { super(message); // TODO Auto-generated constructor stub } }
-
ForwardModel类
观察xml文件Forward节点具备的属性在该类中定义。因为Forward节点下面没有别的节点,所以我们也不需要定义一个集合啦
package com.yjx.test; 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; } public void setRedirect(String redirect) { this.redirect = Boolean.valueOf(redirect); } @Override public String toString() { return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]"; } }
4.工厂类的编写
package com.yjx.test;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
public class XmlRead {
private XmlRead() {
}
private static ConfigModel config=new ConfigModel();
static {
//读取xml文件
InputStream in=XmlRead.class.getResourceAsStream("config.xml");
SAXReader reads=new SAXReader();
//读取该文件得到一个文档
Document doc = null;
try {
doc = reads.read(in);
} catch (DocumentException e1) {
e1.printStackTrace();
}
//得到元素
Element el=doc.getRootElement();
//获取到action该节点
List<Element> action=el.selectNodes("action");
for (Element e : action) {
//获取到action元素的
String path=e.attributeValue("path");
String type=e.attributeValue("type");
ActionModel actionModel=new ActionModel();
actionModel.setPath(path);
actionModel.setType(type);
//在获取到action下的节点forward
List<Element> forward=e.selectNodes("forward");
for(Element f:forward) {
//获取到forward节点的属性
String name=f.attributeValue("name");
String path01=f.attributeValue("path");
String redirect=f.attributeValue("redirect");
ForwardModel forwar=new ForwardModel();
forwar.setName(name);
forwar.setPath(path);
forwar.setRedirect(redirect);
actionModel.put(forwar);
}
config.put(actionModel);
}
}
public static ConfigModel getConfig() {
return config;
}
//测试
public static void main(String[] args) {
ConfigModel config=XmlRead.getConfig();
ActionModel action=config.find("/studentAction");
ForwardModel forward = action.find("students");
System.out.println(forward);
}
}
今天的学习就到这里啦!!!