• J2EE从入门到入土07.XML建模


    回顾:J2EE从入门到入土06.XML配置文件的读取

    目录

    ConfigModel

    ActionModel

    ForwardModel

    ActionDuplicateDefinitionException

    ActionNotFoundException

    ForwardDuplicateDefinitionException

    ForwardNotFoundException

    ConfigModelFactory


    创建根目录resource,把xml文件放到根目录下

     

    导入需要用到的jar包

     

    开始创建与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>

    ConfigModel

    1. public class ConfigModel {
    2. private Map<String,ActionModel> map = new HashMap<String, ActionModel>();
    3. public void put(ActionModel action) {
    4. if(map.containsKey(action.getPath())) {
    5. throw new ActionDuplicateDefinitionException();
    6. }
    7. map.put(action.getPath(), action);
    8. }
    9. public ActionModel find(String path) {
    10. if(!map.containsKey(path)) {
    11. throw new ActionNotFoundException();
    12. }
    13. return map.get(path);
    14. }
    15. }

    ActionModel

    1. public class ActionModel {
    2. private String path;
    3. private String type;
    4. private Map<String,ForwardModel> map = new HashMap<String, ForwardModel>();
    5. public void put(ForwardModel forward) {
    6. if(map.containsKey(forward.getName())) {
    7. throw new ForwardDuplicateDefinitionException();
    8. }
    9. map.put(forward.getName(), forward);
    10. }
    11. public ForwardModel find(String name) {
    12. if(!map.containsKey(name)) {
    13. throw new ForwardNotFoundException();
    14. }
    15. return map.get(name);
    16. }
    17. public String getPath() {
    18. return path;
    19. }
    20. public void setPath(String path) {
    21. this.path = path;
    22. }
    23. public String getType() {
    24. return type;
    25. }
    26. public void setType(String type) {
    27. this.type = type;
    28. }
    29. @Override
    30. public String toString() {
    31. return "ActionModel [path=" + path + ", type=" + type + "]";
    32. }
    33. }

    ForwardModel

    1. public class ForwardModel {
    2. private String name;
    3. private String path;
    4. private boolean redirect;
    5. public String getName() {
    6. return name;
    7. }
    8. public void setName(String name) {
    9. this.name = name;
    10. }
    11. public String getPath() {
    12. return path;
    13. }
    14. public void setPath(String path) {
    15. this.path = path;
    16. }
    17. public boolean isRedirect() {
    18. return redirect;
    19. }
    20. public void setRedirect(boolean redirect) {
    21. this.redirect = redirect;
    22. }
    23. public void setRedirect(String redirect) {
    24. this.redirect = Boolean.valueOf(redirect);
    25. }
    26. @Override
    27. public String toString() {
    28. return "ForwardModel [name=" + name + ", path=" + path + ", redirect=" + redirect + "]";
    29. }
    30. }

    在此过程中还需要定义必要报错

    ActionDuplicateDefinitionException

    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

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

    ConfigModelFactory

    1. public final class ConfigModelFactory {
    2. /**
    3. * 单例模式
    4. */
    5. private ConfigModelFactory(){}
    6. private static ConfigModel config = new ConfigModel();
    7. /**
    8. * 使用单例模式获得configmodel
    9. * @return ConfigModel
    10. */
    11. public static ConfigModel getConfig() {
    12. return config;
    13. }
    14. //static代码块,只会加载一次
    15. static {
    16. try {
    17. InputStream in = XmlReader.class.getResourceAsStream("/config.xml");
    18. SAXReader reader = new SAXReader();
    19. Document doc = reader.read(in);
    20. Element root = doc.getRootElement();
    21. List<Element> actions = root.selectNodes("action");
    22. for(Element e:actions) {
    23. String path = e.attributeValue("path");
    24. String type = e.attributeValue("type");
    25. ActionModel action = new ActionModel();
    26. action.setPath(path);
    27. action.setType(type);
    28. List<Element> forwards = e.selectNodes("forward");
    29. for(Element f : forwards) {
    30. String name = f.attributeValue("name");
    31. String fpath = f.attributeValue("path");
    32. String redirect = f.attributeValue("redirect");
    33. ForwardModel forward = new ForwardModel();
    34. forward.setName(name);
    35. forward.setPath(fpath);
    36. forward.setRedirect(redirect);
    37. action.put(forward);
    38. }
    39. config.put(action);
    40. }
    41. } catch (Exception e) {
    42. e.printStackTrace();
    43. }
    44. }
    45. public static void main(String[] args) {
    46. ActionModel action = ConfigModelFactory.getConfig().find("/studentAction");
    47. System.out.println(action.find("students"));
    48. }
    49. }
  • 相关阅读:
    【面向对象】理解面向对象编程中的封装性
    哈希及其应用
    阿里云人脸识别对比
    Oracle/PLSQL: To_Timestamp_Tz Function
    [Lua] IT技术熟练度生成器 | 根据活动记录生成md表格 | 自创
    【密码学基础】Oblivious Transfer(不经意传输)
    国开现代汉语专题,形考答案形考任务
    PLL锁相环设计中的VCXO性能权衡
    携职教育:软考高项彻底没用了?软考高项证书还值不值得考?
    济南ITSS证书办理大全
  • 原文地址:https://blog.csdn.net/github_67177094/article/details/125612217