How to get properties file from /WEB-INF folder in JSF?

自闭症网瘾萝莉.ら 提交于 2019-12-18 05:11:12

问题


I have some properties file in /WEB-INF. And I want to load it in a JSF managed bean. Is there any way to do that?


回答1:


Use either ExternalContext#getResource() or ExternalContext#getResourceAsStream() wherein you pass the webcontent-relative path.

E.g.:

ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
Properties properties = new Properties();
// ...
properties.load(externalContext.getResourceAsStream("/WEB-INF/file.properties"));

This delegates under the covers to ServletContext#getResource()/getResourceAsStream().

See also:

  • Where to place and how to read configuration resource files in servlet based application?



回答2:


Put it in WEB-INF/classes. That is part of the classpath.




回答3:


     String path="/WEB-INF/list.properties";

    InputStream is=FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(path);
    InputStreamReader r = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(r);


来源:https://stackoverflow.com/questions/2139367/how-to-get-properties-file-from-web-inf-folder-in-jsf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!