【问题标题】:Glassfish 4.1.1 and offline JSP compiler (JSP and OSGI)Glassfish 4.1.1 和离线 JSP 编译器(JSP 和 OSGI)
【发布时间】:2016-11-03 11:54:43
【问题描述】:

众所周知,jsp 不能与当前 osgi web 归档包之外的类一起使用。这是GF中的一个错误。 glassfish 的开发人员为解决此错误https://java.net/jira/browse/GLASSFISH-11208 提供了使用离线 jsp 编译器(换句话说,不是在部署期间而是在存档构建期间编译 jsp 文件)。好的,我在 wab 构建期间使用 jspc-maven-plugin 来编译我的 jsp。

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jspc-maven-plugin</artifactId>
 <version>1.4.6</version>
 <executions>
     <execution>
         <goals>
             <goal>compile</goal>
         </goals>
         <id>compile</id>
     </execution>
 </executions>
 <configuration>
 </configuration>
</plugin>

jsp 已编译,我在构建的 web 存档中看到了它们的 .classes。

现在的问题 - 我怎样才能让 glassfish 使用我编译的 jsp 而不是自己编译它?因为我看到 GF 会忽略已编译的 .classes 并生成 .javas 并自行编译。

编辑 1 我现在所做的: 1)我添加到 glassfish-web.xml

  <jsp-config>
        <property name="usePrecompiled" value="true"/>
        <!-- to see it doesn't generate .javas -->
        <property name="keepgenerated" value="true" />
    </jsp-config>

2)当我构建我的 wab 存档时,我在 WEB-INF/classes/jsp/... 中有 jsp 类但是,我得到了找不到 jsp 文件的异常。当我 manually 将 jsp 类移动到 WEB-INF/classes/org/apache/jsp... 时,我看到容器现在可以看到这些类,但我得到了

  StandardWrapperValve[default]: Servlet.service() for servlet default threw exception
java.lang.NoClassDefFoundError: org/apache/jsp/... (wrong name: jsp/...)
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:760)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.defineClass(BundleWiringImpl.java:2370)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.findClass(BundleWiringImpl.java:2154)
    at org.apache.felix.framework.BundleWiringImpl.findClassOrResourceByDelegation(BundleWiringImpl.java:1542)
    at org.apache.felix.framework.BundleWiringImpl.access$400(BundleWiringImpl.java:79)
    at org.apache.felix.framework.BundleWiringImpl$BundleClassLoader.loadClass(BundleWiringImpl.java:2018)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at org.apache.felix.framework.Felix.loadBundleClass(Felix.java:1925)
    at org.apache.felix.framework.BundleImpl.loadClass(BundleImpl.java:978)
    at org.glassfish.osgijavaeebase.BundleClassLoader.loadClass(BundleClassLoader.java:79)
    at org.glassfish.osgiweb.OSGiWebDeploymentContext$WABClassLoader.loadClass(OSGiWebDeploymentContext.java:169)
    at org.glassfish.osgiweb.OSGiWebDeploymentContext$WABClassLoader.loadClass(OSGiWebDeploymentContext.java:154)
    at org.apache.jasper.JspCompilationContext.load(JspCompilationContext.java:654)
    at org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:202)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:388)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:473)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:377)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682)
    at org.apache.catalina.core.ApplicationDispatcher.doInvoke(ApplicationDispatcher.java:875)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:739)
    at org.apache.catalina.core.ApplicationDispatcher.doInclude(ApplicationDispatcher.java:695)
    at org.apache.catalina.core.ApplicationDispatcher.include(ApplicationDispatcher.java:626)

所以知道这是正确的路径 - org/apache/jsp。问题是如何让maven插件输出到这个方向?

编辑 2 所以我找到了这个 maven 插件的设置 -

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jspc-maven-plugin</artifactId>
 <version>1.4.6</version>
 <executions>
     <execution>
         <goals>
             <goal>compile</goal>
         </goals>
         <id>compile</id>
         <configuration>
             <packageName>org.apache.jsp</packageName>
         </configuration>
     </execution>
 </executions>
 <configuration>
 </configuration>
</plugin>

但是,这是最后一点,而不是结果。我也不例外,但是返回的 http 请求是空的(浏览器中的空白页面)。看来我应该使用另一个 maven 插件,但哪个?

【问题讨论】:

  • 明确地说,您是在构建 WAB 应用程序而不是 WAR 应用程序,对吗?
  • @Steve C 请看我的编辑。
  • 如果您不一定要使用 Glassfish 作为容器,Apache Karaf 提供了一个替代方案作为纯 OSGi 容器。与 Pax-Web 一起,它还支持 JSP
  • @Achim Nierbeck 但是没有 Pax-Web 没有 Apache Karaf 都不是完整的 JavaEE 服务器。
  • 您缺少哪一部分?当然不是 EJB,尽管您可以使用那些具有开放 EJB (tomEE) 的 EJB

标签: java jsp jakarta-ee glassfish osgi


【解决方案1】:

因此,对于我在编辑中所做和解释的所有步骤,有必要修改 web.xml 文件,因为插件将为从 jsp 页面生成的 servlet 添加映射。所以,最终的设置是:

<plugin>
 <groupId>org.codehaus.mojo</groupId>
 <artifactId>jspc-maven-plugin</artifactId>
 <version>1.4.6</version>
 <executions>
     <execution>
         <goals>
             <goal>compile</goal>
         </goals>
         <id>compile</id>
         <configuration>
             <!-- package where the compiled jsp classes will be put -->
             <packageName>org.apache.jsp</packageName>
             <!-- the plugin adds servlets to this web.xml file -->
<outputWebXml>${project.build.directory}/web.xml</outputWebXml>
          <verbose>true</verbose>
         <target>8</target>
         <source>8</source>
         </configuration>
     </execution>
 </executions>
 <configuration>
 </configuration>
</plugin>

编辑 最后我发现 GlassFish 4.1 中 jasper 的版本是未知的,甚至可以修改 -> 我遇到了找不到这种方法的异常等。所以我以以下内容结束 - 我下载了这个插件的源代码并让它使用glassfish 中碧玉的版本。我没有对插件的源代码做任何修改,只在 pom.xml 中。所以最后的pom变成了:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <!--<parent>
    <artifactId>mojo</artifactId>
    <groupId>org.codehaus.mojo</groupId>
    <version>11</version>
  </parent>-->
    <modelVersion>4.0.0</modelVersion>
      <groupId>org.codehaus.mojo</groupId>
    <artifactId>jspc-maven-plugin</artifactId>
    <version>1.4.6</version>
    <packaging>maven-plugin</packaging>
    <name>Maven Jspc plugin</name>
    <developers>
        <developer>
            <name>Jeff Genender</name>
            <email>jgenender@apache.org</email>
            <organization>Savoir Technologies</organization>
            <organizationUrl>http://www.savoirtech.com</organizationUrl>
            <timezone>-7</timezone>
        </developer>
    </developers>
    <contributors>
        <contributor>
            <name>Grzegorz Slowikowski</name>
            <email>gs@tiger.com.pl</email>
            <organization>Scott Tiger S.A.</organization>
            <organizationUrl>http://www.tiger.com.pl</organizationUrl>
            <timezone>+1</timezone>
        </contributor>
        <contributor>
            <name>Pawel Pastula</name>
            <email>pablo@tiger.com.pl</email>
            <organization>Scott Tiger S.A.</organization>
            <organizationUrl>http://www.tiger.com.pl</organizationUrl>
            <timezone>+1</timezone>
        </contributor>
    </contributors>

    <dependencies>
        <!-- from glassfish 4.1.1 modules folder we need:
        javax.servlet.jsp.jar
        javax.servlet-api.jar
        javax.servlet.jsp-api.jar
        javax.el.jar
        javax.servlet.jsp.jstl-api.jar
        javax.servlet.jsp.jstl.jar
        what versions of this jar you can find out in parent pom of glassfish
        http://repo.maven.apache.org/maven2/org/glassfish/main/glassfish-parent/4.1.1/glassfish-parent-4.1.1.pom
        and in manifest file
        -->
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>javax.servlet-api</artifactId>
            <version>3.1.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp</groupId>
            <artifactId>javax.servlet.jsp-api</artifactId>
            <version>2.3.2-b01</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.servlet.jsp</artifactId>
            <version>2.3.3-b02</version>
        </dependency>        
        <dependency>
            <groupId>org.glassfish</groupId>
            <artifactId>javax.el</artifactId>
            <version>3.0.0</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet.jsp.jstl</groupId>
            <artifactId>javax.servlet.jsp.jstl-api</artifactId>
            <version>1.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.glassfish.web</groupId>
            <artifactId>javax.servlet.jsp.jstl</artifactId>
            <version>1.2.4</version>
        </dependency>
        <!-- we need this dependency as it contais tld files for core tag library -->
        <dependency>
            <groupId>org.eclipse.jetty.orbit</groupId>
            <artifactId>org.apache.jasper.glassfish</artifactId>
            <version>2.2.2.v201112011158</version>
        </dependency>
        <dependency>
            <groupId>ant</groupId>
            <artifactId>ant</artifactId>
            <version>1.6.5</version>
        </dependency>        
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-plugin-api</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.maven</groupId>
            <artifactId>maven-project</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.13</version>
            <scope>runtime</scope>
        </dependency>        
    </dependencies>
</project>

当你编译你的 bundle 时,你必须添加以下依赖项:

<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp</artifactId>
    <version>2.3.3-b02</version>
</dependency>       
<dependency>
    <groupId>org.glassfish.web</groupId>
    <artifactId>javax.servlet.jsp.jstl</artifactId>
    <version>1.2.4</version>
</dependency>

此外,您还需要从 glassfish 导入一些包以使其工作。因此,您可以将预编译的 jps 文件与 glassfish 一起使用,但您需要在它之前做一些事情。如您所见,您将代码链接到 GF。

最重要的是——您可以在 jsp 中使用来自其他 osgi 包的类!对于那些在 java-ee 中使用 osgi 的人来说,这可能非常重要。完成所有这些步骤后,我必须得出结论,GF IS NOT SUPPORTED TO BE USED WITH PRECOMPILED JPS FILES 尽管有开发人员的建议。

我希望至少有人会欣赏所有的解决方案,因为在我看来,这是互联网上第一次描述如何使用 GF 的预编译 jps 页面。顺便说一句,如果您使用 osgi,它会抱怨找不到导入必要包的类。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多