【问题标题】:Unable to Compile class for JSP - embedded jetty 9无法为 JSP 编译类 - 嵌入式码头 9
【发布时间】:2016-12-01 21:22:48
【问题描述】:

我正在尝试设置一个嵌入式 jetty 9.3.9 服务器,已经为 Jetty 服务器配置了 JSP 支持 - 对这些东西不是很熟悉,但是,每当我尝试加载任何 JSP 页面时,我都会收到 -

Unable to compile class for JSP: ||An error occurred at line: [1] in the generated java file: [/tmp/jetty-0.0.0.0-8080-webapp-_webMvc-any-4185149399836239637.dir/jsp/org/apache/jsp/WEB_002dINF/views/index_jsp.java]|The type java.util.Map$Entry cannot be resolved. It is indirectly referenced from required .class files||An error occurred at line: [1] in the generated java file: [/tmp/jetty-0.0.0.0-8080-webapp-_webMvc-any-4185149399836239637.dir/jsp/org/apache/jsp/WEB_002dINF/views/index_jsp.java]|The type java.io.ObjectInputStream cannot be resolved|Syntax error on token "<", ? expected after this token|

我在 ServerStart.java 中的 JSP 支持是:

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

    Server server = new Server(8080);
    System.setProperty("org.apache.jasper.compiler.disablejsr199", "false");

    WebAppContext webAppContext = new WebAppContext();
    webAppContext.setDescriptor("/webMvc/src/main/webapp/WEB-INF/web.xml");
    webAppContext.setConfigurations(new Configuration[] {
                            new AnnotationConfiguration(),
                            new WebInfConfiguration(),
                            new WebXmlConfiguration(),
                            new MetaInfConfiguration(),
                            new FragmentConfiguration(),
                            new EnvConfiguration(),
                            new PlusConfiguration(),
                            new JettyWebXmlConfiguration()
                            });

    webAppContext.setAttribute("javax.servlet.context.tempdir", getScratchDir());
    webAppContext.setResourceBase("/webMvc/src/main/webapp/");
    webAppContext.setContextPath("/webMvc");
    webAppContext.setAttribute("org.eclipse.jetty.server.webapp.ContainerIncludeJarPattern",
                            ".*/[^/]*servlet-api-[^/]*\\.jar$|.*/javax.servlet.jsp.jstl-.*\\.jar$|.*/.*taglibs.*\\.jar$");

     webAppContext.setAttribute("org.eclipse.jetty.containerInitializers", jspInitializers());
     webAppContext.setAttribute(InstanceManager.class.getName(), new SimpleInstanceManager());
     webAppContext.addBean(new ServletContainerInitializersStarter(webAppContext), true);
     webAppContext.setClassLoader(new URLClassLoader(new URL[0], this.getClass().getClassLoader()));                    
     webAppContext.setParentLoaderPriority(true);       


    ProtectionDomain protectionDomain = ServerStart.class.getProtectionDomain();
    URL location = protectionDomain.getCodeSource().getLocation();
    webAppContext.setWar(location.toExternalForm());

    server.setHandler(webAppContext);        
    server.start();
    server.join();
}        

private File getScratchDir() throws IOException {
        File tempDir = new File(System.getProperty("java.io.tmpdir"));
        File scratchDir = new File(tempDir.toString(), "webMvc-jetty-jsp");
        scratchDir.mkdirs();
        return scratchDir;
}

private ServletHolder jspServletHolder() {
    ServletHolder holderJsp = new ServletHolder("jsp", JettyJspServlet.class);
                    holderJsp.setInitOrder(0);
                    holderJsp.setInitParameter("logVerbosityLevel", "INFO");
                    holderJsp.setInitParameter("fork", "false");
                    holderJsp.setInitParameter("xpoweredBy", "false");
                    holderJsp.setInitParameter("compilerTargetVM", "1.8");
                    holderJsp.setInitParameter("compilerSourceVM", "1.8");
                    holderJsp.setInitParameter("keepgenerated", "true");
                    return holderJsp;
}`

我的项目结构是 - `

jettyStartup
   |-src/main/java
     |- ServerStart.java
webMvc
   |- src/main/java
   | |- Package com.admin.controllers
   |    |- BaseController
   |- src
     |- main
       |- webapp
         |- META-INF
         |- WEB-INF
           |- web.xml
           |- views
             |-index.jsp
             |-error.jsp

`

我有一个单独的项目用于启动服务器,另一个用于 MVC(用于模块化)我创建了 war 文件并将其提供给码头服务器。在我点击 jsp 页面之前一切正常。

我的 index.jsp 是:

<%@ page import="org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter" %>
<%@ page import="org.springframework.security.core.AuthenticationException" %>

<!DOCTYPE html>
    <head>
        <link href="css/themes/jquery-ui.css" rel="stylesheet" type="text/css"/> 
        <link href="css/themes/page-theme.css" rel="stylesheet" type="text/css"/> 
        <link href="css/index.css" rel="stylesheet" type="text/css">
        <link href="css/jquery.modaldialog.css" rel="stylesheet" type="text/css">

        <script type="application/javascript" src="js/jquery/jquery-1.4.4.js"></script>
        <script type="application/javascript" src="js/jquery/jquery-ui-1.8.10.custom.js"></script>
        <script src="js/index.js" type="text/javascript"></script>          
    </head>
    <body>
        <div id="page-login" >
                <form id="login" method="POST" action="j_spring_security_check" autocomplete="off">
                    <div id="login-credentials">                                    
                        <label>Username:
                                <input type="text" name="j_username" id="j_username"/>
                        </label>
                        <label> Password:
                                <input type="password" name="j_password" id="j_password" />
                                <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/>
                        </label>
                    </div>
                    <input type="submit" id="login-btn" value="Log in" /> 
                    <div style="clear:right"></div>
                </form>
        </div>        
    </body>
</html>

【问题讨论】:

  • 这就是为什么您的 JSP 中没有重要的 Java 代码。
  • @ThorbjørnRavnAndersen 代码不是 JSP 的一部分,它是用于启动嵌入式码头服务器的 Java 类

标签: java jsp spring-mvc embedded-jetty jetty-9


【解决方案1】:

您在方法内部有方法,或者在任何方法之外都有代码。取一小段代码......例如,这永远不会编译:

          ProtectionDomain protectionDomain = ServerStart.class.getProtectionDomain();
            URL location = protectionDomain.getCodeSource().getLocation();          
            webAppContext.setWar(location.toExternalForm());

            private File getScratchDir() throws IOException {
                File tempDir = new File(System.getProperty("java.io.tmpdir"));
                File scratchDir = new File(tempDir.toString(), "webMvc-jetty-jsp");
                scratchDir.mkdirs();
                return scratchDir;
            }

这和做这样的事情是一样的:

public static void main(String[] args){
    public void file(){}
}

方法中不能有方法。

很高兴您尝试使用私有方法,但是您无法在 JSP 中执行您想要执行的操作。

您需要将所有代码内联,没有任何方法。最好不要将 任何 Java 代码放在 JSP 中。

【讨论】:

  • 感谢您的理解并为造成的混乱道歉,我已经编辑了 Java 代码以使其更加清晰。问题中的 java 代码用于启动嵌入式码头服务器。这不是任何 JSP 的一部分。
  • 那么你需要包含导致错误的文件:An error occurred at line: [1] in the generated java file: [/tmp/jetty-0.0.0.0-8080-webapp-_webMvc-any-4185149399836239637.dir/jsp/org/apache/jsp/WEB_002dINF/views/index_jsp.java 这表明 index.jsp 不正确
  • 这是一个基本的登录页面。我在尝试加载的所有 jsp 页面上都收到该错误。我附上了我的 index.jsp。其他所有的jsp页面都与此类似。
猜你喜欢
  • 1970-01-01
  • 2014-02-25
  • 2017-05-12
  • 2017-12-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-12-13
  • 2015-11-15
相关资源
最近更新 更多