如何在JSF应用程序中引用文件资源 [英] How to reference file resource in a JSF Application
本文介绍了如何在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);
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.
解决方案
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);
这篇关于如何在JSF应用程序中引用文件资源的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文