java 子目录中的 JSP 标记文件,使用单个 taglib 前缀。那可能吗?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10592434/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 01:43:59  来源:igfitidea点击:

JSP Tag Files in subdirectories, using a single taglib prefix. Is that possible?

javajspjsp-tagstaglib

提问by electrotype

I currently have my .tag files declared with:

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

<%@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 singlefolder, "/WEB-INF/tags".

我的问题:我不想将所有标签文件放在一个文件夹中,“/WEB-INF/tags”。

I would prefere to have them in different subdirectories :

我更喜欢将它们放在不同的子目录中:

/WEB-INF/tags/users/

/WEB-INF/标签/用户/

/WEB-INF/tags/widgetsA/

/WEB-INF/标签/小部件A/

/WEB-INF/tags/widgetsB/

/WEB-INF/标签/小部件B/

(...)

(...)

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

这是可能的,而无需为每个人创建不同的 taglib 前缀吗?

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”前缀:

<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 anywhereexcept in the element that use them (ex: "<t:users/onetag")!

更新:BalusC 表明通过在单个 .tld 中定义所有标签文件,可以只使用一个前缀。我想我的问题还不够清楚:我想知道是否可以在多个子目录中使用标记文件,而不必在使用它们的元素之外的任何地方指定每个标记文件的路径(例如:“ <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/!!

我不喜欢 JSP 标签的一点是它们的行为与常规 JSP 文件非常不同,即使它们实际上共享非常相似的内容。事实上,我什至决定将我所有的 jsp 文件放在 /WEB-INF/tags/ 文件夹中,所以它们与标签文件并排(我不得不为此选择 /WEB-INF/tags/,因为这出于某种原因,文件夹对于标记文件是必需的)!我不明白为什么我的一些包含 HTML 的文件会在 /WEB-INF/jsp/ 中,而另一些会在 /WEB-INF/tags/ 中!

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

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

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

但这迫使我在多个@tablib 或 .tld 中声明每个子目录的路径,我觉得这有点不方便。我会忍受它,但我认为它可以改进。

回答by BalusC

Define them as <tag-file>in a single .tldfile which you put in /WEB-INFfolder.

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

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

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

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

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

回答by Helder Pereira

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:

我遵循的一种模式,尽管没有直接解决 OP 的问题,但我发现它使整个情况变得不那么痛苦,它创建了一个 JSP 片段,我在其中定义了所有标签库:

/WEB-INF/views/taglibs.jspf

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

然后在每个 JSP 文件的顶部包含这个 JSP 片段:

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

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

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

<layout:main>
    <h1>Employee Profile</h1>
    ...
</layout:main>

回答by nitind

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

应该管用。指定 tag-dir 值下的文件夹名称成为您将使用的标签名称的连字符分隔部分。