• 手把手教会:XML建模


    上期回顾:XML文件的解析操作_小阿飞_的博客-CSDN博客

    XML是比较常用的配置文件,但上期文章中解析XML文件的代码不可能每次在需要用到的时又重新编写一遍,本期所讲的建模(OOP思想)就是一个很好的解决方式(将解析XML文件的代码变成对象型数据)

    本期精彩😜(从第一步到最后一步手把手教会)👇

    目录

    XML建模步骤

    1、环境的搭建

    2、导入相关jar包

    3、 建立相关的包及MVC的配置文件

    4、 通过config.xml格式建立相关的模型类

    5、自定义异常

    5、观察MVC的配置文件config.xml来编写代码

    6、模型类代码编写

    7、工厂类ConfigModelFactory编写


    XML建模步骤

    1、环境的搭建

    (1) 选择一个工作空间后点击Launch

    (2.1) 点击首选项

    (2.2) 进入工作空间将编码格式设置为UTF-8

    (2.3) 将JSP的编码格式也设置为UTF-8

     (3.1) 新建动态项目(项目名称可全小写)

    (3.2) 配置tomcat环境

    选择你电脑上tomcat服务器的文件路径 

     (3.3) 配置JDK

    选择你电脑上JDK的文件路径,一般放于C盘👇 

     

     

     

     最后你可以在这里面将字体设置为自己想要的大小和格式

      

    (4) 生成XML配置文件

    2、导入相关jar包

    • 要导入的jar包

    • 在项目中build path后

    3、 建立相关的包及MVC的配置文件

    包名规范:com/org.公司名.项目名.模块名

    1. com.xyf.mvc.framework:框架包
    2. resources源码包下的文件:放自定义MVC的配置文件config.xml
    • config.xml代码展示👇(编写后续包、类、代码的依据!)
    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE config>
    3. <config>
    4. <action type="org.lisen.mvc.action.StudentAction" path="/studentAction">
    5. <forward path="/students/studentList.jsp" redirect="false" name="students"/>
    6. </action>
    7. </config>

    4、 通过config.xml格式建立相关的模型类

    类名规范:首字母大写的驼峰命名法

    模型类命名:

    • ConfigModel:Config节点模型
    • ActionModel:Action节点模型
    • ForwardModel:Farword节点模型

    5、自定义异常

    异常类命名+代码编写(关键是异常名称)

    • ActionDuplicateDefinitionException:Action元素中存在重复定义异常
    1. public class ActionDuplicateDefinitionException extends RuntimeException {
    2. public ActionDuplicateDefinitionException() {
    3. super();
    4. }
    5. public ActionDuplicateDefinitionException(String msg) {
    6. super(msg);
    7. }
    8. public ActionDuplicateDefinitionException(String msg, Throwable c) {
    9. super(msg, c);
    10. }
    11. }
    • ActionNotFoundException:
    1. public class ActionNotFoundException extends RuntimeException {
    2. public ActionNotFoundException() {
    3. super();
    4. }
    5. public ActionNotFoundException(String msg) {
    6. super(msg);
    7. }
    8. public ActionNotFoundException(String msg, Throwable c) {
    9. super(msg, c);
    10. }
    11. }
    •  ForwardDuplicateDefinitionException:Forward元素中存在重复定义异常
    1. public class ForwardDuplicateDefinitionException extends RuntimeException {
    2. public ForwardDuplicateDefinitionException() {
    3. super();
    4. }
    5. public ForwardDuplicateDefinitionException(String msg) {
    6. super(msg);
    7. }
    8. public ForwardDuplicateDefinitionException(String msg, Throwable c) {
    9. super(msg, c);
    10. }
    11. }
    •  ForwardNotFoundException:
    1. public class ForwardNotFoundException extends RuntimeException {
    2. public ForwardNotFoundException() {
    3. super();
    4. }
    5. public ForwardNotFoundException(String msg) {
    6. super(msg);
    7. }
    8. public ForwardNotFoundException(String msg, Throwable c) {
    9. super(msg, c);
    10. }
    11. }

    5、观察MVC的配置文件config.xml来编写代码

    观察config.xml可知👇

    • 一个config节点下可以有多个action节点
    • action节点中具有type和path两个属性
    • action节点中有forward节点
    • forward节点具有三个属性......

    6、模型类代码编写

    根节点ConfigModel类

    1. public class ConfigModel {
    2. // 一个config节点下可以有多个action节点
    3. // 放key==path和value==ActionModel类/对象:通过根节点找到action
    4. // 初始化:new HashMap<>()
    5. private Map<String, ActionModel> actionMap=new HashMap<>();
    6. /**
    7. * 将path放入key,保持path不重复
    8. * 遍历ActionModel对象,通过ActionModel对象获取path属性
    9. * @param 传入ActionModel这个类/对象
    10. */
    11. public void put(ActionModel action) {
    12. if(actionMap.containsKey(action.getPath())) {
    13. throw new ActionDuplicateDefinitionException("Action path= "+action.getPath()+" Duplicate definition");
    14. }
    15. actionMap.put(action.getPath(), action);
    16. }
    17. /**
    18. * 找path
    19. * @param 传入path
    20. * @return 找不到path则抛出异常,找到就返回path
    21. */
    22. public ActionModel find(String path) {
    23. if(!actionMap.containsKey(path)) {
    24. throw new ActionNotFoundException("Action path ="+path+" not found");
    25. }
    26. return actionMap.get(path);
    27. }
    28. }

    ActionModel类

    1. public class ActionModel {
    2. // 有两个节点
    3. private String type;
    4. private String path;
    5. // 包含元素forward:通过action找到forward
    6. private Map<String, ForwardModel> forwardMap =new HashMap<>();
    7. // 将name放入key,保持name不重复
    8. public void put(ForwardModel forward) {
    9. if(forwardMap.containsKey(forward.getName())) {
    10. throw new ForwardDuplicateDefinitionException("forward name = "+forward.getName()+" DuplicateDefinition");
    11. }
    12. forwardMap.put(forward.getName(), forward);
    13. }
    14. // 找name
    15. public ForwardModel find(String name) {
    16. if(!forwardMap.containsKey(name)) {
    17. throw new ForwardNotFoundException("forward name ="+name+" not found");
    18. }
    19. return forwardMap.get(name);
    20. }
    21. public String getType() {
    22. return type;
    23. }
    24. public void setType(String type) {
    25. this.type = type;
    26. }
    27. public String getPath() {
    28. return path;
    29. }
    30. public void setPath(String path) {
    31. this.path = path;
    32. }
    33. @Override
    34. public String toString() {
    35. return "ActionModel [type=" + type + ", path=" + path + ", forwardMap=" + forwardMap + "]";
    36. }
    37. }

    ForwardModel类

    1. public class ForwardModel extends RuntimeException{
    2. //三个属性
    3. private String name;
    4. private String path;
    5. private boolean redirect;
    6. public String getName() {
    7. return name;
    8. }
    9. public void setName(String name) {
    10. this.name = name;
    11. }
    12. public String getPath() {
    13. return path;
    14. }
    15. public void setPath(String path) {
    16. this.path = path;
    17. }
    18. public boolean isRedirect() {
    19. return redirect;
    20. }
    21. public void setRedirect(boolean redirect) {
    22. this.redirect = redirect;
    23. }
    24. //将redirect中的字符串转成boolean类型的方法
    25. public void setRedirect(String redirect) {
    26. //this=当前类
    27. this.redirect =Boolean.valueOf(redirect);
    28. }
    29. @Override
    30. public String toString() {
    31. return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
    32. }
    33. }

    7、工厂类ConfigModelFactory编写

    编写工厂类ConfigModelFactory的目的是在这个类中使用单例模式,并且只对XML文件中的内容读取一次,读取后放入上一步编写好了的模型对象中

    1. /**
    2. * 单例工厂类
    3. * 让解析XML的代码只执行一次
    4. */
    5. @SuppressWarnings("unchecked")
    6. public final class ConfigModelFactory {
    7. //单例模式
    8. private ConfigModelFactory() {}
    9. //根节点模型
    10. private static ConfigModel config = new ConfigModel();
    11. //只读取一次config中的数据,并填充到模型中
    12. static {
    13. try {
    14. //读
    15. InputStream is=XmlReader.class.getResourceAsStream("/config.xml");
    16. SAXReader reader=new SAXReader();
    17. Document doc = reader.read(is);
    18. //通过根元素获得根元素下的节点action(xml中的数结构!)
    19. Element rootElement = doc.getRootElement();
    20. List<Element> actions = rootElement.selectNodes("action");
    21. for (Element e : actions) {
    22. String path = e.attributeValue("path");
    23. String type = e.attributeValue("type");
    24. //放入模型
    25. ActionModel action=new ActionModel();
    26. action.setPath(path);
    27. action.setType(type);
    28. //通过action元素获得actions下面的节点forward
    29. List<Element> forward1 = e.selectNodes("forward");
    30. for (Element f : forward1) {
    31. String name = f.attributeValue("name");
    32. String fPath = f.attributeValue("path");
    33. String redirect = f.attributeValue("redirect");
    34. //放入模型
    35. ForwardModel forward=new ForwardModel();
    36. forward.setName(name);
    37. forward.setPath(fPath);
    38. forward.setRedirect(redirect);
    39. //将forward放入action
    40. action.put(forward);
    41. }
    42. //循环完成后将action放入根节点
    43. config.put(action);
    44. }
    45. } catch (DocumentException e) {
    46. e.printStackTrace();
    47. }
    48. }
    49. public static ConfigModel getConfig() {
    50. return config;
    51. }
    52. //测试
    53. public static void main(String[] args) {
    54. ConfigModel config = ConfigModelFactory.getConfig();
    55. //通过action中的path(xml中有/)找action
    56. ActionModel action = config.find("/studentAction");
    57. System.out.println(action);
    58. //通过forward中的name找forward,xml中无/
    59. ForwardModel forward = action.find("students");
    60. System.out.println(forward);
    61. }
    62. }

    小结:到这里对config.xml文件的建模操作已经完成了,但还存在一些小bug,比如如果在config.xml的path中没有传入“/”,也会成功建模,这显然是不对的,所以敬请期待下期文章(●'◡'●)

  • 相关阅读:
    2022.08.03_每日一题
    Android签名查看
    R语言使用plot函数可视化数据散点图,使用log函数对X轴数据和Y轴数据进行对数变换后再进行可视化(log(x),log(y))
    select in ()的时候,MySQL会自动按主键自增排序,要是按IN中给定的顺序来取,如何实现呢?
    spring源码理解
    flink 8081 web页面无法被局域网内其他机器访问
    大语言模型底层架构丨带你认识Transformer
    Microsoft Excel 101 简介
    Javascript知识【案例:表格隔行换色】
    流式响应Web小工具实践
  • 原文地址:https://blog.csdn.net/yifei_345678/article/details/125607662