How to create a custom Facelets tag?
Asked Answered
A

1

11

I am using JSF 2.0. I created custom JSTL tags with tagfiles and that were working fine in JSP. But I want to use custom JSTL tags in Facelets too. Is it possible to create tagfiles in Facelets or not?

Ani answered 7/2, 2013 at 12:55 Comment(2)
Possible duplicate of: stackoverflow.com/questions/7726523/…Artless
Thanks I have check that question as well but I have created custom jstl tags that is working fine in jsp but when I use that tag in faces by giving "xmlns:ct="ve.customTag" namespace then its not working in faces. kindly tell that how to use my custom jstl tags in jsfAni
F
18

I created custom JSTL tags with tagfiles and that were working fine in JSP.

The phrase "custom JSTL tags" makes no sense. JSTL is already a taglib at its own. Read the introductory paragraphs of our JSTL wiki page to learn what JSTL really is. You perhaps actually meant "custom JSP tags". Of course they would not work in Facelets as that's a completely different view technology than JSP and actually the successor of the deprecated JSP. See also Why Facelets is preferred over JSP as the view definition language from JSF2.0 onwards?

But I want to use custom JSTL tags in Facelets too

The analogy of a "custom JSP tag" in Facelets is a "custom Facelets tag", or more commonly "Facelets tag file". It's rather simple, you can follow the same syntax as an include file.

/WEB-INF/tags/some.xhtml:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets">
    Hello World
    ...
    <ui:insert /> <!-- This inserts tag body, if necessary. -->
</ui:composition>

and register it in /WEB-INF/example.taglib.xml as follows:

<?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/jsf/facelets</namespace>
    <tag>
        <tag-name>some</tag-name>
        <source>tags/some.xhtml</source>
    </tag>
</facelet-taglib>

which is in turn registered in /WEB-INF/web.xml as follows:

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

(note that registration in web.xml is unnecessary when the *.taglib.xml file is in /META-INF folder of a JAR in /WEB-INF/lib)

and finally use it in your templates as follows:

<html ... xmlns:my="http://example.com/jsf/facelets">
...
<my:some />

See also:

Fixed answered 7/2, 2013 at 13:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.