从外部文件系统或数据库获取 Facelets 模板/文件 [英] Obtaining Facelets templates/files from an external filesystem or database

查看:17
本文介绍了从外部文件系统或数据库获取 Facelets 模板/文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够成功地让它与我的应用程序中的模板一起使用:

I am able to successfully get this to work with the template in my app:

<ui:decorate template="/WEB-INF/templates/mytemplate.xhtml">

我还可以将模板移动到 JAR 并让它工作:

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,您可以通过提供一个自定义的 ResourceHandler 其中您在 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 代替.

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>

<小时>

无论如何,为了从数据库提供资源,您要么将它们保存/缓存在(临时)磁盘文件系统上,这样您就可以仅通过 URL 提供 URL>File,或者发明一个自定义的协议,比如 db:// 并提供一个自定义的 URLStreamHandlerFactoryURLStreamHandler 实现以执行从数据库流式传输的实际工作.您可以在此处找到启动示例 注册和使用自定义 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆