Java 如何在JSF应用程序中引用文件资源

Java 如何在JSF应用程序中引用文件资源,java,web-applications,jsf,glassfish,Java,Web Applications,Jsf,Glassfish,我想从bean中动态引用XSD,这怎么可能呢?我已经将XSD添加到项目中,因此它位于GlassFish域中的某个位置。使用ExternalContext 如果要在bean中加载资源,请通过或: 如果要返回资源的URL,请使用获取相对于主机根目录的路径: ExternalContext ext = FacesContext.getCurrentInstance() .getExternalContext(); String path = ext.getRequestContextPath(

我想从bean中动态引用XSD,这怎么可能呢?我已经将XSD添加到项目中,因此它位于GlassFish域中的某个位置。

使用
ExternalContext

如果要在bean中加载资源,请通过或:

如果要返回资源的URL,请使用获取相对于主机根目录的路径:

ExternalContext ext = FacesContext.getCurrentInstance()
    .getExternalContext();
String path = ext.getRequestContextPath();
path += path.endsWith("/") ? "foo.xsd" : "/foo.xsd";
String url = ext.encodeResourceURL(path);

非常感谢你!我将其作为一个InputStream,之后我可以通过BufferedReader InputStreamReader组合来读取XSD:)
ExternalContext ext = FacesContext.getCurrentInstance()
    .getExternalContext();
String path = ext.getRequestContextPath();
path += path.endsWith("/") ? "foo.xsd" : "/foo.xsd";
String url = ext.encodeResourceURL(path);