How to use Facelets composition with files from another context

谁说胖子不能爱 提交于 2019-11-26 08:33:52

问题


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.

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\">

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


回答1:


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

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;
    }

}

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>


来源:https://stackoverflow.com/questions/5587808/how-to-use-facelets-composition-with-files-from-another-context

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!