I am working on a JSF website and I need some help. I have an XML file that I am trying to read from by backing bean but I don't know how to find the path to it. It is in my resources folder (resources/movies.xml). How do I do this?
            Asked
            
        
        
            Active
            
        
            Viewed 6,529 times
        
    3
            
            
        - 
                    1Possible duplicate of http://stackoverflow.com/questions/10590003/jsf-how-to-get-handle-to-a-resource-within-a-backing-bean – ElderMael Nov 06 '12 at 05:24
2 Answers
8
            
            
        If it is indeed the /resources folder of the public web content where you usually store the static web resources like CSS/JS/images, then you can use ExternalContext#getResourceAsStream() to get an InputStream of it.
ExternalContext externalContext = FacesContext.getCurrentInstance().getExternalContext();
InputStream input = externalContext.getResourceAsStream("/resources/movies.xml");
// ...
 
    
    
        BalusC
        
- 1,082,665
- 372
- 3,610
- 3,555
-1
            
            
        This is how I get path of folder in webapp resources folder:
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String resHomeImgPath = servletContext.getRealPath("resources/img/home");
 
    
    
        Igor Guzenko
        
- 1
- 1
- 
                    
- 
                    2getRealPath() is bad practice: http://stackoverflow.com/q/12160639 Use getResource()/getResourceAsStream() instead. – BalusC Feb 29 '16 at 10:40
