XML建模

目录

一、导入相关jar包

二、 创建相关的包和MVC文件

三、根据config.xml格式创建相关的模型类

四、自定义异常

Action元素中存在重复定义异常:

Forward元素中存在重复定义异常:

五、模型代码的编写

ConfigModel

ActionModel

ForwardModel

六、工厂类ConfigModelFactory编写


一、导入相关jar包

二、 创建相关的包和MVC文件

包名规范:com/org.公司名.项目名.模块名

  1. com.pf.mymvc.framework:框架包
  2. resources源码包下的文件:放自定义MVC的配置文件config.xml

config.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.xml是建模的依据

三、根据config.xml格式创建相关的模型类

模型类命名:

  • ConfigModel:Config节点模型
  • ActionModel:Action节点模型
  • ForwardModel:Farword节点模型

类分布图:

 

四、自定义异常

自定义异常可以让我们的项目变的更加规范

Action元素中存在重复定义异常:

  • ActionDuplicateDefinitionException:
package com.pf.mymvc.framework;
/**
 * 自定义异常(重复)
 * @author zjjt
 *
 */
public class ActionDuplicateDefinitionException extends RuntimeException{
	//RuntimeException运行时异常
	
	public ActionDuplicateDefinitionException() {
		super();
	}
	
	public ActionDuplicateDefinitionException(String msg) {
		super(msg);
	}
	
	public ActionDuplicateDefinitionException(String msg,Throwable c) {
		super(msg,c);
	}

}
  • ActionNotFoundException:
package com.pf.mymvc.framework;
/**
 * 自定义异常
 * @author zjjt
 *
 */
public class ActionNotFoundException extends RuntimeException{
	
	public ActionNotFoundException() {
		super();
	}
	
	public ActionNotFoundException(String msg) {
		super(msg);
	}
	
	public ActionNotFoundException(String msg,Throwable c) {
		super(msg,c);
	}

}

Forward元素中存在重复定义异常:

  •  ForwardDuplicateDefinitionException:
package com.pf.mymvc.framework;
/**
 * 自定义异常(重复)
 * @author zjjt
 *
 */
public class ForwardDuplicateDefinitionException extends RuntimeException{
	//RuntimeException运行时异常
	
	public ForwardDuplicateDefinitionException() {
		super();
	}
	
	public ForwardDuplicateDefinitionException(String msg) {
		super(msg);
	}
	
	public ForwardDuplicateDefinitionException(String msg,Throwable c) {
		super(msg,c);
	}

}
  • ForwardNotFoundException:
package com.pf.mymvc.framework;
/**
 * 自定义异常
 * @author zjjt
 *
 */
public class ForwardNotFoundException extends RuntimeException{
	
	public ForwardNotFoundException() {
		super();
	}
	
	public ForwardNotFoundException(String msg) {
		super(msg);
	}
	
	public ForwardNotFoundException(String msg,Throwable c) {
		super(msg,c);
	}

}

五、模型代码的编写

思路:根据config.xml,代码的关系,有属性的需要在类中定义属性,前一个类拿到后一个类中的数值

  • ConfigModel

package com.pf.mymvc.framework;

import java.util.HashMap;
import java.util.Map;

public class ConfigModel {
	
	private Map<String,ActionModel> actionMap = new HashMap<>();
	
	public void put(ActionModel action) {
		//actionMap容器中有没有包含action.getPath()元素
		//action.getPath()是在ActionModel中定义的
		if(actionMap.containsKey(action.getPath())) {
			//如果出现了重复,直接抛出一个异常出去,并给出一个提示
			throw new ActionDuplicateDefinitionException("Action path="+action.getPath()+"duplicate definition");
		}
		//如果没有重复,那就直接存入
		actionMap.put(action.getPath(), action);
	}
	
	/**
	 * 根据path拿到对应的actionMap,然后就可以拿到type了
	 * @param path
	 * @return
	 */
	public ActionModel find(String path) {
		if(!actionMap.containsKey(path)) {
			throw new ActionNotFoundException("Action path="+path+"not found");
		}
		return actionMap.get(path);
	}
	
	

}

为什么不用list?

private Map<String,ActionModel> actionMap;

一个请求进来,要根据请求的路径找到对应的类型,找的时候一定会用到循环,而用map不需要写这个循环,就更方便

  • ActionModel

package com.pf.mymvc.framework;

import java.util.HashMap;
import java.util.Map;

public class ActionModel {
	
	private String path;
	
	private String type;
	
	private Map<String, ForwardModel> forwardMap = 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(forwardMap.containsKey(forward.getName())) {
			throw new ForwardDuplicateDefinitionException("forward name="+forward.getName()+"duplicate definition");
		}
		forwardMap.put(forward.getName(), forward);
	}
	
	public ForwardModel find(String name) {
		if(!forwardMap.containsKey(name)) {
			throw new ForwardNotFoundException("forward name="+name+"not found");
		}
		return forwardMap.get(name);
	}

	@Override
	public String toString() {
		return "ActionModel [path=" + path + ", type=" + type + ", forwardMap=" + forwardMap + "]";
	}
	
	

}
  • ForwardModel

package com.pf.mymvc.framework;

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 + "]";
	}
	
	

}

六、工厂类ConfigModelFactory编写

编写工厂类ConfigModelFactory的目的是在这个类中使用单例模式,并且只对XML文件中的内容读取一次,读取后放入上一步编写好了的模型对象中

package com.pf.mymvc.framework;

import java.io.InputStream;
import java.util.List;

import javax.servlet.jsp.jstl.core.Config;
import javax.sql.rowset.spi.XmlReader;

import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

/**
 * 单例
 * @author zjjt
 *
 */
@SuppressWarnings("unchecked")
public final class ConfigModelFactory {
	
	private ConfigModelFactory() {}
	
	private static ConfigModel config = new ConfigModel();
	
	//读取config.xml中的数据,填充到模型中
	static {
		try {
			InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(in);
			
			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> forwards = e.selectNodes("forward");
				for(Element f:  forwards) {
					String name = f.attributeValue("name");
					String fPath = f.attributeValue("path");
					String redirect = f.attributeValue("redirect");
					
					ForwardModel forward = new ForwardModel();
					forward.setName(name);
					forward.setPath(fPath);
					forward.setRedirect(redirect);
					
					action.put(forward);
				}
				
				config.put(action);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//私有构造函数,修饰成static直接通过类名调用
	public static ConfigModel getConfig() {
		return config;
	}
	
	//测试方法
	public static void main(String[] args) {
		ConfigModel config = ConfigModelFactory.getConfig();
		ActionModel action = config.find("/studentAction");
		System.out.println(action);
		ForwardModel forward = action.find("students");
		System.out.println(forward);
	}

}

注:测试的时候,需要在模型类中使用tostring方法(这只是方便测试用,不写也没有关系)

测试结果图:

今天就分享到这里咯,下次见,我的宝

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值