子目录中的JSP标记文件,使用单个taglib前缀.那可能吗? [英] JSP Tag Files in subdirectories, using a single taglib prefix. Is that possible?

查看:83
本文介绍了子目录中的JSP标记文件,使用单个taglib前缀.那可能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前的.tag文件声明为:

I currently have my .tag files declared with:

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

标记文件路径的示例:

/WEB-INF/tags/test.tag

我像这样使用它们:

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

我的问题:我不想将我的所有标记文件放在一个 文件夹中,即"/WEB-INF/tags".

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/users/

/WEB-INF/tags/widgetsA/

/WEB-INF/tags/widgetsA/

/WEB-INF/tags/widgetsB/

/WEB-INF/tags/widgetsB/

(...)

是否为每个人都创建一个不同的taglib前缀,这是否可能?

我想避免的例子:

<%@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" %>

使用单个"t"前缀的示例:

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?

更新:BalusC显示通过在单个.tld中定义所有标记文件,可以仅使用一个前缀.我想我的问题当时还不够清楚:我想知道是否可以在多个子目录中使用标记文件,而不必为元素中的每个 anywhere 指定路径,而在元素中除外会使用它们(例如:< t:users/onetag")!

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")!

我对JSP标签不满意的是,即使它们实际上共享非常相似的内容,它们的行为也与常规JSP文件完全不同.实际上,我什至决定将所有jsp文件都放在/WEB-INF/tags/文件夹中,因此它们与标记文件并排放置(为此,我必须选择/WEB-INF/tags/,因为出于某些原因,文件夹对于标签文件是必填的!我不明白为什么我的某些包含HTML的文件会放在/WEB-INF/jsp/中,而另一些文件会放在/WEB-INF/tags/中!!

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/!!

我希望能够将jsp和标记文件分组到目录中,具体取决于它们的相关性!例子:

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

但这迫使我在多个@tablib或.tld中声明每个子目录的路径,这给我带来了一些不便.我会接受,但我认为它可以改进.

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.

推荐答案

在放入/WEB-INF文件夹的单个.tld文件中,将它们定义为<tag-file>.

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

例如/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>

按如下所示在您的JSP中使用它

Use it in your JSPs as follows

<%@taglib prefix="my" uri="http://example.com/tags" %>
...
<my:foo />
<my:bar />
<my:baz />

这篇关于子目录中的JSP标记文件,使用单个taglib前缀.那可能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆