How to use Facelets composition with files from another context
Asked Answered
G

1

4

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.

Geotropism answered 7/4, 2011 at 21:21 Comment(0)
A
10

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>
Accomplish answered 8/4, 2011 at 0:35 Comment(3)
Thank you @BalusC. I'm using JBoss Seam 2.x with JEE 5, and I can't find ResourceResolver class.Disseisin
@BalusC: I see that ResourceResolver has been deprecated.Adin
@Shirgill: It's only deprecated in JSF 2.2. This answer is targeted on JSF 1.x.Accomplish

© 2022 - 2024 — McMap. All rights reserved.