Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reference file resource in a JSF Application

I want to dynamically reference an XSD from a bean, how is this possible? I already added the XSD to the project, so it's located somewhere in the GlassFish domain.

like image 258
Daniel Szalay Avatar asked Oct 06 '09 21:10

Daniel Szalay


1 Answers

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);
like image 89
McDowell Avatar answered Sep 21 '22 15:09

McDowell