从外部文件系统或数据库获取Facelets模板/文件 [英] Obtaining Facelets templates/files from an external filesystem or database
问题描述
我能够成功将其与我的应用程序中的模板一起使用:
I am able to successfully get this to work with the template in my app:
<ui:decorate template="/WEB-INF/templates/mytemplate.xhtml">
我还可以将模板移至 JAR的/META-INF/templates/mytemplate.xhtml
并使其起作用:
I am also able to move template to /META-INF/templates/mytemplate.xhtml
of a JAR and get this to work:
<ui:decorate template="/templates/mytemplate.xhtml">
我实际上想将此文件放到文件系统(或与此相关的数据库)上.我怎样才能做到这一点?我发现了很多与com.sun.facelets.impl.DefaultResourceResolver
相关的内容,但我认为这实际上与覆盖模板的服务无关.它不是在尝试解析URL,而是在试图以某种方式在类路径上获取文件.
I would actually like to put this file onto filesystem (or database for that matter). How can I achieve this? I found plenty of things related to com.sun.facelets.impl.DefaultResourceResolver
, but I don't think that is actually related to override the serving of the template. It is not trying resolve a URL, it is simply trying to get the file somehow on the classpath.
推荐答案
如果您已经在使用JSF 2.2,则可以通过提供自定义 createViewResource()
.
If you're already on JSF 2.2, you can do this by providing a custom ResourceHandler
wherein you return the desired view resource in createViewResource()
.
public class FaceletsResourceHandler extends ResourceHandlerWrapper {
private ResourceHandler wrapped;
public FaceletsResourceHandler(ResourceHandler wrapped) {
this.wrapped = wrapped;
}
@Override
public ViewResource createViewResource(FacesContext context, final String name) {
ViewResource resource = super.createViewResource(context, name);
if (resource == null) {
resource = new ViewResource() {
@Override
public URL getURL() {
try {
return new File("/some/base/path", name).toURI().toURL();
} catch (MalformedURLException e) {
throw new FacesException(e);
}
}
};
}
return resource;
}
@Override
public ResourceHandler getWrapped() {
return wrapped;
}
}
在faces-config.xml
中注册的地址如下:
<application>
<resource-handler>com.example.FaceletsResourceHandler</resource-handler>
</application>
或者如果您尚未使用JSF 2.2,请使用 ResourceResolver
代替.
Or if you're not on JSF 2.2 yet, then use ResourceResolver
instead.
public class FaceletsResourceResolver extends ResourceResolver {
private ResourceResolver parent;
public FaceletsResourceResolver(ResourceResolver parent) {
this.parent = parent;
}
@Override
public URL resolveUrl(String path) {
URL url = parent.resolveUrl(path); // Resolves from WAR.
if (url == null) {
try {
url = new File("/some/base/path", path).toURI().toURL();
} catch (MalformedURLException e) {
throw new FacesException(e);
}
}
return url;
}
}
在web.xml
中注册的地址如下:
<context-param>
<param-name>javax.faces.FACELETS_RESOURCE_RESOLVER</param-name>
<param-value>com.example.FaceletsResourceResolver</param-value>
</context-param>
无论采用哪种方式,为了从数据库中提供资源,您都可以将它们保存/缓存在(临时)磁盘文件系统上,这样就可以仅通过File
提供URL
,或者发明一个自定义协议,例如db://
并提供自定义 URLStreamHandlerFactory
和 URLStreamHandler
实施,以执行从DB进行流式传输的实际工作.您可以在此处注册和使用自定义Java来找到启动示例. net.URL协议.
Regardless of the way, in order to provide the resource from the database, you'd either save/cache them on (temp) disk file system so you can provide the URL
just via File
, or invent a custom protocol such as db://
and provide a custom URLStreamHandlerFactory
and URLStreamHandler
implementation to perform the actual job of streaming from the DB. You can find a kickoff example here Registering and using a custom java.net.URL protocol.
这篇关于从外部文件系统或数据库获取Facelets模板/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!