2010-04-26 2 views
3

J'ai créé un tag personnalisé jsf (je ne suis pas sûr que ce soit correct, je pourrais manquer quelque chose facilement, donc j'ai joint le code ci-dessous).Pourquoi ai-je une erreur "le préfixe [..] n'est pas défini" lorsque j'utilise ma balise personnalisée jsf?

Maintenant, je suis en train d'utiliser cette balise mais je reçois une erreur:

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

Alors, voici le xhtml page:

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

Et voici tld -file (il est placé dans 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> 

Voici une partie de 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; 
    } 
    ... 
} 

bloc correspondant de faces-config:

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

Et la dernière partie si 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"); 
    } 

} 

Alors, qu'est-ce que je manque? Toutes les idées sur la façon de déboguer ou où peut être le problème sont les bienvenus.

Répondre

3

Vous devez définir votre nouveau taglib a-la-facelets.

Créer un fichier appelé "gc.taglib.xml" sous/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"> 
<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> 

Et gardez ce que vous avez fait avec faces-config.xml.

2

Vous utilisez Facelets, mais en définissant une balise JSP. Facelets ont leurs propres fichiers de définition de balises (avec un suffixe .taglib.xml).

Si vous utilisez JSF 1.2 avec Facelets, vous trouverez la DTD here. Si vous utilisez JSF 2.0, un schéma est défini dans le JSR 314 spec (Annexe A, section 1.2).

Questions connexes