JSP 2.0 – using tag files in a JSP document without .tld declaration

jspjsp-tags

I want to use something like this in my jsp document files:

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

However, in all the 30+ examples I've seen, everyone uses simple jsp syntax, not jsp document syntax. Something like this:

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
    xmlns:fmt="http://java.sun.com/jsp/jstl/fmt"
    xmlns:t="/WEB-INF/tags" 
    xmlns="http://www.w3.org/1999/xhtml"
    version="2.0">

simply does not work. All the tag files which reside in /WEB-INF/tags aren't seen on the page. Only if I define a tld file, and list all the tags there, they can be accessed on the page. Is it possible to avoid tld declaration and still use tag files in a jsp document page?

Best Answer

1 Create tags directory in your WEB-INF/ directory

2 Create sample.tag file where your tag will be:

<%@ attribute name="exampleAttribute" required="true" type="java.lang.String" description="Example attribute" %>

<c:out value="${exampleAttribute}">

3 Declare tag library at you jsp where you want to use it:

<%@ taglib prefix="tags" tagdir="/WEB-INF/tags" %>

4 Use it:

<tags:sample exampleAttribute="Hello from custom tag!"/>

And I think you should have version of web application 2.5. Think only from that version JSP 2.0 is supported (check in web.xml).