【发布时间】:2016-11-28 14:07:07
【问题描述】:
有什么方法可以访问web.xml 中的welcome-file-list 元素,而不必重新解析web.xml 本身?
【问题讨论】:
标签: jsf servlets web.xml welcome-file
有什么方法可以访问web.xml 中的welcome-file-list 元素,而不必重新解析web.xml 本身?
【问题讨论】:
标签: jsf servlets web.xml welcome-file
没有。没有公共的 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();
【讨论】: