如何在JSF应用程序中引用文件资源
我想从bean动态引用XSD,这怎么可能? 我已经将XSD添加到项目中,因此它位于GlassFish域中的某个位置。
使用ExternalContext
。
如果要在bean中加载资源,请通过getResource或getResourceAsStream执行 :
InputStream stream = FacesContext.getCurrentInstance().getExternalContext() .getResourceAsStream("/foo.xsd");
如果要返回资源的URL,请使用getRequestContextPath获取相对于主机根目录的路径:
ExternalContext ext = FacesContext.getCurrentInstance() .getExternalContext(); String path = ext.getRequestContextPath(); path += path.endsWith("/") ? "foo.xsd" : "/foo.xsd"; String url = ext.encodeResourceURL(path);