如果您的 TLD 与您的 webapp 不同,Pax 将找不到它们:
标签库
为了让您的自定义标签库正常工作,您的 TLD 文件必须可以在您的捆绑包中的“特殊”位置访问:
- Bundle-ClassPath 清单条目引用的任何 jar 中的所有 tld 文件
- bundle jar 中 WEB-INF 目录或 WEB-INF 子目录中的所有 tld 文件
请注意,不会搜索您导入的包(可能稍后会添加此支持)
我在一个基于 Struts 的系统中遇到了这个问题,我在多个 webapp 包之间共享一个 OSGi-fied Struts 包。webapps 有需要Struts taglib 的JSP。
一个有点骇人听闻的方法(因为它会到处复制 TLD)解决方法是将 TLD 放在 webapp 的META-INF
目录中,并使 webapp 包导入所需的 Struts 包(或者,如果您不使用 Struts,则任何类处理标签)。这可以使用 Maven 自动执行,如下所示:
<plugin>
<!--
Extract the TLD file from the Struts bundle you are using
and place it in src/main/resources/META-INF of your webapp's
project directory during generate-resources. This will make
the file end up in the appropriate place in the resulting WAR
-->
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<executions>
<execution>
<id>extract-tld</id>
<phase>generate-resources</phase>
<goals>
<goal>unpack</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>org.apache.struts</groupId>
<artifactId>struts2-core</artifactId>
<version>${struts.version}</version>
<outputDirectory>src/main/resources</outputDirectory>
<includes>META-INF/struts-tags.tld</includes>
</artifactItem>
</artifactItems>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<!--
Add the required Manifest headers using the maven-bundle-plugin
-->
<groupId>org.apache.felix</groupId>
<artifactId>maven-bundle-plugin</artifactId>
<configuration>
<!-- ... -->
<instructions>
<!-- ... -->
<Import-Package>[...],org.apache.struts2.views.jsp</Import-Package>
<!-- ... -->
</instructions>
</configuration>
</plugin>