0

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

Впервые решил опробовать 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

Если же обратиться таким образом, http://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

Каким образом возможно исключить эту ошибку?

1 ответ 1

3

Вероятно, надо сконфигурировать путь до кастомного тега в web.xml приложения.

<%taglib uri="myTLD" prefix="mytaglib" %>
<taglib>
  <taglib-uri>myTLD</taglib-uri>
  <taglib-location>library.tld</taglib-location>
</taglib>

https://docs.oracle.com/cd/E11035_01/wls100/taglib/quickstart.html

Ваш ответ

Нажимая на кнопку «Отправить ответ», вы соглашаетесь с нашими условиями использования и подтверждаете, что прочитали и поняли наши политику конфиденциальности и нормы поведения.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.