OSGi中的JSP:如何从bundle加载TLD?

Bas*_*sil 7 java jsp osgi taglib

我们正在构建一个JSP Web应用程序,它在Apache Felix OSGi容器内运行(Web应用程序本身是一个OSGi Bundle).现在,我们面临以下问题:

根据JSP 2.0规范,TLD(taglib描述符)不再需要驻留在Web应用程序WEB-INF文件夹中,而是直接从taglib的jar META-INF文件夹加载.这个taglib jar通常驻留在web应用程序WEB-INF/lib文件夹中,但因为它们是OSGi包,所以它们由Felix加载.

在taglib的OSGi信息中,我们确实导入了所有需要的包.那里的任何人都知道如何告诉servlet,在加载的OSGi Bundles中搜索TLD?

谢谢你的帮助!

Han*_*etz 3

如果您的 TLD 与您的网络应用程序位于不同的捆绑包中,Pax 将找不到您的 TLD :

标签库

为了让您的自定义标签库正常工作,您的 TLD 文件必须可以在捆绑包中的“特殊”位置访问:

  • Bundle-ClassPath 清单条目引用的任何 jar 中的所有 tld 文件
  • 捆绑包 jar 中 WEB-INF 目录或 WEB-INF 子目录中的所有 tld 文件

请注意,您导入的包将不会被搜索(可能稍后会添加此支持)

我在基于 Struts 的系统中遇到了这个问题,我在多个 webapp 包之间共享 OSGi-fied Struts 包。Web 应用程序具有需要 Struts 标记库的 JSP。

一个稍微有点hackish(因为它会到处复制 TLD)的解决方法是将 TLD 放入您的 web 应用程序的META-INF目录中,并使 web 应用程序包导入所需的 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>
Run Code Online (Sandbox Code Playgroud)