【问题标题】:Access the value of welcome-file-list in web.xml访问 web.xml 中 welcome-file-list 的值
【发布时间】:2016-11-28 14:07:07
【问题描述】:

有什么方法可以访问web.xml 中的welcome-file-list 元素,而不必重新解析web.xml 本身?

【问题讨论】:

    标签: jsf servlets web.xml welcome-file


    【解决方案1】:

    没有。没有公共的 JSF 或 Servlet API。

    你能做的最好的就是抓住 JAXP+XPath。

    InputStream input = FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream("/WEB-INF/web.xml");
    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(input);
    XPathExpression xpath = XPathFactory.newInstance().newXPath().compile("web-app/welcome-file-list/welcome-file");
    NodeList nodes = (NodeList) xpath.evaluate(document, XPathConstants.NODESET);
    
    for (int i = 0; i < nodes.getLength(); i++) {
        String welcomeFile = nodes.item(i).getFirstChild().getNodeValue().trim();
        // ...
    }
    

    如果你碰巧使用 JSF 实用程序库OmniFaces,你可以使用它的WebXml utility class

    List<String> welcomeFiles = WebXml.INSTANCE.getWelcomeFiles();
    

    另见:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-20
      • 2012-11-07
      • 2017-08-19
      • 2014-08-31
      • 1970-01-01
      • 2019-01-03
      • 1970-01-01
      相关资源
      最近更新 更多