2016-05-24 46 views
0

我試着在JSP上定製標籤。我跟着一個教程,並結束了這段代碼:TLDs,Taglibs和屬性設置器方法有問題

標籤庫導入:

<%@taglib prefix="me" uri="/WEB-INF/tlds/myTLD.tld" %> 

在這裏我實現了我的標籤:

<body> 
    <h1>Testing custom tags</h1> 
    <me:MiTag titulo="Some title"> 
     A test text 
    </me:MiTag> 
</body> 

這是我的TLD樣子(通過NetBeans的產生):

<?xml version="1.0" encoding="UTF-8"?> 
<taglib version="2.1" 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-jsptaglibrary_2_1.xsd"> 
    <tlib-version>1.0</tlib-version> 
    <short-name>mytld</short-name> 
    <uri>/WEB-INF/tlds/myTLD</uri> 
    <tag> 
    <name>MiTag</name> 
    <tag-class>MiTag</tag-class> 
    <body-content>scriptless</body-content> 
    <attribute> 
     <name>titulo</name> 
     <required>true</required> 
     <rtexprvalue>false</rtexprvalue> 
     <type>java.lang.String</type> 
    </attribute> 
    </tag> 
</taglib> 

而這,就是我的標籤處理類:

public class MiTag extends SimpleTagSupport { 

    private String titulo; 

    @Override 
    public void doTag() throws JspException { 
     JspWriter out = getJspContext().getOut(); 

     try { 
      out.println("<h3>"+titulo+"</h3>"); 
      out.println(" <blockquote>"); 

      JspFragment f = getJspBody(); 
      if (f != null) { 
       f.invoke(out); 
      } 
      out.println(" </blockquote>"); 
     } catch (java.io.IOException ex) { 
      throw new JspException("Error in MiTag tag", ex); 
     } 
    } 

    /** 
    * @param titulo the Titulo to set 
    */ 
    public void setTitulo(String titulo) { 
     this.titulo = titulo; 
    } 


} 

那麼,這個應該正在工作。但是...:

enter image description here

這裏有什麼問題?

回答

1

我意識到出了什麼問題:標籤處理程序類不在包中。一旦我將課程放入一個包(每個示例,「標籤」)中並且通過

<tag-class>tags.MiTag</tag-class> 

來引用它時,它就開始工作了!