Zerok
Zerok

Reputation: 227

Something wrong with TLDs, Taglibs and attribute setter method

I'm giving a try to custom tags at JSP. I followed a tutorial, and ended up with this code:

Taglib import:

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

Here I implement my tag:

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

This is what my TLD looks like (generated by 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>

And this, is my tag handler class:

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


}

Well, this should be working. But...:

enter image description here

What is wrong here?

Upvotes: 0

Views: 189

Answers (1)

Zerok
Zerok

Reputation: 227

I realised what was going wrong: the tag handler class wasn't in a package. Once I've put the class into a package (per example, "tags"), and referred to it by

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

...it began working!

Upvotes: 1

Related Questions