Reputation: 12413
I am learning to create my own custom tags, but i am having a bit of trouble, i cant make this simple app use the tag i created. I think i did everything fine, but i am afraid the paths to the new library i created is wrong. Maybe someone can help me find where my error is and understand the reason of it. This is what i did so far:
1- I created the tag as an xhtml chunk(mybutton.xhtml)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core">
<ui:composition>
<h:commandButton type="submit" value="#{buttonSubmitLabel}" />
<h:commandButton type="reset" value="#{buttonResetLabel}" />
</ui:composition>
</html>
2- Then i created an .xml file that will act as the libraries where all my custom tags are indexed(mytagsconfig.taglib.xml)
<?xml version="1.0"?>
<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://mytags.com/facelets</namespace>
<tag>
<tag-name>mybutton</tag-name>
<source>mytags/mybutton.xhtml</source>
</tag>
</facelet-taglib>
3- I tried to register my new library in the web.xml, so i can use it
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>CHAPTER 5 Creating your own Custom tags</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.xhtml</url-pattern>
</servlet-mapping>
<!-- REGISTERING A CUSTOM TAG INTO JSF APPLICATION -->
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/mytagsconfig.taglib.xml</param-value>
</context-param>
</web-app>
4- And last i try to use the tag in some page(In my case inside a component that is inserted in a template)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:mytags="http://mytags.com/facelets">
<ui:composition template="WEB-INF/templates/masterLayout.xhtml">
<ui:define name="pagetitle">
Defining custom tags
</ui:define>
<ui:define name="content">
Defining custom tags is a 3 step process:
<ul>
<li>Use ui:compisition to create some content.(Custom tags are stored in WEB-INF/customtags)</li>
<li>Declares the custom tag in a tag library descriptor into the WEB-INF folder(Example: mycustomtags.taglib.xml).</li>
<li>Register the tag library descriptor in the web.xml.</li>
</ul>
<!-- Here should go a call to the new created tag -->
<mytags:mybutton buttonSubmitLabel="Submit" buttonResetLabel="Reset" />
</ui:define>
</ui:composition>
</html>
This is my folder structure:
****Update**** When i build i see the index.xhtml page, but the custom tag is not there(I dont see the 2 buttons)
Upvotes: 0
Views: 6737
Reputation: 1108537
Your taglib declaration in web.xml
does not point the right filename.
You said that you've created a /WEB-INF/mytagsconfig.taglib.xml
, but you've declared it in web.xml
as /WEB-INF/mytags.taglib.xml
instead. Fix it accordingly.
Not directly related to the problem, but consider upgrading to JSF/Facelets 2.0 compatible taglib root declaration and web.xml
context param name.
<?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">
<!-- Taglib config here. -->
</facelet-taglib>
and
<context-param>
<param-name>javax.faces.FACELETS_LIBRARIES</param-name>
<param-value>/WEB-INF/mytagsconfig.taglib.xml</param-value>
</context-param>
Upvotes: 7