导语:
回顾上一期的XML的解析,我们会发现我们的读取太麻烦了,所以我们通才采取使用模型的方式来解析XML。
那怎么来建立这个模型呢,下面我们一起来看看
步骤
找到自己的XML文件,先分析一下,我先提供一个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>
</config>
我们看到在config下有action,action中有path,type,在action下有forward,forward中有name,path,redirect,这样一看是不是结构就很清晰了,下面我们在一步步来。
首先先写出configmodel,actionmodel,forwardmodel,为了表明他们之间的层级关系,我们先定义好其中的属性,actionmodel中有path,type;forwardmodel中有name,path,redirect,在分别提供get,set方法。
根据上面我们的分析,我们开始建立模型
类名规范:首字母大写的驼峰命名法
模型类命名:
- ConfigModel:Config节点模型
- ActionModel:Action节点模型
- ForwardModel:Farword节点模型
ForwardModel
public class ConfigModel {
// 一个config节点下可以有多个action节点
// 放key==path和value==ActionModel类/对象:通过根节点找到action
// 初始化:new HashMap<>()
private Map<String, ActionModel> actionMap=new HashMap<>();
/**
* 将path放入key,保持path不重复
* 遍历ActionModel对象,通过ActionModel对象获取path属性
* @param 传入ActionModel这个类/对象
*/
public void put(ActionModel action) {
if(actionMap.containsKey(action.getPath())) {
throw new ActionDuplicateDefinitionException("Action path= "+action.getPath()+" Duplicate definition");
}public class ForwardModel extends RuntimeException{
//三个属性
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;
}
//将redirect中的字符串转成boolean类型的方法
public void setRedirect(String redirect) {
//this=当前类
this.redirect =Boolean.valueOf(redirect);
}
@Override
public String toString() {
return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
}
}
actionMap.put(action.getPath(), action);
}
/**
* 找path
* @param 传入path
* @return 找不到path则抛出异常,找到就返回path
*/
public ActionModel find(String path) {
if(!actionMap.containsKey(path)) {
throw new ActionNotFoundException("Action path ="+path+" not found");
}
return actionMap.get(path);
}
}
ActionModel:
public class ActionModel {
// 有两个节点
private String type;
private String path;
// 包含元素forward:通过action找到forward
private Map<String, ForwardModel> forwardMap =new HashMap<>();
// 将name放入key,保持name不重复
public void put(ForwardModel forward) {
if(forwardMap.containsKey(forward.getName())) {
throw new ForwardDuplicateDefinitionException("forward name = "+forward.getName()+" DuplicateDefinition");
}
forwardMap.put(forward.getName(), forward);
}
// 找name
public ForwardModel find(String name) {
if(!forwardMap.containsKey(name)) {
throw new ForwardNotFoundException("forward name ="+name+" not found");
}
return forwardMap.get(name);
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
@Override
public String toString() {
return "ActionModel [type=" + type + ", path=" + path + ", forwardMap=" + forwardMap + "]";
}
}
ConfigModel:
public class ConfigModel {
// 一个config节点下可以有多个action节点
// 放key==path和value==ActionModel类/对象:通过根节点找到action
// 初始化:new HashMap<>()
private Map<String, ActionModel> actionMap=new HashMap<>();
/**
* 将path放入key,保持path不重复
* 遍历ActionModel对象,通过ActionModel对象获取path属性
* @param 传入ActionModel这个类/对象
*/
public void put(ActionModel action) {
if(actionMap.containsKey(action.getPath())) {
throw new ActionDuplicateDefinitionException("Action path= "+action.getPath()+" Duplicate definition");
}
actionMap.put(action.getPath(), action);
}
/**
* 找path
* @param 传入path
* @return 找不到path则抛出异常,找到就返回path
*/
public ActionModel find(String path) {
if(!actionMap.containsKey(path)) {
throw new ActionNotFoundException("Action path ="+path+" not found");
}
return actionMap.get(path);
}
}
建立好这些之后我们就开始写我们的单例模式,来控制这些模型
工厂类ConfigModelFactory的目的是在这个类中使用单例模式,并且只对XML文件中的内容读取一次,读取后放入上一步编写好了的模型对象中(包含测试代码)
package com.zking.mvc.farmework;
import java.io.InputStream;
import java.util.List;
import javax.sql.rowset.spi.XmlReader;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
@SuppressWarnings("unchecked")
public final class ConfigModelFactory {
public ConfigModelFactory() {
}
private static ConfigModel config = new ConfigModel();
// 读取config.xml的数据,填充到config的模型之中
static {
try {
InputStream is = ConfigModelFactory.class.getResourceAsStream("/config.xml");
SAXReader reader = new SAXReader();
Document doc = reader.read(is);
Element rootElement = doc.getRootElement();
List<Element> actions = rootElement.selectNodes("action");
for (Element e : actions) {
String path = e.attributeValue("path");
String type = e.attributeValue("type");
ActionModel action = new ActionModel();
action.setPath(path);
action.setType(type);
List<Element> forward = e.selectNodes("forward");
for (Element f : forward) {
String name = f.attributeValue("name");
String pathf = f.attributeValue("path");
String redirect = f.attributeValue("redirect");
ForwardModel forward2 = new ForwardModel();
forward2.setName(name);
forward2.setPath(pathf);
forward2.setRedirect(redirect);
action.put(forward2);
}
config.put(action);
}
} catch (Exception e) {
e.printStackTrace();
}
}
// 单例模式,提供一个获取方法
public static ConfigModel getConfig() {
return config;
}
public static void main(String[] args) {
ConfigModel config = new ConfigModelFactory().getConfig();
ActionModel action = config.find("/studentAction");
System.out.println(action);
ForwardModel forward = action.find("students");
System.out.println(forward);
}
}
完成这些之后就可以开始写我们的自定义异常了
自定义异常
自定义的异常其实关键在于自定义的异常名字,要让别人看到名字就知道是哪里的异常,所以命名很重要,一定要规范;异常类命名+代码编写(关键是异常名称)
例如:ActionDuplicateDefinitionException:Action元素中存在重复定义异常
ActionDuplicateDefinitionException
package com.zking.mvc.farmework;
public class ActionDuplicateDefinitionException extends RuntimeException {
public ActionDuplicateDefinitionException() {
super();
}
public ActionDuplicateDefinitionException(String msg) {
super(msg);
}
public ActionDuplicateDefinitionException(String msg, Throwable c) {
super(msg, c);
}
}
ActionNotFoundException
package com.zking.mvc.farmework;
public class ActionNotFoundException extends RuntimeException {
public ActionNotFoundException() {
super();
}
public ActionNotFoundException(String msg) {
super(msg);
}
public ActionNotFoundException(String msg, Throwable c) {
super(msg, c);
}
}
ActionTopException
package com.zking.mvc.farmework;
public class ActionTopException extends RuntimeException {
public ActionTopException() {
super();
}
public ActionTopException(String msg) {
super(msg);
}
public ActionTopException(String msg, Throwable c) {
super(msg, c);
}
}
还有我们的forward的自定义异常,其实与我们的Action是一样的,所以我就写了,提供一个命名
ForwardDuplicateDefinitionException
ForwardNotFoundException
ForwardTopException
总结:
MXML 的建模关键在于去把XL文件给解析出来,按自己的思路去根据XML来建立模型,就比如我的案例里,config下是action,action下是forward,然后action中又有什么,forward中又有什么,按照这个节点的关系来建模,就很清楚了;自定义异常的关键就是名称,然后就是要继承RuntimeException 这个类,然后按照函数构造链来写就好啦!!!