lundi 4 août 2014

How to reference file resource in a JSF Application

Use the ExternalContext.
If you want to load the resource in the bean, do it via getResource or getResourceAsStream:
InputStream stream = FacesContext.getCurrentInstance().getExternalContext()
    .getResourceAsStream("/foo.xsd");
If you want to return a URL to the resource, use getRequestContextPath to get the path relative to the host root:
ExternalContext ext = FacesContext.getCurrentInstance()
    .getExternalContext();
String path = ext.getRequestContextPath();
path += path.endsWith("/") ? "foo.xsd" : "/foo.xsd";
String url = ext.encodeResourceURL(path);

Aucun commentaire:

Enregistrer un commentaire