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

后端 未结 3 1446
醉话见心
醉话见心 2020-12-18 09:03

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条回答
  • 2020-12-18 09:31

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

    0 讨论(0)
  • 2020-12-18 09:41

    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?
    0 讨论(0)
  • 2020-12-18 09:54
         String path="/WEB-INF/list.properties";
    
        InputStream is=FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(path);
        InputStreamReader r = new InputStreamReader(is);
        BufferedReader br = new BufferedReader(r);
    
    0 讨论(0)
提交回复
热议问题