在 web.xml 中声明 JSP taglib 指令

发布于 2024-07-07 22:06:07 字数 217 浏览 5 评论 0原文

我似乎记得读过可以

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

在 web.xml 中 声明 taglib 指令,例如: 这样就无需在使用 taglib 的每个 JSP 文件中重复此指令。 有人可以告诉我如何将这些指令添加到 web.xml 中吗?

I seem to remember reading that it's possible to declare taglib directives such as:

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

in web.xml. This eliminates the need to duplicate this directive in every JSP file where the taglib is used. Could someone tell me how these directives can be added to web.xml?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一萌ing 2024-07-14 22:06:07

web.xml 中的 taglib 元素与上面的 taglib 指令具有不同的用途。

正如 David 所说,每个页面都需要 taglib 指令。

如果您有许多页面使用通用标记库,则可以通过将标记库指令放入包含文件中并在每个页面中包含该文件来简化此操作。 但无论你怎么做,taglib 指令都必须以某种方式出现在页面上。

您需要在每个页面上包含的标记如下所示:

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

如果您在自定义位置有自定义标记库,您还可以指定相对于 web 应用程序根目录的位置:

 <%@ taglib prefix="ex" uri="/taglib.tld" %>

进一步阅读 taglib 指令

web.xml 中的 taglib 指令将标签 uri 映射到物理位置你的标签库。 从 JSP 2.0 开始,它是可选的,因为兼容容器将在一组标准位置中查找以尝试自动发现 taglib:/WEB-INF 及其子目录、/META-INF 以及 JAR 文件。

在 web.xml 中,它看起来像这样:

<taglib>
  <taglib-uri>
    http://www.example.com/taglib
  </taglib-uri>
  <taglib-location>
    /taglib.tld
  </taglib-location>
</taglib>

并且 taglib 在 JSP 页面中被引用,如下所示(每个页面上的 taglib 指令是不可避免的!):

<%@ taglib prefix="ex" uri="http://www.example.com/taglib" %>

这相当于我为上面的 taglib 指令给出的第二个示例。 最大的区别在于如何指向 taglib 位置。

此页面包含更多信息。

The taglib element in web.xml serves a different purpose to the taglib directive which you have above.

As David said, the taglib directive is required on each page.

If you have many pages which use common taglibs, you can shortcut this by putting the taglib directives into an include file, and including this file each page. But no matter how you do it, the taglib directive has to be on the page somehow.

That tag you need to include on each page looks like this:

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

If you have a custom taglib in a custom location, you can also specify a location relative to the webapp root:

 <%@ taglib prefix="ex" uri="/taglib.tld" %>

Further reading on the taglib directive

The taglib directive from web.xml maps tag uris to the physical location of your taglib. It is optional since JSP 2.0, as compliant containers will look in a set of standard locations to try to auto-discover the taglib: /WEB-INF and its subdirectories, /META-INF as well for JAR files.

It looks like this, in web.xml:

<taglib>
  <taglib-uri>
    http://www.example.com/taglib
  </taglib-uri>
  <taglib-location>
    /taglib.tld
  </taglib-location>
</taglib>

And the taglib is referenced in the JSP page like this (the taglib directive on each page is unavoidable!):

<%@ taglib prefix="ex" uri="http://www.example.com/taglib" %>

This is equivalent to the second example I gave for the taglib directive above. The biggest difference is in how you point to the taglib location.

This page contains a bit more information.

吹梦到西洲 2024-07-14 22:06:07

抱歉,你有点误会了。 如果页面使用 taglib,则页面上必须有一个 taglib 指令。 您可以将常见的 taglib 指令放在所有页面都包含的包含文件中,但在编译时 taglib 指令必须存在。

我不想在 web.xml 中包含 taglib 元素,而是让 taglib 指令指定 WEB-INF/lib 中 taglib jar 文件内 TLD 中“uri”元素中使用的 URI 值。

Sorry, you're slightly mistaken. If a page uses a taglib, you have to have a taglib directive for it on the page. You could place the common taglib directives in an include file that all of your pages include with an include directive, but at compile time the taglib directive has to be there.

I prefer to NOT have the taglib elements in the web.xml, and instead have the taglib directive specify the URI value that is used in the "uri" element in the TLD that is inside the taglib jar file in your WEB-INF/lib.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文