N
N
Nikolay Baranenko2016-09-01 22:25:59
Java
Nikolay Baranenko, 2016-09-01 22:25:59

Why does an Unable to find taglib error occur when calling custom tag?

Hello.

For the first time I decided to try out the custon taglib

java class

package RU.Tags.Examples;

        import javax.servlet.jsp.tagext.*;
        import javax.servlet.jsp.*;
        import java.io.*;

public class CustomAttribute extends SimpleTagSupport {

    private String message;

    public void setMessage(String msg) {
        this.message = msg;
    }

    StringWriter sw = new StringWriter();

    public void doTag()
            throws JspException, IOException
    {
        if (message != null) {
          /* Use message from attribute */
            JspWriter out = getJspContext().getOut();
            out.println("Первый кастом таг :"+ message );
        }
        else {
          /* use message from the body */
            getJspBody().invoke(sw);
            getJspContext().getOut().println(sw.toString());
        }
    }

}


custom_tag_attribute.tld is located at WEB-INF/jstl/custom_tag_attribute.tld

<?xml version="1.0" encoding="UTF-8" ?>

<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
        version="2.0">

    <description>JSTL 1.1 core library</description>
    <display-name>JSTL core</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>CustomAttribute</short-name>


    <tag>
        <name>Hello</name>
        <tag-class>RU.Tags.Examples.CustomAttribute</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>message</name>
        </attribute>
    </tag>

</taglib>


custom taglib call in JSP

examples.jsp is located in the root folder of the project newproject/examples.jsp

<%@ taglib uri="/WEB-INF/jstl/custom_tag_attribute.tld" prefix="CustomAttribute" %>

<CustomAttribute:Hello message="This is custom tag" />

Run codeCopy code in responseExpand fragment
Everything is fine, BUT this option works if the project works in the root folder, for example, localhost:8000/examples.jsp

If you access it this way, localhost/newproject/examples.jsp , then an error occurs

HTTP Status 500 - Unable to find taglib "CustomAttribute" for URI: /WEB-INF/jstl/custom_tag_attribute.tld

type Exception report

message Unable to find taglib "CustomAttribute" for URI: /WEB-INF/jstl/custom_tag_attribute.tld

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to find taglib "CustomAttribute" for URI: /WEB-INF/jstl/custom_tag_attribute.tld

How can this error be eliminated?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nikolay Baranenko, 2016-09-01
@drno-reg

completely forgot about web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <jsp-config>
        <!-- JSTL Tag Library Local Descriptors -->
        <taglib>
            <taglib-uri>/WEB-INF/jstl/custom_tag_attribute.tld</taglib-uri>
            <taglib-location>/WEB-INF/jstl/custom_tag_attribute.tld</taglib-location>
        </taglib>
        <taglib>
</web-app>

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question