数据(Action配置文件)有了,对应的数据存储类(ActionMapping)也有了。毫无疑问,接下来就是数据的解析了。使用dom4j解析xml文件。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
   | package com.snails.framework.action;
  import java.io.InputStream; import java.util.HashMap; import java.util.Iterator; import java.util.Map;
  import org.apache.commons.lang3.StringUtils; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader;
 
 
 
 
  public class ActionMappingManager {               Map<String, ActionMapping> actionMappings = new HashMap<String, ActionMapping>();          public ActionMappingManager() {    }          public ActionMappingManager(String[] configFiles) throws Exception {                  if (null == configFiles || configFiles.length <= 0) {             throw new Exception("请指定action的配置文件!");         }                  for (String cfgFile : configFiles) {             readCfg(cfgFile);         }     }          
 
 
 
 
      public ActionMapping getActionMapping(String actionName) throws Exception {         if (StringUtils.isBlank(actionName)) {             throw new Exception("入参actionName不允许为空!");         }         ActionMapping am = this.actionMappings.get(actionName);         if (null == am) {             throw new Exception("没有Action["+actionName+"]的配置!");         }                  return am;     }          
 
 
      @SuppressWarnings("rawtypes")     private void readCfg(String cfgFile) {
          try {             
              InputStream is = Thread.currentThread().getContextClassLoader().getResourceAsStream(cfgFile);                           SAXReader reader = new SAXReader();                            Document doc = reader.read(is);                             Element actions = doc.getRootElement();                          Iterator it = actions.elementIterator();             while (it.hasNext()) {                                  ActionMapping am = new ActionMapping();                 Element action = (Element) it.next();                                  String actionName = action.attributeValue("name");                 String actionClassName = action.attributeValue("class");                                  am.setActionName(actionName);                 am.setActionClassName(actionClassName);                                                   Iterator results = action.elementIterator();                 while (results.hasNext()) {                     Element result = (Element) results.next();                                          String resultName = result.attributeValue("name");                     String redirect = result.attributeValue("redirect");                     String resultContent = result.getTextTrim();                     if (StringUtils.isBlank(resultContent))                         resultContent = "input";                                                               Map<String, String> rs = new HashMap<String, String>();                     rs.put("RESULT_CONTENT", resultContent);                     rs.put("REDIRECT", redirect);                     am.setResultMap(resultName, rs);                 }                                  actionMappings.put(am.getActionName(), am);             }
          } catch (DocumentException e) {             e.printStackTrace();         }              }          public static void main(String[] args) {                      String cfgFile = "E:\\html2pdf_codes\\snails\\src\\com\\snails\\framework\\config\\snails-actions.xml";         new ActionMappingManager().readCfg(cfgFile);     } }
   | 
 
方法中的参数异常直接抛到上一层处理。代码也没什么难度,注意解析过程获取节点数据的关键字与xml文件对应即可。
如果你对xml文件的解析不是很明白请参考xml解析教程。其他不多说,请继续看后面的文章。
项目完整代码请看MyMVC,欢迎fork学习,如果你觉得对你有帮助给我点个赞吧,当然也欢迎给我提意见(email:1527254027@qq.com,chendequanroob@gmail.com)。