Java 在 web.xml 中声明 JSP taglib 指令
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/226514/
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
declare JSP taglib directives in web.xml
提问by Dónal
I seem to remember reading that it's possible to declare taglib directives such as:
我似乎记得读到可以声明 taglib 指令,例如:
<%@ 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 howthese directives can be added to web.xml?
在 web.xml 中。这消除了在使用 taglib 的每个 JSP 文件中复制该指令的需要。有人能告诉我如何将这些指令添加到 web.xml 吗?
采纳答案by Athena
The taglib
element in web.xml serves a different purpose to the taglib
directive which you have above.
taglib
web.xml 中的元素与taglib
您上面的指令有不同的用途。
As David said, the taglib
directive is required on each page.
正如大卫所说,taglib
每页都需要该指令。
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.
如果您有许多使用公共 taglib 的页面,您可以通过将 taglib 指令放入包含文件并在每个页面中包含此文件来快捷方式。但是无论你怎么做,taglib 指令都必须以某种方式出现在页面上。
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,您还可以指定相对于 webapp 根的位置:
<%@ 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.
taglib
web.xml 中的指令将标记 uri 映射到标记库的物理位置。从 JSP 2.0 开始它是可选的,因为兼容容器将在一组标准位置中查找以尝试自动发现 taglib:/WEB-INF 及其子目录,/META-INF 以及 JAR 文件。
It looks like this, in web.xml:
它看起来像这样,在 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!):
并且在 JSP 页面中像这样引用 taglib(每个页面上的 taglib 指令是不可避免的!):
<%@ 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.
这相当于我为上面的 taglib 指令给出的第二个例子。最大的区别在于您如何指向 taglib 位置。
This pagecontains a bit more information.
此页面包含更多信息。
回答by David M. Karr
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.
对不起,你有点误会了。如果页面使用 taglib,则必须在页面上为其设置 taglib 指令。您可以将常见的 taglib 指令放在一个包含文件中,您的所有页面都包含一个包含指令,但在编译时 taglib 指令必须在那里。
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.
我更喜欢在 web.xml 中不包含 taglib 元素,而是让 taglib 指令指定在 WEB-INF/lib 中 taglib jar 文件内的 TLD 中的“uri”元素中使用的 URI 值.