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>
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…