jetty 8.x, 9.x无法加载jstl的PWC6188问题

时间:2023-03-09 13:26:23
jetty 8.x, 9.x无法加载jstl的PWC6188问题

参考:

cannot load JSTL taglib within embedded Jetty server:http://*.com/questions/2151075/cannot-load-jstl-taglib-within-embedded-jetty-server

How do you get embedded Jetty 9 to successfully resolve the JSTL URI?: http://*.com/questions/17685330/how-do-you-get-embedded-jetty-9-to-successfully-resolve-the-jstl-uri​;

JSTL核心标签使用:http://www.cnblogs.com/lihuiyy/archive/2012/02/24/2366806.html

JSP表达式语言:http://blog.csdn.net/csuliky/article/details/2452207
Using Spring3.2 MVC with embedded jetty9:http://*.com/questions/19169377/using-spring3-2-mvc-with-embedded-jetty9

1. 简述

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1" isELIgnored="false" %>

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

上述异常在tomcat等地方只需要在页面上添加相应的语句即可解决。但是这里使用的嵌入式Jetty时还是会出现上述问题。

在jetty启动前添加上面的代码。问题原因是Jetty无法加载JSTL类库。这是一个bug

2. Jetty依赖

<dependency>

<groupId>org.eclipse.jetty</groupId>

<artifactId>jetty-webapp</artifactId>

<version>9.0.7.v20131107</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.eclipse.jetty</groupId>

<artifactId>jetty-servlet</artifactId>

<version>9.0.7.v20131107</version>

<scope>test</scope>

</dependency>

<dependency>

<groupId>org.eclipse.jetty</groupId>

<artifactId>jetty-jsp</artifactId>

<version>9.0.7.v20131107</version>

</dependency>

3. 运行代码

public class JettyServer {

public static void main(String[] args) throws Exception {

System.setProperty("DWPROJECTNO", "resource");

System.setProperty("DWENV", "dev");

startJettyServer(80, "/");

}

/**

* 创建用于正常运行调试的Jetty Server, 以src/main/webapp为Web应用目录.

*

* @throws Exception

*/

public static void startJettyServer(int port, String contextPath) throws Exception {

/* fix jetty8.x, jetty 9.x can't load jstl library */

Field f = TldScanner.class.getDeclaredField("systemUris");

f.setAccessible(true);

((Set<?>) f.get(null)).clear();

f.setAccessible(false);

Server server = new Server(port);

WebAppContext webContext = new WebAppContext("src/main/webapp", contextPath);

webContext.setClassLoader(Thread.currentThread().getContextClassLoader());

webContext.setDefaultsDescriptor("src/test/resources/jetty-webdefault.xml");

server.setHandler(webContext);

server.setStopAtShutdown(true);

server.start();

server.join();

}

}