7

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

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

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

谢谢你的帮助!

4

2 回答 2

3

如果您的 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>
于 2010-10-07T14:36:48.227 回答
0

一般来说,很难集成 OSGi 和 Java EE 库。来自 Nuxeo CMS 的人设法集成了 Seam Framework 和 OSGi,所以我认为使用他们的想法,您也可以更轻松地集成 JSP TLD 和 OSGi。只需下载 Nuxeo 并分析其源代码:http ://www.nuxeo.org/xwiki/bin/view/Main/

然而,集成 OSGi 和 Java EE 通常很难。你真的需要 OSGi 运行时集成吗?也许 Maven 编译时集成对您来说就足够了?许多人只是将 Maven 和类似工具视为编译时 OSGi。

于 2010-10-11T18:15:44.820 回答