0
votes

I've got huge problem with custom tag in JSP. I've created a Tag Library Descriptor file in WEB-INF/tlds called "decorate". I set the prefix to "d". Then I've created tag handler class called "decorateHandler".

I wanted to decorate text in my JSP title page in simple way, but I get an error...

My decorate.tld file:

<tlib-version>1.0</tlib-version>
  <short-name>l</short-name>
  <uri>/WEB-INF/tlds/decorate</uri>
  <tag>
    <name>decorate</name>
    <tag-class>decorateHandler</tag-class>
    <body-content>JSP</body-content> 
    <attribute>
        <name>value</name>
    </attribute>
  </tag>
</taglib>

In decorateHandler.java I tried to do that:

private String value;

    private void writeTagBodyContent(JspWriter out, BodyContent bodyContent) throws IOException {

        out.println("<font color='"+value+"'>");
        bodyContent.writeOut(out);
        out.println("</font>");

        bodyContent.clearBody();
    }

Of course I've created getter and setter for String value variable.

In index.jsp I've included taglib like so:

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

and I tried something like this:

 <d:decorate value="red">Lorem ipsum</d:decorate>

But I've got an exception:

org.apache.jasper.JasperException: Unable to compile class for JSP: 

An error occurred at line: [112] in the jsp file: [/index.jsp]
decorateHandler cannot be resolved to a type
109:                         <li class="nav-item">
110:                             <a class="nav-link" href="index.jsp">
111:                                 <img src="logo.png" height="20px" width="20px">
112:                                 <d:decorate value="red">Lorem ipsum</d:decorate>
113:                             </a>
114:                         </li>
115:                     </ul>      


An error occurred at line: [112] in the jsp file: [/index.jsp]
decorateHandler cannot be resolved to a type
109:                         <li class="nav-item">
110:                             <a class="nav-link" href="index.jsp">
111:                                 <img src="logo.png" height="20px" width="20px">
112:                                 <d:decorate value="red">Lorem ipsum</d:decorate>
113:                             </a>
114:                         </li>
115:                     </ul>      


An error occurred at line: [112] in the jsp file: [/index.jsp]
The method get(Class<? extends Tag>) in the type TagHandlerPool is not applicable for the arguments (Class<decorateHandler>)
109:                         <li class="nav-item">
110:                             <a class="nav-link" href="index.jsp">
111:                                 <img src="logo.png" height="20px" width="20px">
112:                                 <d:decorate value="red">Lorem ipsum</d:decorate>
113:                             </a>
114:                         </li>
115:                     </ul>      


An error occurred at line: [112] in the jsp file: [/index.jsp]
decorateHandler cannot be resolved to a type
109:                         <li class="nav-item">
110:                             <a class="nav-link" href="index.jsp">
111:                                 <img src="logo.png" height="20px" width="20px">
112:                                 <d:decorate value="red">Lorem ipsum</d:decorate>
113:                             </a>
114:                         </li>
115:                     </ul>      


Stacktrace:
    org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:212)
    org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:549)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:350)
    org.apache.jasper.compiler.Compiler.compile(Compiler.java:334)
    org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:595)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:399)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:386)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:330)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
    Filtr2.doFilter(Filtr2.java:67)

I don't know what is the issue... :/

1

1 Answers

0
votes

First of all I think that you should check the logs of your web container (e.g. Tomcat, Glassfish?). The stacktrace of that error will be available there and hint you what the actual cause is. Anyway, at first glance, it's clear that the error is caused by the custom decorator taglib, which seems to do a bodyContent.clearBody() at the end. I don't think that's allowed here, maybe you meant to do a bodyContent.flush() instead?

Here a tutorial on how to create custom jsp tags.