在web.xml中声明JSP taglib指令 [英] declare JSP taglib directives in web.xml

查看:108
本文介绍了在web.xml中声明JSP taglib指令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎记得读过,有可能声明taglib指令,例如:

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" %>

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

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.xml中的taglib元素与上面的taglib指令具有不同的用途.

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

正如David所说,每页上都需要taglib指令.

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

如果有许多使用通用taglib的页面,则可以通过将taglib指令放入包含文件,并在每个页面中包含此文件来简化此操作.但是无论您如何执行,都必须以某种方式在页面上放置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.

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

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

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

如果在自定义位置具有自定义taglib,则还可以指定相对于Web应用程序根目录的位置:

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" %>

进一步阅读taglib指令

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

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.

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

It looks like this, in web.xml:

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

这样在JSP页面中引用了taglib(不可避免地在每个页面上使用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" %>

这等效于我为上面的taglib指令提供的第二个示例.最大的区别在于您如何指向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.

此页面包含更多信息.

这篇关于在web.xml中声明JSP taglib指令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆