Java 无法在嵌入式 Jetty 服务器中加载 JSTL taglib

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2151075/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 04:09:54  来源:igfitidea点击:

cannot load JSTL taglib within embedded Jetty server

javajettyjstltaglibembedded-jetty

提问by

I am writing a web application that runs within an embedded Jetty instance.

我正在编写一个在嵌入式 Jetty 实例中运行的 Web 应用程序。

When I attempt to execute a JSTL statement, I receive the following exception:

当我尝试执行 JSTL 语句时,收到以下异常:

org.apache.jasper.JasperException: /index.jsp(1,63) PWC6188: The absolute uri: http://java.sun.com/jsp/jstl/corecannot be resolved in either web.xml or the jar files deployed with this application

org.apache.jasper.JasperException:/index.jsp(1,63) PWC6188:绝对 uri:http: //java.sun.com/jsp/jstl/core无法在 web.xml 或 jar 文件中解析使用此应用程序部署

I have the following jars on the classpath

我在类路径上有以下 jars

  • ant-1.6.5.jar
  • ant-1.7.1.jar
  • ant-launcher-1.7.1.jar
  • core-3.1.1.jar
  • jetty-6.1.22.jar
  • jetty-util-6.1.22.jar
  • jsp-2.1-6.1.14.jar
  • jsp-api-2.1.jar
  • jstl-1.2.jar
  • servlet-api-2.5-20081211.jar
  • servlet-api-2.5-6.1.14.jar
  • standard-1.1.2.jar
  • ant-1.6.5.jar
  • ant-1.7.1.jar
  • ant-launcher-1.7.1.jar
  • core-3.1.1.jar
  • 码头-6.1.22.jar
  • jetty-util-6.1.22.jar
  • jsp-2.1-6.1.14.jar
  • jsp-api-2.1.jar
  • jstl-1.2.jar
  • servlet-api-2.5-20081211.jar
  • servlet-api-2.5-6.1.14.jar
  • 标准 1.1.2.jar

My web.xml looks like this:

我的 web.xml 看起来像这样:

<?xml version="1.0" encoding="ISO-8859-1"?>  
    <web-app xmlns="http://java.sun.com/xml/ns/j2ee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee h77p://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"  
    version="2.4">  
    <display-name>test</display-name>  
</web-app>

My code looks like this:

我的代码如下所示:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>  
<html>  
    <body>  
        <h2>Hello World!</h2>  
        <%= new java.util.Date() %><br/>  
        ${1+2}<br/>  
        <c:out var="${5+9}"/><br/>  
    </body>  
</html>

I started my embedded Jetty server like this:

我像这样启动了我的嵌入式 Jetty 服务器:

Server server = new Server(80);  
WebAppContext context = new WebAppContext("pig-1.0-SNAPSHOT.war","/");
server.addHandler(context);
server.start();

I spent the past two days experimenting with various combinations of jar files, web.xml configurations, and tag library declarations, but to no avail.

过去两天我尝试了各种 jar 文件、web.xml 配置和标签库声明的组合,但无济于事。

How can I get an embedded Jetty server up and running with full JSTL support?

如何启动并运行具有完整 JSTL 支持的嵌入式 Jetty 服务器?

回答by BalusC

  • jstl-1.2.jar
  • standard-1.1.2.jar
  • jstl-1.2.jar
  • 标准 1.1.2.jar

This collides. Remove the standard-1.1.2.jar. You should use standard-1.1.2.jaronlywith jstl-1.1.2.jar. Since JSTL 1.2 the standard JAR has been merged into JSTL JAR, resulting in a single jstl-1.2.jarfile.

这碰撞。删除standard-1.1.2.jar. 你应该standard-1.1.2.jar使用with jstl-1.1.2.jar。从 JSTL 1.2 开始,标准 JAR 已合并到 JSTL JAR 中,从而形成单个jstl-1.2.jar文件。

回答by Drew

In your web.xml, try changing "h77p://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"to start with "http://" and see if that fixes the error.

在您的 web.xml 中,尝试将“h77p://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”更改为以“http://”开头,看看是否能修复错误。

However, that may not be the underlying cause, since I had that same error when using jetty-maven-plugin and JSTL taglib header in my JSP:

但是,这可能不是根本原因,因为我在 JSP 中使用 jetty-maven-plugin 和 JSTL taglib 标头时遇到了同样的错误:

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

PWC6188: The absolute uri: http://java.sun.com/jsp/jstl/corecannot be resolved in either web.xml or the jar files deployed with this application

PWC6188:绝对 uri:http: //java.sun.com/jsp/jstl/core无法在 web.xml 或使用此应用程序部署的 jar 文件中解析

I'm using an out-of-the-box Spring MVC template from SpringSource Tool Suite, so am not sure why the Maven plugin for Jetty chokes on it.

我正在使用来自 SpringSource Tool Suite 的开箱即用的 Spring MVC 模板,所以我不确定为什么 Jetty 的 Maven 插件会阻塞它。

<build>
   <plugins>
      <plugin>
         <groupId>org.mortbay.jetty</groupId>
         <artifactId>jetty-maven-plugin</artifactId>
      </plugin>
   </plugins>
</build>
<repositories>
     <repository>
       <id>maven2-repository.dev.java.net</id>
       <name>Java.net Repository for Maven</name>
       <url>http://download.java.net/maven/2/</url>
       <layout>default</layout>
     </repository>
</repositories>

And only javax.servlet:jstl:1.2 is listed in my POM's dependencies, since it now obsoletes taglibs:standard:1.1.2, which was a suggestion given above.

并且只有 javax.servlet:jstl:1.2 列在我的 POM 的依赖项中,因为它现在过时了 taglibs:standard:1.1.2,这是上面给出的建议。

回答by bmargulies

Jetty 8.0, which has pushed as the default when you use jetty:run, has servlet API 3.0. As of that version of the standard, JSTL standard is supposed to be included, and these taglibs cannot be in the webapp classpath, only the standard classpath. However, 8.0.0.M0 forgot to include them.

Jetty 8.0,在您使用 jetty:run 时已作为默认推送,具有 servlet API 3.0。从该版本的标准开始,应该包含 JSTL 标准,并且这些标签库不能在 webapp 类路径中,只能在标准类路径中。但是,8.0.0.M0 忘了包括它们。

Specifying 7.1.4.v20100610 helped for me.

指定 7.1.4.v20100610 对我有帮助。

回答by LND

@Drew Thanks Drew. It works.I had been googling for this and end up here.What my mistake was : I was using

@Drew 谢谢德鲁。它有效。我一直在谷歌搜索并最终到了这里。我的错误是:我正在使用

                 <dependency>
                    <groupId>javax.servlet</groupId>
                    <artifactId>jstl</artifactId>
                     <version>1.1.2</version>
                     <scope>provided</scope>
                </dependency>

I changed it from above to

我从上面改成

           <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
             <version>1.2</version>
             <scope>provided</scope>
        </dependency>

and it got working. Also I was using jstls dependency which I removed.

它开始工作了。我还使用了我删除的 jstls 依赖项。

回答by Arsouille

The jstl taglibs must be on serverclasspath. You can add a classloader to current classloader chain before start the server. That's the principle used by start.jar when it start a jetty server.

jstl 标记库必须位于服务器类路径上。您可以在启动服务器之前将类加载器添加到当前的类加载器链中。这就是 start.jar 在启动 jetty 服务器时使用的原理。

ClassLoader currentClassLoader = Thread.currentThread().getContextClassLoader();
URL urlTaglibs = new File(PATH_TO_TAGLIBS).toURI().toURL();
URLClassLoader newClassLoader = new URLClassLoader(new URL[]{urlTaglibs},currentClassLoader);
Thread.currentThread().setContextClassLoader(newClassLoader);

server.start();

You should also add it to java start command line argument.

您还应该将它添加到 java start 命令行参数。

回答by N.G

I also had the same problems. I fixed it by adding the below code:

我也有同样的问题。我通过添加以下代码来修复它:

public static final String[] TLD_JAR_NAMES = new String[]{"sitemesh", "spring-webmvc", "shiro-web", "springside-core"};
...
JettyFactory.setTldJarNames(server, TLD_JAR_NAMES);

Maybe you can try it. Please replace TLD_JAR_NAMES with your real TLD Jar names.

也许你可以试试。请将 TLD_JAR_NAMES 替换为您真实的 TLD Jar 名称。

回答by Cristiano Costantini

I got the same problem on Jetty 7, I solved it by enabling Jetty to search for TLD:

我在 Jetty 7 上遇到了同样的问题,我通过启用 Jetty 搜索 TLD 来解决它:

I did it by setting an attribute on the context:

我通过在上下文中设置一个属性来做到这一点:

Server server = new Server(80);  
WebAppContext context = new WebAppContext("pig-1.0-SNAPSHOT.war","/");
context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern", 
    ".*/.*jsp-api-[^/]*\.jar$|.*/.*jsp-[^/]*\.jar$|.*/.*taglibs[^/]*\.jar$");
server.addHandler(context);
server.start();

Refer to http://wiki.eclipse.org/Jetty/Howto/Configure_JSP#Using_JSTL_Taglibs_for_Jetty_7.x_and_8.xfor further details.

有关更多详细信息,请参阅http://wiki.eclipse.org/Jetty/Howto/Configure_JSP#Using_JSTL_Taglibs_for_Jetty_7.x_and_8.x

On my project (using maven), I have standard TLDs are on the JAR "org.apache.taglibs.standard.glassfish-1.2.0.v2011120803.jar" and theoretically it would be enough to use as value for ContainerIncludeJarPattern the following pattern:

在我的项目中(使用 maven),我在 JAR 上有标准的 TLD“org.apache.taglibs.standard.glassfish-1.2.0.v2011120803.jar”,理论上它足以作为 ContainerIncludeJarPattern 的值使用以下模式:

".*/org\.apache\.taglibs\.standard\.glassfish-1\.2\.0\.v201112081803\.jar"

It actually works and it is a confirmation of where do jetty found the tag libs, but I've preferred to leave the previous pattern which I found on the wiki.eclipse.org page linked above.

它确实有效,并且确认了 jetty 在哪里找到了标签库,但我更喜欢保留我在上面链接的 wiki.eclipse.org 页面上找到的先前模式。

It may be required to extend the pattern if you want to include custom tag libs.

如果您想包含自定义标签库,可能需要扩展模式。

回答by Daniel

I had the same problem and found out that http://java.sun.com/jsp/jstl/coreis regarded as the single system URI and all taglib definitions that try to define it are ignored (but when referenced, an error occurs anyway).

我遇到了同样的问题,发现它http://java.sun.com/jsp/jstl/core被视为单个系统 URI,并且所有尝试定义它的 taglib 定义都被忽略(但在引用时,无论如何都会发生错误)。

I used the following before starting the Jetty and now it works:

我在启动 Jetty 之前使用了以下内容,现在它可以工作了:

try {
    Field f = TldScanner.class.getDeclaredField("systemUris");
    f.setAccessible(true);
    ((Set)f.get(null)).clear();
} catch (Exception e) {
    throw new RuntimeException("Could not clear TLD system uris.",e);
}

回答by Alok Rana

By adding the following code I got rid of the problem:

通过添加以下代码,我解决了这个问题:

<dependency>
    <groupId>org.apache.tomcat</groupId>
    <artifactId>jasper</artifactId>
    <version>6.0.29</version>
</dependency>

I am using jetty-runner 8.

我正在使用码头跑步者 8。

回答by David Chan

Two step:

两步:

1)add annotation support for the server

1)为服务端添加注解支持

//Enable parsing of jndi-related parts of web.xml and jetty-env.xml, #server is

//开启web.xml和jetty-env.xml中jndi相关部分的解析,#server是

org.eclipse.jetty.webapp.Configuration.ClassList classlist = org.eclipse.jetty.webapp.Configuration.ClassList.setServerDefault(server);
classlist.addAfter("org.eclipse.jetty.webapp.FragmentConfiguration", "org.eclipse.jetty.plus.webapp.EnvConfiguration", "org.eclipse.jetty.plus.webapp.PlusConfiguration");
classlist.addBefore("org.eclipse.jetty.webapp.JettyWebXmlConfiguration", "org.eclipse.jetty.annotations.AnnotationConfiguration");

2) add the follow attribute to the WebAppContext

2)在WebAppContext中添加follow属性

context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*/[^/]*servlet-api-[^/]*\.jar$|.*/javax.servlet.jsp.jstl-.*\.jar$|.*/org.apache.taglibs.taglibs-standard-impl-.*\.jar$");