J2EE从入门到入土07.XML建模

回顾:J2EE从入门到入土06.XML配置文件的读取

目录

ConfigModel

ActionModel

ForwardModel

ActionDuplicateDefinitionException

ActionNotFoundException

ForwardDuplicateDefinitionException

ForwardNotFoundException

ConfigModelFactory


创建根目录resource,把xml文件放到根目录下

 

导入需要用到的jar包

 

开始创建与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>

ConfigModel

public class ConfigModel {
	
	private Map<String,ActionModel> map = new HashMap<String, ActionModel>();
	
	public void put(ActionModel action) {
		
		
		if(map.containsKey(action.getPath())) {
			throw new ActionDuplicateDefinitionException();
		}
		map.put(action.getPath(), action);
	}
	
	public ActionModel find(String path) {
		if(!map.containsKey(path)) {
			throw new ActionNotFoundException();
		}
		return map.get(path);
	}
}

ActionModel

public class ActionModel {
	
	private String path;
	private String type;
	
	private Map<String,ForwardModel> map = new HashMap<String, ForwardModel>();
	
	public void put(ForwardModel forward) {
		if(map.containsKey(forward.getName())) {
			throw new ForwardDuplicateDefinitionException();
		}
		map.put(forward.getName(), forward);
	}
	
	public ForwardModel find(String name) {
		if(!map.containsKey(name)) {
			throw new ForwardNotFoundException();
		}
		return map.get(name);
	}
	
	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;
	}
	
	@Override
	public String toString() {
		return "ActionModel [path=" + path + ", type=" + type + "]";
	}
}

ForwardModel

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

在此过程中还需要定义必要报错

ActionDuplicateDefinitionException

public class ActionDuplicateDefinitionException extends RuntimeException{
	
	public ActionDuplicateDefinitionException() {
		super();
	}
	
	public ActionDuplicateDefinitionException(String msg) {
		super(msg);
	}
	
	public ActionDuplicateDefinitionException(String msg, Throwable c) {
		super(msg, c);
	}
}

ActionNotFoundException

public class ActionNotFoundException extends RuntimeException {
	
	public ActionNotFoundException() {
		super();
	}
	
	public ActionNotFoundException(String msg) {
		super(msg);
	}
	
	public ActionNotFoundException(String msg, Throwable c) {
		super(msg, c);
	}

}

ForwardDuplicateDefinitionException

public class ForwardDuplicateDefinitionException extends RuntimeException {
	
	public ForwardDuplicateDefinitionException() {
		super();
	}
	
	public ForwardDuplicateDefinitionException(String msg) {
		super(msg);
	}
	
	public ForwardDuplicateDefinitionException(String msg, Throwable c) {
		super(msg, c);
	}

}

ForwardNotFoundException

public class ForwardNotFoundException extends RuntimeException {
	
	public ForwardNotFoundException() {
		super();
	}
	
	public ForwardNotFoundException(String msg) {
		super(msg);
	}
	
	public ForwardNotFoundException(String msg, Throwable c) {
		super(msg, c);
	}

}

ConfigModelFactory

public final class ConfigModelFactory {
	
	/**
	 * 单例模式
	 */
	private ConfigModelFactory(){}
	
	private static ConfigModel config = new ConfigModel();
	
	/**
	 * 使用单例模式获得configmodel
	 * @return ConfigModel
	 */
	public static ConfigModel getConfig() {
		return config;
	}
	
	//static代码块,只会加载一次
	static {
		try {
			InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(in);
			Element root = doc.getRootElement();
			List<Element> actions = root.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();
		}
	}
	
	public static void main(String[] args) {
		ActionModel action = ConfigModelFactory.getConfig().find("/studentAction");
		System.out.println(action.find("students"));
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值