J2EE&XML建模

目录

用一个xml-config文件实例:

先看config.xml文件

[再看 ActionModel](#再看 ActionModel)

ConfigModel

ActionNotFoundException

ForwardNotFoundException

ConfigModelFactory

ActionDuplicateDefinitionException

ForwardDuplicateDefinitionException

InvalidPathException


用一个xml-config文件实例:

  • ActionModel
  • ConfigModel
  • ForwardModel
  • ActionNotFoundException
  • ForwardNotFoundException
  • ConfigModelFactory
  • ActionDuplicateDefinitionException
  • ForwardDuplicateDefinitionException
  • InvalidPathException

先看config.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>

再看 ActionModel

java 复制代码
package com.zking.mymvc.framework;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * ActionModel类:表示一个Action的模型,包含了Action的路径、类型、转发模型、重定向等属性
 * */
public class ActionModel {
	
	private String path; //Action的路径
	private String type; //Action的类型,例如:request、ajax等
	
	private static Pattern pattern = Pattern.compile("^/.+$"); //静态的正则表达式,用于匹配Action的路径
	
	private Map<String, ForwardModel> forwardmap = new HashMap<>(); //转发模型的HashMap
	private Boolean redirect; //是否重定向
	
	
	public String getPath() {
		return path;
	}
	
	public void setPath(String path) {
		checkPath(path); //校验Action的路径是否符合规范,即必须以/开头
		this.path = path;
	}
	
	public String getType() {
		return type;
	}
	
	public void setType(String type) {
		this.type = type;
	}
	
	
	// put()方法用于将ForwardModel转发模型添加到HashMap集合中;
	public void put(ForwardModel forward) {
		if(!forwardmap.containsKey(forward.getName())) {
			forwardmap.put(forward.getName(), forward); //添加转发模型
		}
		else {
			throw new ForwardDuplicateDefinitionException("forward name:"+forward.getName()+" 不能重复");
			//如果转发模型已经存在,则抛出ForwardDuplicateDefinitionException异常
		}
	}
	
	// find()方法用于查找指定名称的转发模型,如果不存在则抛出ForwardNotFoundException异常
	public  ForwardModel find(String name) {
		if(!forwardmap.containsKey(name)) {
			return forwardmap.get(name);
		}
		else {
			throw new ForwardNotFoundException("forward name:"+name+"不存在");
			//如果转发模型不存在,则抛出ForwardNotFoundException异常
		}
	}
	
	// setRedirect()方法用于设置属性redirect的值必须为true或者false;
	public void setRedirect(String redirect) {
		if("true".equals(redirect) || "false".equals(redirect)){
			this.redirect=Boolean.valueOf(redirect);
		}
		else {
			throw new RuntimeException("属性redirect的值必须为true或者false");
			//如果属性redirect的值不为true或者false,则抛出RuntimeException异常
		}
	}
	
	// checkPath()方法用于校验路径是否符合规范,即必须以/开头;
	public void checkPath(String path) {
		Matcher matcher = pattern.matcher(path); //匹配Action的路径是否符合规范
		boolean b = matcher.matches();
		if(!b) {
			throw new InvalidPathException("ForwardModel.path["+path+"]必须以/开头");
			//如果Action的路径不符合规范,则抛出InvalidPathException异常
		}
	}
	
	
}

ConfigModel

java 复制代码
public class ConfigModel {

    private Map<String, ActionModel> actionMap = new HashMap<>();
	//根据指定的路径 path,在 actionMap 中查找对应的 ActionModel 对象并返回。
	public ActionModel find(String path) {
		if(actionMap.containsKey(path)) {
			return actionMap.get(path);
		}
		else {
			throw new RuntimeException("action path:"+path+"没有找到");
		}
	}
	
    //将指定的 ActionModel 对象存储到 actionMap 中。
	public void put(ActionModel action) {
		
		if(!actionMap.containsKey(action.getPath())) {
			actionMap.put(action.getPath(), action);
		}
		else {
            //如果该对象的路径已经存在于 actionMap 中,则抛出自定义的 ActionDuplicateDefinitionException 异常,提示路径重复定义。 
			throw new ActionDuplicateDefinitionException("action path:"+action.getPath()+"重复定义");
		}
		
	}

    
}

ForwardModel

java 复制代码
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(String redirect) {
		this.redirect = Boolean.valueOf(redirect);
	}
    
}

ActionNotFoundException

java 复制代码
/*
 * action找不到指定路径
 */
public class ActionNotFoundException extends RuntimeException{

    public ActionNotFoundException() {
		super();
	}
	
	public ActionNotFoundException(String msg) {
		super(msg);
	}
	
	public ActionNotFoundException(String msg,Throwable cause) {
		super(msg,cause);
	}
    
}

ForwardNotFoundException

java 复制代码
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(String redirect) {
		this.redirect = Boolean.valueOf(redirect);
	}
    
}

ConfigModelFactory

java 复制代码
public class ConfigModelFactory {
    //私有化构造方法,确保该类不会被实例化
    private ConfigModelFactory() {}

    //使用饿汉模式,类加载时就初始化了config对象
    private static ConfigModel config = null;
    static {
        //读取配置文件config.xml
        InputStream in = ConfigModelFactory.class.getResourceAsStream("/config.xml");
        SAXReader reader  = new SAXReader();
        Document doc;
        try {
            doc = reader.read(in);
            Element root = doc.getRootElement();
            config = new ConfigModel();
            
            //读取每个action节点
            List<Element> actions = root.selectNodes("action");
            for (Element action : actions) {
                String actionPath = action.attributeValue("path");
                String actionType = action.attributeValue("type");
                //创建ActionModel对象
                ActionModel actionModel = new ActionModel();
                actionModel.setPath(actionPath);
                actionModel.setType(actionType);

                //读取每个action节点下的forward子节点
                List<Element> forwards = action.selectNodes("forward");
                for (Element forward : forwards) {
                    String forwardPath = forward.attributeValue("path");
                    String forwardName = forward.attributeValue("name");
                    String redirect = forward.attributeValue("redirect");
                    //创建ForwardModel对象
                    ForwardModel forwardModel = new ForwardModel();
                    forwardModel.setPath(forwardPath);
                    forwardModel.setName(forwardName);
                    forwardModel.setRedirect(redirect);
                    //将ForwardModel对象放入ActionModel对象中
                    actionModel.put(forwardModel);
                }
                //将ActionModel对象放入ConfigModel对象中
                config.put(actionModel);
            }
        } catch (Exception e) {
            //抛出运行时异常
            throw new RuntimeException("解析config.xml发生异常", e.getCause());
        }
    }

    //提供一个方法获取config对象
    public static ConfigModel getConfigModel() {
        return config;
    }

    public static void main(String[] args) throws DocumentException {
        //测试
        ActionModel action = config.find("/studentAction");
        System.out.println(action.getType());
        System.out.println("yes");
    }
}

ActionDuplicateDefinitionException

java 复制代码
/*
 * action重复定义异常
 */
public class ActionDuplicateDefinitionException extends RuntimeException{
    
    public ActionDuplicateDefinitionException() {
		super();
	}
	
	public ActionDuplicateDefinitionException(String msg) {
		super(msg);
	}
	
	public ActionDuplicateDefinitionException(String msg,Throwable cause) {
		super(msg,cause);
	}
}

ForwardDuplicateDefinitionException

java 复制代码
/**
 * forward重复定义异常
 * @author PC
 *
 */
public class ForwardDuplicateDefinitionException extends RuntimeException{

	public ForwardDuplicateDefinitionException() {
		super();
	}
	
	public ForwardDuplicateDefinitionException(String msg) {
		super(msg);
	}
	
	public ForwardDuplicateDefinitionException(String msg,Throwable cause) {
		super(msg,cause);
	}

InvalidPathException

java 复制代码
public class InvalidPathException extends RuntimeException{

	public InvalidPathException() {
		super();
	}
	
	public InvalidPathException(String msg) {
		super(msg);
	}
	
	public InvalidPathException(String msg,Throwable cause) {
		super(msg,cause);
	}
相关推荐
seventeennnnn1 分钟前
Java大厂面试真题:谢飞机的技术挑战
java·spring boot·面试·aigc·技术挑战·电商场景·内容社区
wkj00112 分钟前
接口实现类向上转型和向上转型解析
java·开发语言·c#
qqxhb13 分钟前
零基础设计模式——行为型模式 - 观察者模式
java·观察者模式·设计模式·go
寒士obj40 分钟前
类加载的过程
java·开发语言
无名之逆43 分钟前
大三自学笔记:探索Hyperlane框架的心路历程
java·开发语言·前端·spring boot·后端·rust·编程
Chuck1sn1 小时前
我把 Cursor AI 整合到 Ruoyi 中,从此让 Java 脚手架脱离人工!
java·vue.js·后端
水木石画室1 小时前
Spring Boot 常用注解面试题深度解析
java·spring boot·后端
hweiyu001 小时前
tomcat指定使用的jdk版本
java·开发语言·tomcat
百锦再1 小时前
.NET 类库开发详细指南c
java·log4j·.net·net·dot
黎䪽圓2 小时前
【Java多线程从青铜到王者】阻塞队列(十)
java·开发语言