12

I currently have my .tag files declared with:

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

Example of the path of a tag file :

/WEB-INF/tags/test.tag

And I use them like so :

<t:test oneAttributeKey="oneAttributeValue">
   some content...
</t:test>

My problem : I don't want to put all my tag files in one single folder, "/WEB-INF/tags".

I would prefere to have them in different subdirectories :

/WEB-INF/tags/users/

/WEB-INF/tags/widgetsA/

/WEB-INF/tags/widgetsB/

(...)

Is this possible, without creating a different taglib prefix for each and everyone of them?

Example of what I'd like to avoid :

<%@taglib prefix="t_users" tagdir="/WEB-INF/tags/users" %>
<%@taglib prefix="t_widgetsA" tagdir="/WEB-INF/tags/widgetsA" %>
<%@taglib prefix="t_widgetsB" tagdir="/WEB-INF/tags/widgetsB" %>

Example of what I would like, using a single "t" prefix :

<t:users/onetag oneAttributeKey="oneAttributeValue">
   some content...
</t:users/onetag>

Does a similar solution exist?

UPDATE : BalusC showed it's possible to use one prefix only, by defining all the tag files in a single .tld. I guess my question was not clear enough then : I'd like to know if it's possible to use the tag files in multiple subdirectories, without having to specify a path to each of them anywhere except in the element that use them (ex: "<t:users/onetag")!

What I do not like about JSP Tags is that they act very differently than regular JSP files, even if they actually share very similar content. In fact, I even decided to put all my jsp files inside the /WEB-INF/tags/ folder, so they are side to side with the tag files (I had to choose /WEB-INF/tags/ for that, since this folder is mandatory for the tag files, for some reason)! I don't understand why some of my files containing HTML would go in /WEB-INF/jsp/ and some others in /WEB-INF/tags/!!

I want to be able to group the jsp and tag files into directories, depending of what they are related to! Example :

 /WEB-INF/tags/users/userProfileLayout.tag
 /WEB-INF/tags/users/employeeProfile.jsp
 /WEB-INF/tags/users/employerProfile.jsp

 /WEB-INF/tags/widgetsA/widgetALayout.tag
 /WEB-INF/tags/widgetsA/oldWidgetA.jsp
 /WEB-INF/tags/widgetsA/newWidgetA.jsp

But this forces me to declare the path of each of the subdirectories, in multiple @tablib or in a .tld, which I find a little bit inconvenient. I'll live with it, but I think it could be improved.

electrotype
  • 8,342
  • 11
  • 59
  • 96
  • I'm pretty sure such a thing is not possible, and it also seems to go against the grain of what's expected with taglibs. You want to be able to quickly find the tag file for a tag you're using, and if it could be in one of several directories, that's less than ideal. – roguenet May 15 '12 at 01:17
  • 1
    You don't understand why tag files would go in one directory and JSP files in another? Same reason you have packages, subdirectories, etc--separation of concerns. Same reason you have different prefixes for tags with different--to make them more distinct. Putting your JSP files in the taglib directory completely violates the principle of least surprise. – Dave Newton May 15 '12 at 12:17
  • Dave Newton, don't you agree that those tag files are pretty much jsp files with some few differences? Both have HTML. Both have view logic like . Both can use a parent tag file. My view logic is in both of them, so why would they be in different hierarchy? – electrotype May 15 '12 at 13:54

3 Answers3

18

Define them as <tag-file> in a single .tld file which you put in /WEB-INF folder.

E.g. /WEB-INF/my-tags.tld

<?xml version="1.0" encoding="UTF-8" ?>
<taglib 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1"
>    
    <display-name>My custom tags</display-name>    
    <tlib-version>1.0</tlib-version>
    <short-name>my</short-name>
    <uri>http://example.com/tags</uri>

    <tag-file>
        <name>foo</name>
        <path>/WEB-INF/tags/users/foo.tag</path>
    </tag-file>

    <tag-file>
        <name>bar</name>
        <path>/WEB-INF/tags/widgetsA/bar.tag</path>
    </tag-file>

    <tag-file>
        <name>baz</name>
        <path>/WEB-INF/tags/widgetsB/baz.tag</path>
    </tag-file>
</taglib>

Use it in your JSPs as follows

<%@taglib prefix="my" uri="http://example.com/tags" %>
...
<my:foo />
<my:bar />
<my:baz />
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • This answers the question I asked, but I realise my question was not clear enought. Thanks for the input though. I'll mark your answer as "accepted" if I do not receive answers to my edited question! – electrotype May 15 '12 at 10:53
0

A pattern that I follow, even though doesn't address the OP's problem directly, I find it makes the whole situation a lot less painful, which is creating a JSP Fragment where I define all taglibs:

/WEB-INF/views/taglibs.jspf

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<%@ taglib prefix="layout" tagdir="/WEB-INF/tags/layout" %>
<%@ taglib prefix="t_users" tagdir="/WEB-INF/tags/users" %>
<%@ taglib prefix="t_widgetsA" tagdir="/WEB-INF/tags/widgetsA" %>
<%@ taglib prefix="t_widgetsB" tagdir="/WEB-INF/tags/widgetsB" %>

And then include this JSP Fragment at the top of every JSP file:

/WEB-INF/views/users/employeeProfile.jsp

<%@ include file="/WEB-INF/views/taglibs.jspf" %>

<layout:main>
    <h1>Employee Profile</h1>
    ...
</layout:main>
Helder Pereira
  • 5,522
  • 2
  • 35
  • 52
-1

Should work. Folder names under the specified tag-dir value become hyphen-separated parts of the tag names you would use.

nitind
  • 19,089
  • 4
  • 34
  • 43