目录
ActionDuplicateDefinitionException
创建根目录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>
- 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);
- }
- }
- 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 + "]";
- }
- }
- 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 + "]";
- }
-
- }
在此过程中还需要定义必要报错
- public class ActionDuplicateDefinitionException extends RuntimeException{
-
- public ActionDuplicateDefinitionException() {
- super();
- }
-
- public ActionDuplicateDefinitionException(String msg) {
- super(msg);
- }
-
- public ActionDuplicateDefinitionException(String msg, Throwable c) {
- super(msg, c);
- }
- }
- public class ActionNotFoundException extends RuntimeException {
-
- public ActionNotFoundException() {
- super();
- }
-
- public ActionNotFoundException(String msg) {
- super(msg);
- }
-
- public ActionNotFoundException(String msg, Throwable c) {
- super(msg, c);
- }
-
- }
- public class ForwardDuplicateDefinitionException extends RuntimeException {
-
- public ForwardDuplicateDefinitionException() {
- super();
- }
-
- public ForwardDuplicateDefinitionException(String msg) {
- super(msg);
- }
-
- public ForwardDuplicateDefinitionException(String msg, Throwable c) {
- super(msg, c);
- }
-
- }
- public class ForwardNotFoundException extends RuntimeException {
-
- public ForwardNotFoundException() {
- super();
- }
-
- public ForwardNotFoundException(String msg) {
- super(msg);
- }
-
- public ForwardNotFoundException(String msg, Throwable c) {
- super(msg, c);
- }
-
- }
- 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"));
- }
- }