1

I have a custom JSF component which is registered in a .tld file. It works fine in JSP when I declare it as below:

<%@taglib uri="http://example.com/ui" prefix="ex"%>

However, it doesn't work in Facelets when I try to declare as below:

<html xmlns:ex="http://example.com/ui">

How can I use my custom JSF component in Facelets too?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Jekin Kalariya
  • 3,255
  • 2
  • 18
  • 31

1 Answers1

5

JSP and Facelets are entirely distinct view technologies. JSP is Servlet based while Facelets is XML based. You can't reuse tags/taglibs of the one on the other. What *.tld files for JSP are, are *.taglib.xml files for Facelets.

Here's a kickoff example of how a Facelets taglib file look like for JSF 2.0:

<?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd"
    version="2.0">

    <namespace>http://example.com/ui</namespace>

    <tag>
        <tag-name>foo</tag-name>
        <component>
            <component-type>com.example.Foo</component-type>
        </component>
    </tag>
</facelet-taglib>

If you have a component library in flavor of a JAR file, just drop it in its /META-INF folder. It will be auto-discovered. If you have however those custom components coupled in the WAR itself, then drop it in /WEB-INF folder and register it in web.xml via below context param:

<context-param>
    <param-name>javax.faces.FACELETS_LIBRARIES</param-name>
    <param-value>/WEB-INF/example-ui.taglib.xml</param-value>
</context-param>

If you indend to pick JSF 2.2 as a minimum requirement, update the taglib's root declaration as below:

<facelet-taglib
    xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-facelettaglibrary_2_2.xsd"
    version="2.2">

See also:

Noted should be that JSP is since 2009 deprecated as view technology for JSF. So if you intend to build a new custom component library, making it JSP compatible would be a complete waste of effort as no one sane JSF developer would use it. Moreover, practically all JSF 2.x component libraries don't support JSP (anymore).

Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452