简体   繁体   English

在web.xml中声明JSP taglib指令

[英]declare JSP taglib directives in web.xml

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. 在web.xml中。 This eliminates the need to duplicate this directive in every JSP file where the taglib is used. 这消除了在使用taglib的每个JSP文件中复制此指令的需要。 Could someone tell me how these directives can be added to web.xml? 有人能告诉我如何将这些指令添加到web.xml中吗?

The taglib element in web.xml serves a different purpose to the taglib directive which you have above. web.xml中的taglib元素与上面的taglib指令有不同的用途。

As David said, the taglib directive is required on each page. 正如David所说,每页都需要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. 如果您有许多使用常见标记库的页面,可以通过将taglib指令放入包含文件并在每个页面中包含此文件来快捷方式。 But no matter how you do it, the taglib directive has to be on the page somehow. 但无论你如何做,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: 如果您在自定义位置具有自定义标记库,则还可以指定相对于Web应用程序根目录的位置:

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

Further reading on the taglib directive 进一步阅读taglib指令

The taglib directive from web.xml maps tag uris to the physical location of your taglib. web.xml中的taglib指令将标记uris映射到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. 它是自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. 这相当于我为上面的taglib指令提供的第二个例子。 The biggest difference is in how you point to the taglib location. 最大的区别在于你如何指向taglib位置。

This page contains a bit more information. 此页面包含更多信息。

Sorry, you're slightly mistaken. 对不起,你有点误会。 If a page uses a taglib, you have to have a taglib directive for it on the page. 如果页面使用taglib,则必须在页面上为其指定taglib指令。 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指令放在include文件中,所有页面都包含include指令,但在编译时,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值。 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM