如何对来自另一个上下文的文件使用 Facelets 组合 [英] How to use Facelets composition with files from another context

查看:26
本文介绍了如何对来自另一个上下文的文件使用 Facelets 组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用组合的应用程序(用于页面模板).但是我们认为在创建一个网络应用程序(战争)时,将所有应用程序共享的所有模板托管在所有应用程序的同一主机中.

I have an application that use composition (for page templates). But we think in create a web-application (war) to host all templates shared by all applications in the same host of all applications.

如何包含来自其他上下文的模板?此时我使用从 http 请求导入.但听起来很糟糕.

How I can include a template from another context? At this time I use import from http request. But it's sounds like bad.

我使用 JBoss Seam 2.x 和 JSF 1.

I'm using JBoss Seam 2.x with JSF 1.

推荐答案

请注意,这在 JSF 2.x Facelets 中的处理方式有所不同,请参阅 这个答案 详情.

Note that this is to be done differently in JSF 2.x Facelets, see this answer for detail.

这可以通过自定义 Facelets 资源解析器实现.我不仅不会通过 HTTP 解析它们,而只会从类路径中解析它们.只需将共享模板打包在例如 JAR 文件的 /META-INF/resources 文件夹中,并将解析器类放在同一个 JAR 中.最后将这个 JAR 分发给所有的 webapp.

This is possible with a custom Facelets resource resolver. I would only not resolve them by HTTP, but just from the classpath. Just package the shared templates in for example the /META-INF/resources folder of the JAR file and drop the resolver class in the same JAR. Finally distribute this JAR among all webapps.

package com.example;

import java.net.URL;

import com.sun.facelets.impl.DefaultResourceResolver;

public class FaceletsResourceResolver extends DefaultResourceResolver {

    private String basePath;

    public FaceletsResourceResolver() {
        this.basePath = "/META-INF/resources"; // TODO: Make configureable?
    }

    public URL resolveUrl(String path) {
        URL url = super.resolveUrl(path); // Resolves from WAR.

        if (url == null) {
            url = getClass().getResource(basePath + path); // Resolves from JAR.
        }

        return url;
    }

}

web.xml中注册如下:

<context-param>
    <param-name>facelets.RESOURCE_RESOLVER</param-name>
    <param-value>com.example.FaceletsResourceResolver</param-value>
</context-param>

这篇关于如何对来自另一个上下文的文件使用 Facelets 组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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