在 web.xml 中访问 welcome-file-list 的值

Access the value of welcome-file-list in web.xml

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

没有。没有 public 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, you can use its WebXml utility class

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

另请参阅:

  • How to read session-timout from web.xml