【问题标题】:Embedded Jetty Error The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolvedEmbedded Jetty Error The absolute uri: http://java.sun.com/jsp/jstl/core 无法解析
【发布时间】:2014-12-23 13:12:14
【问题描述】:

我正在使用 Embedded Jetty 服务器运行我的测试并使用 Maven 进行构建。 以下是用于在测试前启动 Jetty 的代码。

System.out.println("Initializing Jetty Server...");
jettyServer = new Server(0);

WebAppContext webapp = new WebAppContext("src/main/webapp", "/testApp");
jettyServer.addHandler(webapp);

jettyServer.start();
int actualPort = jettyServer.getConnectors()[0].getLocalPort();
String baseUrl = "http://localhost:" + actualPort + "/testApp";

如果我使用 'Run as Junit Test' 运行它,所有测试都会通过。这里没有问题。

但如果我将其作为 Maven TestMaven Install 运行,测试会因以下原因而失败

Caused by: com.gargoylesoftware.htmlunit.FailingHttpStatusCodeException: 500 /WEB-INF/pages/test1.jsp(3,62) PWC6188: The absolute uri: http://java.sun.com/jsp/jstl/core cannot be resolved in either web.xml or the jar files deployed with this application

我在 POM.xml 中添加了 JSTL 依赖项。请帮助解决这个问题..

【问题讨论】:

    标签: maven junit jetty jstl


    【解决方案1】:

    您没有以适合 JSP 使用的方式初始化和设置环境。

    这需要大量额外的工作。

    您需要操作类加载器,设置一些初始化程序,声明 javac 实现行为,甚至声明 jsp servlet 处理。 (缺少其中任何一个,您将受制于您执行的环境,这在您的 3 个示例中有所不同)

    有关完整的 maven 项目示例,请参阅https://github.com/jetty-project/embedded-jetty-jsp

    public class Main
    {
        // Resource path pointing to where the WEBROOT is
        private static final String WEBROOT_INDEX = "/webroot/";
    
        public static void main(String[] args) throws Exception
        {
            int port = 8080;
            LoggingUtil.config();
            Log.setLog(new JavaUtilLog());
    
            Main main = new Main(port);
            main.start();
            main.waitForInterrupt();
        }
    
        private static final Logger LOG = Logger.getLogger(Main.class.getName());
    
        private int port;
        private Server server;
        private URI serverURI;
    
        public Main(int port)
        {
            this.port = port;
        }
    
        public URI getServerURI()
        {
            return serverURI;
        }
    
        public void start() throws Exception
        {
            server = new Server();
            ServerConnector connector = new ServerConnector(server);
            connector.setPort(port);
            server.addConnector(connector);
    
            URL indexUri = this.getClass().getResource(WEBROOT_INDEX);
            if (indexUri == null)
            {
                throw new FileNotFoundException("Unable to find resource " + WEBROOT_INDEX);
            }
    
            // Points to wherever /webroot/ (the resource) is
            URI baseUri = indexUri.toURI();
    
            // Establish Scratch directory for the servlet context (used by JSP compilation)
            File tempDir = new File(System.getProperty("java.io.tmpdir"));
            File scratchDir = new File(tempDir.toString(),"embedded-jetty-jsp");
    
            if (!scratchDir.exists())
            {
                if (!scratchDir.mkdirs())
                {
                    throw new IOException("Unable to create scratch directory: " + scratchDir);
                }
            }
    
            // Set JSP to use Standard JavaC always
            System.setProperty("org.apache.jasper.compiler.disablejsr199","false");
    
    
            // Setup the basic application "context" for this application at "/"
            // This is also known as the handler tree (in jetty speak)
            WebAppContext context = new WebAppContext();
            context.setContextPath("/");
            context.setAttribute("javax.servlet.context.tempdir",scratchDir);
            context.setResourceBase(baseUri.toASCIIString());
            context.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
            server.setHandler(context);
    
            // Add Application Servlets
            context.addServlet(DateServlet.class,"/date/");
    
            //Ensure the jsp engine is initialized correctly
            JettyJasperInitializer sci = new JettyJasperInitializer();
            ServletContainerInitializersStarter sciStarter = new ServletContainerInitializersStarter(context);
            ContainerInitializer initializer = new ContainerInitializer(sci, null);
            List<ContainerInitializer> initializers = new ArrayList<ContainerInitializer>();
            initializers.add(initializer);
    
            context.setAttribute("org.eclipse.jetty.containerInitializers", initializers);
            context.addBean(sciStarter, true);
    
    
    
            // Set Classloader of Context to be sane (needed for JSTL)
            // JSP requires a non-System classloader, this simply wraps the
            // embedded System classloader in a way that makes it suitable
            // for JSP to use
            ClassLoader jspClassLoader = new URLClassLoader(new URL[0], this.getClass().getClassLoader());
            context.setClassLoader(jspClassLoader);
    
            // Add JSP Servlet (must be named "jsp")
            ServletHolder holderJsp = new ServletHolder("jsp",JspServlet.class);
            holderJsp.setInitOrder(0);
            holderJsp.setInitParameter("logVerbosityLevel","DEBUG");
            holderJsp.setInitParameter("fork","false");
            holderJsp.setInitParameter("xpoweredBy","false");
            holderJsp.setInitParameter("compilerTargetVM","1.7");
            holderJsp.setInitParameter("compilerSourceVM","1.7");
            holderJsp.setInitParameter("keepgenerated","true");
            context.addServlet(holderJsp,"*.jsp");
            //context.addServlet(holderJsp,"*.jspf");
            //context.addServlet(holderJsp,"*.jspx");
    
            // Add Example of mapping jsp to path spec
            ServletHolder holderAltMapping = new ServletHolder("foo.jsp", JspServlet.class);
            holderAltMapping.setForcedPath("/test/foo/foo.jsp");
            context.addServlet(holderAltMapping,"/test/foo/");
    
            // Add Default Servlet (must be named "default")
            ServletHolder holderDefault = new ServletHolder("default",DefaultServlet.class);
            LOG.info("Base URI: " + baseUri);
            holderDefault.setInitParameter("resourceBase",baseUri.toASCIIString());
            holderDefault.setInitParameter("dirAllowed","true");
            context.addServlet(holderDefault,"/");
    
            // Start Server
            server.start();
    
            // Show server state
            if (LOG.isLoggable(Level.FINE))
            {
                LOG.fine(server.dump());
            }
    
            // Establish the Server URI
            String scheme = "http";
            for (ConnectionFactory connectFactory : connector.getConnectionFactories())
            {
                if (connectFactory.getProtocol().equals("SSL-http"))
                {
                    scheme = "https";
                }
            }
            String host = connector.getHost();
            if (host == null)
            {
                host = "localhost";
            }
            int port = connector.getLocalPort();
            serverURI = new URI(String.format("%s://%s:%d/",scheme,host,port));
            LOG.info("Server URI: " + serverURI);
        }
    
        public void stop() throws Exception
        {
            server.stop();
        }
    
        /**
         * Cause server to keep running until it receives a Interrupt.
         * <p>
         * Interrupt Signal, or SIGINT (Unix Signal), is typically seen as a result of a kill -TERM {pid} or Ctrl+C
         */
        public void waitForInterrupt() throws InterruptedException
        {
            server.join();
        }
    }
    

    【讨论】:

    • 非常感谢您提供的详细信息。在这里,我使用 Embedded Jetty 来运行我的测试。你能建议我一个简单的方法来做同样的事情吗?我正在使用JWebUnit 测试我的JSP 页面。由于Jwebunit 需要一个基本URL 开始,我在测试之前启动Jetty 服务器并在测试之后关闭。请给我一个解决方案
    【解决方案2】:

    在以下仓库中:https://github.com/ericminio/learning-jetty

    你可以找到:

    1. 演示 Jetty 服务 jsp 的 JstlTest,其中包含 c:forEach 标签
    2. pom 需要删除关于解析http://java.sun.com/jsp/jstl/core 的错误消息

    希望对你有帮助

    【讨论】:

      猜你喜欢
      • 2019-04-02
      • 2017-12-14
      • 2013-10-27
      • 1970-01-01
      • 2014-09-03
      • 2013-04-28
      • 2013-08-21
      • 2011-08-05
      • 2011-05-28
      相关资源
      最近更新 更多