有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java嵌入式Jetty 9 PWC6188错误

和其他许多人一样,我也面临着PWC6188的错误

"http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application". This is a quite common problem, however, I didn't make it to find a suitable solution.

首先,一些背景知识。我们刚搬到Maven。从胖Tomcat web服务器切换到嵌入式Jetty 9 web服务器

下面,我已经阅读了这个非常有用的文档:https://stackoverflow.com/tags/jstl/info 关于此文档,以下内容可能有用

JSP-Taglib引用:

.<.%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%.>.

网络。xml标题

<web-app 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="YourWebAppID"
    version="2.5">

总之,我至少需要JSTL1.1和一个支持2.5Servlet规范的servletcontainer。 因此,带有JSTL 1.2的Jetty 9可能适合<;——真的吗

此外,我想谈谈webapp的构建和部署。 关于这个职位:

cannot load JSTL taglib within embedded Jetty server

我可能有一些依赖性问题。在这里,您可以看到pom中定义的所有依赖项。web应用程序的xml:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <!-- Parent container, which contains embedded Jetty 9 also uses log4j and provides the lib. -->
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
        <scope>provided</scope>
    </dependency>
    <!-- Some project related libs... -->
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts-core</artifactId>
        <version>${struts.version}</version>
    </dependency>
    <dependency>
        <groupId>org.apache.struts</groupId>
        <artifactId>struts-taglib</artifactId>
        <version>${struts.version}</version>
    </dependency>
    <dependency>
        <groupId>commons-fileupload</groupId>
        <artifactId>commons-fileupload</artifactId>
        <version>1.3.1</version>
    </dependency>
    <!-- Some more proprietary libs... -->
    <!-- ... -->

    <!-- Now it's getting interesting... -->
    <!-- Following dependencies are necessary to run 'mvn package'. 
        Scope is set to provided, so it's not transferred to resulting *.war. 
        Thus, containerserver must provide those.
    -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>${jetty.version}</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jsp-api</artifactId>
        <version>2.0</version>
        <scope>provided</scope>
    </dependency>
</dependencies>

运行“mvn jetty:run”效果很好。它打包war文件,其中包含以下LIB:

ls target/ROOT/WEB-INF/lib
    antlr-2.7.2.jar
    commons-beanutils-1.8.0.jar
    commons-chain-1.2.jar
    commons-digester-1.8.jar
    commons-fileupload-1.3.1.jar
    commons-io-2.2.jar
    commons-logging-1.0.4.jar
    commons-validator-1.3.1.jar
    oro-2.0.8.jar

对我来说,这看起来很好。所有与web应用相关的LIB都打包在war中。 因此,此war可能无法在TomcatWeb服务器中正常工作,因此web应用程序不提供JSTL标记库。但是,我将使用嵌入式Jetty 9。。。下面是pom中定义的所有依赖项。“我的服务器”容器中的xml:

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${lib.log4j.version}</version>
    </dependency>
    <!-- Some more proprietary libs... -->
    <!-- ... -->

    <!-- Project-related libs. -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.7</version>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <version>10.10.2.0</version>
    </dependency>
    <dependency>
        <groupId>org.apache.commons</groupId>
        <artifactId>commons-email</artifactId>
        <version>1.3.3</version>
    </dependency>

    <!-- Jetty Web-Server -->
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-webapp</artifactId>
        <version>${jetty.version}</version>
    </dependency>
    <dependency>
        <groupId>org.eclipse.jetty</groupId>
        <artifactId>jetty-jsp</artifactId>
        <version>${jetty.version}</version>
    </dependency>
</dependencies>

由于某些附带库的许可,我需要按原样提供所有JAR。也就是说,我可能不会将类提取并包含到生成的jar中。因此,我的maven程序集创建了以下结构:

*prefix*
    /bin
    myexecute.sh (calls java -jar ${prefix}/lib/java/main_app.jar)
    /lib/java
    *.jar (all the jar files)

prefix/lib/java列出了以下库:

ls
    activation-1.1.jar
    commons-email-1.3.3.jar
    derby-10.10.2.0.jar
    main_app.jar
    javax.el-3.0.0.jar
    javax.servlet-api-3.1.0.jar
    javax.servlet.jsp-2.3.2.jar
    javax.servlet.jsp-api-2.3.1.jar
    javax.servlet.jsp.jstl-1.2.0.v201105211821.jar
    javax.servlet.jsp.jstl-1.2.2.jar
    jetty-http-9.2.2.v20140723.jar
    jetty-io-9.2.2.v20140723.jar
    jetty-jsp-9.2.2.v20140723.jar
    jetty-schemas-3.1.M0.jar
    jetty-security-9.2.2.v20140723.jar
    jetty-server-9.2.2.v20140723.jar
    jetty-servlet-9.2.2.v20140723.jar
    jetty-util-9.2.2.v20140723.jar
    jetty-webapp-9.2.2.v20140723.jar
    jetty-xml-9.2.2.v20140723.jar
    log4j-1.2.17.jar
    mail-1.4.7.jar
    org.eclipse.jdt.core-3.8.2.v20130121.jar

主应用程序中的类路径。罐子看起来很好。列出了所有必要的java库,应用程序运行良好。但是,由于PWC6188,我无法访问web应用程序。 请注意,在使用eclipse运行应用程序时,没有PWC6188错误。顺便说一句,我使用“mvn eclipse:eclipse”设置了这个项目。 下面,关于这个线程

Jetty 9 The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved

只需要jettywebapp和jettyjsp工件,就可以用jsp、JSTL、Servlet等支持来设置jettycontainerserver

我还做了什么?我读了一些关于Jetty类加载的有用信息

http://www.eclipse.org/jetty/documentation/current/jetty-classloading.html It states, that libraries defined in WEB-INF folder has a higher priority than libs from it's container server. However, even if I switch priority, I'm facing PWC6188. As mentioned here

cannot load JSTL taglib within embedded Jetty server

设置类加载器可能是一个解决方案——我尝试过,但没有成功

最后,正如您可能注意到的,我做了很多尝试,并深入研究了所有这些JSTL库。然而,我无法摆脱PWC6188错误。我打赌这是件小事,但是,这也可能是Jetty 9的一个主要问题。 因此,我想说谢谢你的帮助


共 (1) 个答案

  1. # 1 楼答案

    If you are using jetty in an embedded scenario, and you need to use JSTL, then you must ensure that the JSTL jars are included on the container's classpath - that is the classpath that is the parent of the webapp's classpath. This is a restriction that arises from the Java EE specification.

    所以,你可以这样解决你的问题:

    WebAppContext webapp = new WebAppContext();
    ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
    URLClassLoader subClassLoader = new URLClassLoader(new URL[]{}, currentClassLoader);//this is the point.
    webapp.setClassLoader(subClassLoader); //this is the point.
    webapp.setContextPath("/");
    webapp.setResourceBase("src/main/webapp");
    server.setHandler(webapp);
    server.start();
    server.join();