J2EE&XML建模

目录

用一个xml-config文件实例:

先看config.xml文件

再看 ActionModel

ConfigModel

ActionNotFoundException

ForwardNotFoundException

ConfigModelFactory

ActionDuplicateDefinitionException

ForwardDuplicateDefinitionException

InvalidPathException


用一个xml-config文件实例:

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

先看config.xml文件



	
	
	
	
]>

	
		
	

再看 ActionModel

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 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

public class ConfigModel {

    private Map 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     

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

/*
 * 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

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

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 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 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

/*
 * 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

/**
 * 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


public class InvalidPathException extends RuntimeException{

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

你可能感兴趣的:(java-ee,xml,java,eclipse,map)