Freitag, 4. Juni 2010

A non-obvious pitfall when using a custom facelets resource resolver

Sometimes it can be helpful to use a custom facelets resource resolver. This resource resolver resolves the XHTML snippets during facelet composition, for instance in:
<ui:composition template="/masterTemplate.xhtml" /> or <ui:include src="/toInclude.xhtml" />

A pitfall is to omit the leading slash in /masterTemplate.xhtml or /toInclude.xhtml. The custom resource resolver is only used WITH leading slash. The reason is the implementation of com.sun.facelets.impl.DefaultFaceletFactory#resolveURL(...), where this.resolver is the custom resource resolver:

public URL resolveURL(URL source, String path) throws IOException {
  if (path.startsWith("/")) {
    URL url = this.resolver.resolveUrl(path);
    if (url == null) {
      throw new FileNotFoundException(path + " Not Found in ExternalContext as a Resource");
    }
    return url;
  } else {
  return new URL(source, path);
  }
}