views:

137

answers:

2

I created a jsf custom tag (I'm not sure that it's correct, I could miss something easily, so I attached code below).

Now I'm trying to use this tag but I get an error:

error on line 28 at column 49: Namespace prefix gc on ganttchart is not defined

So, here is the xhtml-page:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
<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:gc="http://myganttchart.org"&gt;

<body>
<ui:composition template="/masterpage.xhtml">
    <ui:define name="title">Gantt chart test</ui:define>
    <ui:define name="content">
        <f:view>
            <gc:ganttchart width="300" height="100" rendered="true"/>
            ...
        </f:view>
    </ui:define>
</ui:composition>
</body>
</html>

And here is tld-file (it's placed in WEB-INF/):

<taglib xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
        xmlns="http://java.sun.com/xml/ns/javaee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.1">

    <tlib-version>
        1.0
    </tlib-version>
    <short-name>
        oext
    </short-name>
    <uri>
        http://myganttchart.org
    </uri>

    <tag>
        <name>ganttchart</name>
        <tag-class>usermanagement.support.ganttchart.GanttChartTag</tag-class>
        <body-content>empty</body-content>

        <attribute>
            <name>binding</name>
            <deferred-value>
                <type>javax.faces.component.UIComponent</type>
            </deferred-value>
        </attribute>

        ...
    </tag>
</tablib>

Here is a part of tag-class code:

public class GanttChartTag extends UIComponentELTag {

    private ValueExpression width;
    private ValueExpression height;
    private ValueExpression styleClass;

    public String getComponentType () {
        return "org.myganttchart";
    }

    public String getRendererType () {
        return null;  
    }
    ...
}

Correspondent block from faces-config:

<component>
    <component-type>org.myganttchart</component-type>
    <component-class>usermanagement.support.ganttchart.UIGanttChart</component-class>
</component>

And the last part if UIGanttChart:

public class UIGanttChart extends UIOutput {

    public UIGanttChart() {
        setRendererType (null);
    }

    //some test code
    public void encodeBegin(FacesContext context) throws IOException {
        ResponseWriter writer = context.getResponseWriter ();
        writer.startElement("img", this);
        writer.writeAttribute("src", "no-img", "source");
        writer.writeAttribute("width", getAttributes ().get ("width"), "width");
        writer.writeAttribute("height", getAttributes ().get ("height"), "height");
        writer.writeAttribute("class", ".someclass", "styleClass");
        writer.endElement("img");
    }

}

So, what did I miss? Any ideas about how to debug or where can be the problem are welcome.

+2  A: 

You're using Facelets, but defining a JSP tag. Facelets have their own tag definition files (with a .taglib.xml suffix).

If you're using JSF 1.2 with Facelets, you'll find the DTD here. If you're using JSF 2.0, a schema is defined in the JSR 314 spec (Appendix A, section 1.2).

McDowell
+3  A: 

You need to define your new taglib a-la-facelets.

Create a file called "gc.taglib.xml" under /WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC "-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
                                "https://facelets.dev.java.net/source/browse/*checkout*/facelets/src/etc/facelet-taglib_1_0.dtd"&gt;
<facelet-taglib>
 <namespace>http://org.myganttchart/gc&lt;/namespace&gt;
 <tag>
        <tag-name>gantchart</tag-name>
        <component>
        <component-type>org.myganttchart</component-type>
        </component>
    </tag>
</facelet-taglib>

And keep what you did with faces-config.xml.

pakore