drno-reg
@drno-reg
см не кратко

Почему при вызове custom tag возникает ошибка Unable to find taglib?

Здравствуйте.

Впервые решил опробовать custon taglib

java класс

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 расположен в 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>


вызов кастомного taglib в JSP

examples.jsp раcполагается в корневой папке проекта newproject/examples.jsp

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

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

Выполнить кодКопировать код в ответРазвернуть фрагмент
Все бы хорошо, НО данный вариант работает, если проект работает в корневой папке к примеру localhost:8000/examples.jsp

Если же обратиться таким образом, localhost/newproject/examples.jsp то возникает ошибка

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

Каким образом возможно исключить эту ошибку?
  • Вопрос задан
  • 374 просмотра
Решения вопроса 1
drno-reg
@drno-reg Автор вопроса
см не кратко
совсем забыл про 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>
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы