无法在嵌入式Jetty服务器中加载JSTL taglib
我正在编写一个在嵌入式Jetty实例中运行的Web应用程序。
当我尝试执行JSTL语句时,收到以下exception:
org.apache.jasper.JasperException:/index.jsp(1,63)PWC6188:绝对的uri: http : //java.sun.com/jsp/jstl/core无法在web.xml或jar文件中解析部署此应用程序
我在类路径上有以下jar
- ANT-1.6.5.jar
- ANT-1.7.1.jar
- ant发射-1.7.1.jar
- 核心3.1.1.jar
- docker,6.1.22.jar
- docker-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
我的web.xml看起来像这样:
test
我的代码如下所示:
Hello World!
${1+2}
我开始使用这样的嵌入式Jetty服务器:
Server server = new Server(80); WebAppContext context = new WebAppContext("pig-1.0-SNAPSHOT.war","/"); server.addHandler(context); server.start();
过去两天我花了不少尝试jar文件,web.xml配置和标签库声明的各种组合,但无济于事。
如何在完整的JSTL支持下启动并运行嵌入式Jetty服务器?
Jetty 8.0,在使用jetty:run时作为默认值推送,具有servlet API 3.0。 从该版本的标准开始,应该包含JSTL标准,并且这些taglib不能在webapp类路径中,只能在标准类路径中。 但是,8.0.0.M0忘了包含它们。
指定7.1.4.v20100610对我有帮助。
jstl taglib必须位于服务器类路径上。 您可以在启动服务器之前将类加载器添加到当前的类加载器链。 这是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();
您还应该将其添加到java start命令行参数中。
- JSTL-1.2.jar
- 标准1.1.2.jar
这碰撞了。 删除standard-1.1.2.jar
。 您应该仅将standard-1.1.2.jar
与jstl-1.1.2.jar
。 从JSTL 1.2开始,标准JAR已合并到JSTL JAR中,从而生成单个jstl-1.2.jar
文件。
@Drew感谢Drew。 它工作。我一直在谷歌上搜索并最终在这里。我的错误是:我正在使用
javax.servlet jstl 1.1.2 provided
我从上面改为
javax.servlet jstl 1.2 provided
它开始工作了。 我也使用了我删除的jstls依赖项。
我在Jetty 7上遇到了同样的问题,我通过让Jetty搜索TLD解决了这个问题:
我是通过在上下文中设置属性来完成的:
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();
有关更多详细信息,请参阅http://wiki.eclipse.org/Jetty/Howto/Configure_JSP#Using_JSTL_Taglibs_for_Jetty_7.x_and_8.x 。
在我的项目上(使用maven),我在JAR“org.apache.taglibs.standard.glassfish-1.2.0.v2011120803.jar”上有标准TLD,理论上它可以作为ContainerIncludeJarPattern的值用于以下模式:
".*/org\\.apache\\.taglibs\\.standard\\.glassfish-1\\.2\\.0\\.v201112081803\\.jar"
它实际上是有效的,它确认了jetty在哪里找到了标签库,但我更倾向于保留我在上面链接的wiki.eclipse.org页面上找到的先前模式。
如果要包含自定义标记库,可能需要扩展模式。
我遇到了同样的问题,发现http://java.sun.com/jsp/jstl/core
被视为单个系统URI,并且忽略了尝试定义它的所有taglib定义(但是在引用时,会发生错误无论如何)。
在启动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); }
两步:
1)为服务器添加注释支持
//启用解析web.xml和jetty-env.xml的jndi相关部分,#server is
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)将follow属性添加到WebAppContext
context.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/org.apache.taglibs.taglibs-standard-impl-.*\\.jar$");
在您的web.xml中,尝试将“h77p://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”更改为以“http://”开头,看看是否可以修复错误。
但是,这可能不是根本原因,因为我在JSP中使用jetty-maven-plugin和JSTL taglib头时遇到了同样的错误:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
PWC6188:绝对uri: http : //java.sun.com/jsp/jstl/core无法在web.xml或使用此应用程序部署的jar文件中解析
我正在使用来自SpringSource Tool Suite的开箱即用的Spring MVC模板,所以我不确定为什么Jetty的Maven插件会呛到它。
org.mortbay.jetty jetty-maven-plugin maven2-repository.dev.java.net Java.net Repository for Maven http://download.java.net/maven/2/ default
并且只有javax.servlet:jstl:1.2列在我的POM的依赖项中,因为它现在废弃了taglibs:standard:1.1.2,这是上面给出的建议。
我也有同样的问题。 我通过添加以下代码修复它:
public static final String[] TLD_JAR_NAMES = new String[]{"sitemesh", "spring-webmvc", "shiro-web", "springside-core"}; ... JettyFactory.setTldJarNames(server, TLD_JAR_NAMES);
也许你可以尝试一下。 请将TLD_JAR_NAMES替换为您的真实TLD Jar名称。
通过添加以下代码,我摆脱了问题:
org.apache.tomcat jasper 6.0.29
我正在使用jetty-runner 8。