• XML建模


    目录

    一、xml建模核心思想

    二、xml建模的作用

    三、建模案例

    ConfigModel类

    ActionModel类

    自定义异常

    ForwardModel类

    自定义异常

    ConfigFactory类

    测试


    一、xml建模核心思想

    xml建模的核心思想就是利用java面向对象的特性,用操作对象的方式操作xml。

    二、xml建模的作用

    1、节约资源

    2、优化性能

    3、更加便捷操作xml文件

    三、建模案例

    以下方config.xml为例

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <!DOCTYPE config[
    3. <!ELEMENT config (action*)>
    4. <!ELEMENT action (forward*)>
    5. <!ELEMENT forward EMPTY>
    6. <!ATTLIST action
    7. path CDATA #REQUIRED
    8. type CDATA #REQUIRED
    9. >
    10. <!ATTLIST forward
    11. name CDATA #REQUIRED
    12. path CDATA #REQUIRED
    13. redirect (true|false) "false"
    14. >
    15. ]>
    16. <config>
    17. <action path="/studentAction" type="org.lisen.mvc.action.StudentAction">
    18. <forward name="students" path="/students/studentList.jsp" redirect="false"/>
    19. </action>
    20. </config>
    • 一个标签就是一个对象,在上方xml文件中有三个标签config、action、forward。所以我们需要三个实体类来进行建模

    ConfigModel类

    在集合中增加ActionModel对象,通过path找到对应的ActionModel对象

    1. package com.zking.mymvc.framework;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. public class ConfigModel {
    5. private Map<String, ActionModel> actionMap=new HashMap<String, ActionModel>();
    6. /**
    7. * 将ActionModel对象放到map集合
    8. * @param forward
    9. */
    10. public void put(ActionModel action) {
    11. if(actionMap.containsKey(action.getPath())) {
    12. throw new ActionDuplicateDefinitionException("Action path ="+ action.getPath()+" duplicate definition");
    13. }
    14. actionMap.put(action.getPath(), action);
    15. }
    16. /**
    17. * 通过action中的path从map结合中取出对应的action对象 若path填错则抛出自定义异常未找到
    18. * @param name
    19. * @return
    20. */
    21. public ActionModel find(String path) {
    22. if(!actionMap.containsKey(path)) {
    23. throw new ActionNotFoundException("Action path ="+ path +" not found");
    24. }
    25. return actionMap.get(path);
    26. }
    27. @Override
    28. public String toString() {
    29. return "ConfigModel [actionMap=" + actionMap + "]";
    30. }
    31. }

    ActionModel类

    在集合中增加ForwardModel对象,通过path找到对应的ForwardModel对象

    1. package com.zking.mymvc.framework;
    2. import java.util.HashMap;
    3. import java.util.Map;
    4. public class ActionModel {
    5. private String path;
    6. private String type;
    7. private Map<String, ForwardModel> forwardMap=new HashMap<String, ForwardModel>();
    8. public String getPath() {
    9. return path;
    10. }
    11. public void setPath(String path) {
    12. this.path = path;
    13. }
    14. public String getType() {
    15. return type;
    16. }
    17. public void setType(String type) {
    18. this.type = type;
    19. }
    20. public Map<String, ForwardModel> getForwardMap() {
    21. return forwardMap;
    22. }
    23. public void setForwardMap(Map<String, ForwardModel> forwardMap) {
    24. this.forwardMap = forwardMap;
    25. }
    26. /**
    27. * 将forwardModel对象放到map集合
    28. * @param forward
    29. */
    30. public void put(ForwardModel forward) {
    31. if(forwardMap.containsKey(forward.getName())) {
    32. throw new ForwardDuplicateDefinitionException("forward name= "+forward.getName()+"duplicate definition");
    33. }
    34. forwardMap.put(forward.getName(), forward);
    35. }
    36. /**
    37. * 通过forward中的name从map结合中取出对应的forward对象 若name填错则抛出自定义异常未找到
    38. * @param name
    39. * @return
    40. */
    41. public ForwardModel find(String name) {
    42. if(!forwardMap.containsKey(name)) {
    43. throw new ForwardNotFoundException("Forward name ="+name+" not found");
    44. }
    45. return forwardMap.get(name);
    46. }
    47. @Override
    48. public String toString() {
    49. return "ActionModel [path=" + path + ", type=" + type + ", forwardMap=" + forwardMap + "]";
    50. }
    51. }

    自定义异常

    • action重复定义
    1. package com.zking.mymvc.framework;
    2. public class ActionDuplicateDefinitionException extends RuntimeException{
    3. public ActionDuplicateDefinitionException() {
    4. super();
    5. }
    6. /**
    7. * 报错信息
    8. * @param msg
    9. */
    10. public ActionDuplicateDefinitionException(String msg) {
    11. super(msg);
    12. }
    13. /**
    14. * 报错信息、报错原因
    15. * @param msg
    16. * @param c
    17. */
    18. public ActionDuplicateDefinitionException(String msg,Throwable c) {
    19. super(msg,c);
    20. }
    21. }
    •  action的path找不到
    1. package com.zking.mymvc.framework;
    2. public class ActionNotFoundException extends RuntimeException {
    3. public ActionNotFoundException() {
    4. super();
    5. }
    6. /**
    7. * 报错信息
    8. * @param msg
    9. */
    10. public ActionNotFoundException(String msg) {
    11. super(msg);
    12. }
    13. /**
    14. * 报错信息、报错原因
    15. * @param msg
    16. * @param c
    17. */
    18. public ActionNotFoundException(String msg,Throwable c) {
    19. super(msg,c);
    20. }
    21. }

    ForwardModel类

    1. package com.zking.mymvc.framework;
    2. public class ForwardModel {
    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. public void setRedirect(String redirect) {
    25. this.redirect = Boolean.valueOf(redirect);
    26. }
    27. @Override
    28. public String toString() {
    29. return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
    30. }
    31. }

    自定义异常

    • forward重复定义
    1. package com.zking.mymvc.framework;
    2. public class ForwardDuplicateDefinitionException extends RuntimeException{
    3. public ForwardDuplicateDefinitionException() {
    4. super();
    5. }
    6. /**
    7. * 报错信息
    8. * @param msg
    9. *
    10. */
    11. public ForwardDuplicateDefinitionException(String msg) {
    12. super(msg);
    13. }
    14. /**
    15. * 报错信息、报错原因
    16. * @param msg
    17. * @param c
    18. */
    19. public ForwardDuplicateDefinitionException(String msg,Throwable c) {
    20. super(msg,c);
    21. }
    22. }
    • forward中的name找不到
    1. package com.zking.mymvc.framework;
    2. public class ForwardNotFoundException extends RuntimeException {
    3. public ForwardNotFoundException() {
    4. super();
    5. }
    6. /**
    7. * 报错信息
    8. * @param msg
    9. */
    10. public ForwardNotFoundException(String msg) {
    11. super(msg);
    12. }
    13. /**
    14. * 报错信息、报错原因
    15. * @param msg
    16. * @param c
    17. */
    18. public ForwardNotFoundException(String msg,Throwable c) {
    19. super(msg,c);
    20. }
    21. }
    • 当三个都完成后,将他们装到ConfigModel中去,需要建一个ConfigFactory类

    ConfigFactory类

    1. package com.zking.mymvc.framework;
    2. import java.io.InputStream;
    3. import java.util.List;
    4. import org.dom4j.Document;
    5. import org.dom4j.Element;
    6. import org.dom4j.io.SAXReader;
    7. import org.xml.sax.XMLReader;
    8. @SuppressWarnings("unchecked")
    9. public final class ConfigModelFactory {
    10. private ConfigModelFactory() {
    11. }
    12. private static ConfigModel config=new ConfigModel();
    13. //进行读取config.xml中的数据填充到模型中
    14. static {
    15. try {
    16. //1.获取io流
    17. InputStream in = XMLReader.class.getResourceAsStream("/config.xml");
    18. //2.创建xml读取工具类SAXReader
    19. SAXReader sax = new SAXReader();
    20. //3.读取配置文件,获得Document对象
    21. Document doc = sax.read(in);
    22. //获取根元素
    23. Element rootElement = doc.getRootElement();
    24. //找到action节点
    25. List<Element> actions = rootElement.selectNodes("action");
    26. for(Element e: actions) {
    27. //获取属性值
    28. String path = e.attributeValue("path");
    29. String type = e.attributeValue("type");
    30. //实例化ActionModel对象
    31. ActionModel action = new ActionModel();
    32. //将属性值放到ActionModel中
    33. action.setPath(path);
    34. action.setType(type);
    35. //找到forward节点
    36. List<Element> forwards = e.selectNodes("forward");
    37. for(Element f: forwards) {
    38. //获取属性值
    39. String name = f.attributeValue("name");
    40. String fpath = f.attributeValue("path");
    41. String redirect = f.attributeValue("redirect");
    42. //实例化ForwardModel对象
    43. ForwardModel forward = new ForwardModel();
    44. //将属性值放入ForwardModel对象中去
    45. forward.setName(name);
    46. forward.setPath(fpath);
    47. forward.setRedirect(redirect);
    48. //将ForwardModel放到ActionModel集合中去
    49. action.put(forward);
    50. }
    51. //将ActionModel放到集合中去ConfigModel
    52. config.put(action);
    53. }
    54. } catch (Exception e) {
    55. e.printStackTrace();
    56. }
    57. }
    58. public static ConfigModel getConfig() {
    59. return config;
    60. }
    61. }
    • 测试

    1. package com.zking.mymvc.framework;
    2. public class Test {
    3. public static void main(String[] args) {
    4. ConfigModel config=ConfigModelFactory.getConfig();
    5. ActionModel action = config.find("/studentAction");
    6. System.out.println(action);
    7. ForwardModel forward = action.find("students");
    8. System.out.println(forward);
    9. }
    10. }

    测试结果:

    若我们故意将action中的路径写错

    1. package com.zking.mymvc.framework;
    2. public class Test {
    3. public static void main(String[] args) {
    4. ConfigModel config=ConfigModelFactory.getConfig();
    5. ActionModel action = config.find("/studentction");
    6. System.out.println(action);
    7. ForwardModel forward = action.find("students");
    8. System.out.println(forward);
    9. }
    10. }

     效果图如下:就会显示自定义异常该路径找不到

     

  • 相关阅读:
    PHP 二手物品交易网站系统mysql数据库web结构apache计算机软件工程网页wamp
    kubernetes—Service介绍
    六、串口通信
    JVM基础06_StringTable
    【MySQL】学习多表查询和笛卡尔积
    车船边缘网关是如何给车辆船只定位的?
    【Unity】旋转的尽头是使用四元数让物体旋转
    es6(二)——常用es6说明
    谷歌搜索引擎关键词优化,竞价排名怎么做?大舍传媒
    Yolov8魔术师:卷积变体大作战,涨点创新对比实验,提供CVPR2023、ICCV2023等改进方案
  • 原文地址:https://blog.csdn.net/weixin_62270300/article/details/125613799