struts2中的include的實現

來源:互聯網
上載者:User

通過讀取原始碼,發現struts2是通過Xwork中的XmlConfigurationProvider來讀取配置的.其中發現在xwork2.1版本中,是支援*號的通配讀取.但在xwork2.0中是不支援這樣的寫法. 
所以在String2.1的版本中是可以使用<include file="resource/struts-config/*.xml"/>這樣的配置.在Struts2.0中是不支援這樣的配置 
xwork2.1 

Java代碼  
  1. if ("include".equals(nodeName)) {  
  2.                             String includeFileName = child.getAttribute("file");  
  3.                             if (includeFileName.indexOf('*') != -1) {  
  4.                                 // handleWildCardIncludes(includeFileName, docs, child);  
  5.                                 ClassPathFinder wildcardFinder = new ClassPathFinder();  
  6.                                 wildcardFinder.setPattern(includeFileName);  
  7.                                 Vector<String> wildcardMatches = wildcardFinder.findMatches();  
  8.                                 for (String match : wildcardMatches) {  
  9.                                     finalDocs.addAll(loadConfigurationFiles(match, child));  
  10.                                 }  
  11.                             } else {  
  12.                                 finalDocs.addAll(loadConfigurationFiles(includeFileName, child));  
  13.                             }  
  14.                         }  


xwork2.0 

Java代碼  
  1. Element rootElement = doc.getDocumentElement();  
  2.                 NodeList children = rootElement.getChildNodes();  
  3.                 int childSize = children.getLength();  
  4.   
  5.                 for (int i = 0; i < childSize; i++) {  
  6.                     Node childNode = children.item(i);  
  7.   
  8.                     if (childNode instanceof Element) {  
  9.                         Element child = (Element) childNode;  
  10.   
  11.                         final String nodeName = child.getNodeName();  
  12.   
  13.                         if (nodeName.equals("include")) {  
  14.                             String includeFileName = child.getAttribute("file");  
  15.                             docs.addAll(loadConfigurationFiles(includeFileName, child));  
  16.                         }  
  17.                     }  
  18.                 }  
  19.                 docs.add(doc);  
  20.                 loadedFileUrls.add(url.toString());  

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.