如何将Facelets合成与其他上下文中的文件一起使用 [英] How to use Facelets composition with files from another context

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

问题描述

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

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.

<ui:composition template="http://localhost:8080/templates/layout/foo.xhtml">

我在JSF 1中使用JBoss Seam 2.x.

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文件夹中,然后将resolver类放入同一JAR中.最后,在所有Web应用程序中分发此JAR.

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中注册它:

Register it in web.xml as follows:

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

这篇关于如何将Facelets合成与其他上下文中的文件一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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