Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jsp

jsp-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?

like image 226
Shajirr Avatar asked Dec 16 '22 23:12

Shajirr


2 Answers

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).

like image 68
alexey28 Avatar answered May 10 '23 13:05

alexey28


You need to put the prefix "urn:jsptagdir:" in your xmlns attribute. In your case,

<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="urn:jsptagdir:/WEB-INF/tags" 
    xmlns="http://www.w3.org/1999/xhtml"
    version="2.0">

You can also use the prefix "urn:jsptld:" to specify the location of a TLD. For more details, see the section on "Declaring Tag Libraries" in http://docs.oracle.com/javaee/1.4/tutorial/doc/JSPX3.html.

like image 20
Ryan Doherty Avatar answered May 10 '23 13:05

Ryan Doherty