繁体   English   中英

当我尝试使用我的jsf自定义标记时,为什么会出现错误“前缀[..]未定义”?

[英]Why do I get error “prefix [..] is not defined” when I try to use my jsf custom tag?

我创建了一个jsf自定义标签(我不确定它是否正确,我可能很容易错过,所以我在下面附上代码)。

现在我正在尝试使用此标记但是我收到错误:

第49行第28行的错误:未定义ganttchart上的命名空间前缀gc

所以,这是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"
      xmlns:gc="http://myganttchart.org">

<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>

这里是tld -file(它放在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>

这是tag-class代码的一部分:

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;  
    }
    ...
}

来自faces-config通讯块:

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

最后一部分如果是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");
    }

}

那么,我错过了什么? 任何有关如何调试或在哪里可能是问题的想法都是受欢迎的。

您需要定义新的taglib a-la-facelets。

在/ WEB-INF下创建一个名为“gc.taglib.xml”的文件

<?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">
<facelet-taglib>
 <namespace>http://org.myganttchart/gc</namespace>
 <tag>
        <tag-name>gantchart</tag-name>
        <component>
        <component-type>org.myganttchart</component-type>
        </component>
    </tag>
</facelet-taglib>

并使用faces-config.xml保留你所做的。

您正在使用Facelets,但定义了JSP标记。 Facelets有自己的标记定义文件(后缀为.taglib.xml )。

如果你正在使用带有Facelets的JSF 1.2,你可以在这里找到DTD。 如果您使用的是JSF 2.0,则在JSR 314规范中定义了一个模式(附录A,第1.2节)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM