taglib标签库
近来,在适配一些老旧的应用中,经常遇到jsp配置标签库一些问题,特写下此篇文章来记录
要了解tld文件在扫描中的逻辑,就需要阅读tomcat源码了
在TldScanner.java中找到了一些逻辑说明
首先扫描web.xml的jsp-config中的配置,再扫描WEB-INF下面的tld资源文件
public void scan() throws IOException, SAXException {
scanPlatform();
scanJspConfig(); //扫描web.xml的jsp-config
scanResourcePaths(WEB_INF); //扫描WEB-INF下面的tld资源文件
scanJars();
}
protected void parseTld(String resourcePath) throws IOException, SAXException {
TldResourcePath tldResourcePath =
new TldResourcePath(context.getResource(resourcePath), resourcePath);
parseTld(tldResourcePath);
}
protected void parseTld(TldResourcePath path) throws IOException, SAXException {
TaglibXml tld = tldParser.parse(path);
String uri = tld.getUri();
if (uri != null) {
if (!uriTldResourcePathMap.containsKey(uri)) {
uriTldResourcePathMap.put(uri, path);
}
}
if (tldResourcePathTaglibXmlMap.containsKey(path)) {
// TLD has already been parsed as a result of processing web.xml
return;
}
tldResourcePathTaglibXmlMap.put(path, tld);
if (tld.getListeners() != null) {
listeners.addAll(tld.getListeners());
}
}
比如jsp的简单配置如下:
<%@ taglib prefix="test" uri="/myweb" %>
web.xml的简单配置说明如下:
<jsp-config>
<taglib>
<taglib-uri>/myweb</taglib-uri>
<taglib-location>/WEB-INF/my.tld</taglib-location>
</taglib>
</jsp-config>
my.tld简单配置如下:
<taglib>
<uri>/myweb</uri>
tomcat会先将web.xml和tld文件<taglib>标签中中uri存进uriTldResourcePathMap中作为key,最后jsp查找uri就是对其map中的key进行匹配,最终匹配到正确的tld
那按这个逻辑jsp、web.xml、tld文件可以这样相对应了
1、直接在jsp引入具体的tld,但是jar包中或者WEB-INF的tld文件得存在
jsp配置:<%@ taglib prefix="test" uri="/WEB-INF/my.tld" %>
2、不配置web.xml,只在jsp和tld文件定义uri
jsp配置:<%@ taglib prefix="test" uri="/myweb" %>
my.tld配置:<taglib><uri>/myweb</uri>
3、不配置tld的uri,只在jsp和web.xml文件定义uri
jsp配置:<%@ taglib prefix="test" uri="/myweb" %>
web.xml配置:
<jsp-config>
<taglib>
<taglib-uri>/myweb</taglib-uri>
<taglib-location>/WEB-INF/my.tld</taglib-location>
</taglib>
</jsp-config>
4、还有一种网络查找的方式<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
学习链接
https://blog.csdn.net/afandaafandaafanda/article/details/50583278
http://www.itpub.net/thread-716189-1-1.html
使用use.tag,可以参考如下链接
https://blog.csdn.net/qq_35076190/article/details/88425620?spm=1001.2014.3001.5501
浙公网安备 33010602011771号