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?
            Asked
            
        
        
            Active
            
        
            Viewed 9,299 times
        
    4
            
            
        3 Answers
14
            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:
- 
                    Could you give me a hint, what classes are subclasses of ExternalContext (ExternalContext is abstract) or where to get externalContext object from? – Lukasz Czerwinski Oct 15 '12 at 10:47
- 
                    @LukaszCzerwinski : ExternalContext externalContext = facesContext.getExternalContext(); – Abdennour TOUMI Aug 24 '14 at 07:02
1
            
            
        Put it in WEB-INF/classes. That is part of the classpath.
 
    
    
        Kees de Kooter
        
- 7,078
- 5
- 38
- 45
- 
                    2...which is not necessary for `ExternalContext#getResource()` methods by the way. – BalusC Jan 26 '10 at 12:48
0
            
            
             String path="/WEB-INF/list.properties";
    InputStream is=FacesContext.getCurrentInstance().getExternalContext().getResourceAsStream(path);
    InputStreamReader r = new InputStreamReader(is);
    BufferedReader br = new BufferedReader(r);
 
    
    
        Abdennour TOUMI
        
- 87,526
- 38
- 249
- 254
 
     
    