当前位置: 首页 > news >正文

J2EEXML建模

目录

用一个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文件

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE config[<!ELEMENT config (action*)><!ELEMENT action (forward*)><!ELEMENT forward EMPTY><!ATTLIST actionpath CDATA #REQUIREDtype CDATA #REQUIRED><!ATTLIST forwardname CDATA #REQUIREDpath CDATA #REQUIREDredirect (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

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<>(); //转发模型的HashMapprivate 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<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     

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

/** 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);}

http://www.lryc.cn/news/95121.html

相关文章:

  • vue中export和export default
  • 转职做项目经理,我为什么选择PMP?
  • LangChain(5)Conversational Agents
  • 【云原生】Kubernetes临时容器
  • Jenkins+Robot 接口自动化测试
  • 【Visual Studio Code】---自定义键盘快捷键设置
  • FastEdit ⚡:在10秒内编辑大型语言模型
  • SpringBoot + Docker 实现一次构建到处运行
  • Spring-Cloud-Gateway如何自定义断言工厂?
  • Android平台如何高效率实现GB28181对接?
  • vue2 实现后台管理系统左侧菜单联动实现 tab根据路由切换联动内容,并支持移动端框架
  • 一本通1910:【00NOIP普及组】计算器的改良题解
  • golang网络编程学习-1rpc
  • 【MQTT】Esp32数据上传采集:最新mqtt插件(支持掉线、真机调试错误等问题)
  • 基于PyQt5的UI界面开发——对基本控件的介绍
  • flink 报错:Caused by: java.lang.RuntimeException: Assigned key must not be null!
  • AN OVERVIEW OF LANGUAGE MODELS RECENT DEVELOPMENTS AND OUTLOOK
  • ArcGIS、ENVI、InVEST、FRAGSTATS等多技术融合提升
  • fastapi初使用,构建自己的api
  • Html基础知识学习——圣杯布局、margin负值、等高布局(十七)
  • 从一长串字符串中找出图片,查看是否符合md5要求
  • 新手小白如何学好UI设计?一般学多久? 优漫动游
  • 实现 Rollup 插件alias 并使用vitest提高开发效率
  • 【DSL】ES+DSL 查询语法
  • Vue第三篇:最简单的vue购物车示例
  • MFC 基于数据库的管理系统
  • EfficientNet论文笔记
  • 系统学习Linux-SSH远程服务(二)
  • PyTorch训练RNN, GRU, LSTM:手写数字识别
  • 基于深度学习的高精度道路瑕疵检测系统(PyTorch+Pyside6+YOLOv5模型)