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

2020-02-11 07:24发布

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?

3条回答
来,给爷笑一个
2楼-- · 2020-02-11 07:34
     String path="/WEB-INF/list.properties";

    InputStream is=FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(path);
    InputStreamReader r = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(r);
查看更多
走好不送
3楼-- · 2020-02-11 07:42

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

查看更多
霸刀☆藐视天下
4楼-- · 2020-02-11 07:54

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:

查看更多
登录 后发表回答