在 web.xml 中声明 JSP taglib 指令
我似乎记得读过可以
<%@ 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
web.xml 中的
taglib
元素与上面的taglib
指令具有不同的用途。正如 David 所说,每个页面都需要
taglib
指令。如果您有许多页面使用通用标记库,则可以通过将标记库指令放入包含文件中并在每个页面中包含该文件来简化此操作。 但无论你怎么做,taglib 指令都必须以某种方式出现在页面上。
您需要在每个页面上包含的标记如下所示:
如果您在自定义位置有自定义标记库,您还可以指定相对于 web 应用程序根目录的位置:
进一步阅读 taglib 指令
web.xml 中的
taglib
指令将标签 uri 映射到物理位置你的标签库。 从 JSP 2.0 开始,它是可选的,因为兼容容器将在一组标准位置中查找以尝试自动发现 taglib:/WEB-INF 及其子目录、/META-INF 以及 JAR 文件。在 web.xml 中,它看起来像这样:
并且 taglib 在 JSP 页面中被引用,如下所示(每个页面上的 taglib 指令是不可避免的!):
这相当于我为上面的 taglib 指令给出的第二个示例。 最大的区别在于如何指向 taglib 位置。
此页面包含更多信息。
The
taglib
element in web.xml serves a different purpose to thetaglib
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:
If you have a custom taglib in a custom location, you can also specify a location relative to the webapp root:
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:
And the taglib is referenced in the JSP page like this (the taglib directive on each page is unavoidable!):
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.
抱歉,你有点误会了。 如果页面使用 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.