gpt4 book ai didi

java - 碧 Jade 异常 : Unable to compile class for JSP - Tomcat 8 plus Spring MVC

转载 作者:行者123 更新时间:2023-11-28 22:05:21 26 4
gpt4 key购买 nike

我成功地遵循了 Oracle 的优秀教程,该教程展示了如何创建一个嵌入式 tomcat 应用程序,我可以通过 java -jar\targeg\"myExecutableJar"启动它。在这样的教程中根本没有 Spring。 ( Oracle Tutorial

然后我编辑了 index.jsp,如下所示。显然,我在 pom.xml 中添加了所有必要的 Spring 库。

在此之后,我从我的问题主题中得到了错误。我已经阅读了几个具有相似点的问题,但没有一个解决了我的问题。例如,在某些问题中,有人告诉我检查 ecj,所以我将它添加到 pom.xml 中。其他答案建议使用最新的 Tomcat。我做到了。

在我看来,某些嵌入式 tomcat 库与其他库之间可能存在一些冲突,但我无法想象是哪一个。

其他可能性可能与 spring 上下文的某些问题有关,特别是因为它是一个嵌入式 tomcat,但我猜不出还有什么可以尝试的。

我是否缺少任何额外的配置?请注意,在下图中,我将 META-INF 下的 context.xml 保留为自动生成的,并将所有 beans 定义放在 App-servlet.xml 中。

Project Structure and the main method

索引.jsp

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<html>

<body>



<h2>Uploading a file.. </h2>

<form:form method="POST" action="uploadFile" enctype="multipart/form-data">

Upload your file please:

<input type="file" name="file" />

<input type="text" name="name">

<input type="submit" value="Upload" />

<form:errors path="file" cssStyle="color: #ff0000;" />

</form:form>



</body>

</html>

App-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="

http://www.springframework.org/schema/beans

http://www.springframework.org/schema/beans/spring-beans-4.2.xsd

http://www.springframework.org/schema/context

http://www.springframework.org/schema/context/spring-context-4.2.xsd">



<context:component-scan base-package="com.example.employees" />



<bean

class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="prefix" value="/WEB-INF/" />

<property name="suffix" value=".jsp" />

</bean>



<bean id="multipartResolver"

class="org.springframework.web.multipart.commons.CommonsMultipartResolver" />



</beans>

网络.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">



<display-name>Spring MVC Application</display-name>









<servlet>

<servlet-name>App</servlet-name>

<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>

<init-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/App-servlet.xml</param-value>

</init-param>

<load-on-startup>1</load-on-startup>

</servlet>



<servlet-mapping>

<servlet-name>App</servlet-name>

<url-pattern>/</url-pattern>

</servlet-mapping>

<welcome-file-list>

<welcome-file>index.jsp</welcome-file>

</welcome-file-list>

</web-app>

点数

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">

<modelVersion>4.0.0</modelVersion>

<groupId>com.example.employees</groupId>

<artifactId>employees-app</artifactId>

<packaging>war</packaging>

<version>1.0-SNAPSHOT</version>

<name>employees-app Maven Webapp</name>

<url>http://maven.apache.org</url>

<properties>

<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<tomcat.version>8.0.32</tomcat.version>

<spring.version>4.2.5.RELEASE</spring.version>

</properties>

<dependencies>



<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-web</artifactId>

<version>${spring.version}</version>

</dependency>



<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-webmvc</artifactId>

<version>${spring.version}</version>

</dependency>



<dependency>

<groupId>commons-fileupload</groupId>

<artifactId>commons-fileupload</artifactId>

<version>1.3.1</version>

</dependency>



<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-core</artifactId>

<version>${tomcat.version}</version>

</dependency>

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-logging-juli</artifactId>

<version>${tomcat.version}</version>

</dependency>

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

<version>${tomcat.version}</version>

</dependency>

<dependency>

<groupId>org.apache.tomcat</groupId>

<artifactId>tomcat-jasper</artifactId>

<version>${tomcat.version}</version>

</dependency>

<dependency>

<groupId>org.apache.tomcat</groupId>

<artifactId>tomcat-jasper-el</artifactId>

<version>${tomcat.version}</version>

</dependency>

<dependency>

<groupId>org.apache.tomcat</groupId>

<artifactId>tomcat-jsp-api</artifactId>

<version>${tomcat.version}</version>

</dependency>

<dependency>

<groupId>jstl</groupId>

<artifactId>jstl</artifactId>

<version>1.2</version>

</dependency>

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>javax.servlet-api</artifactId>

<version>3.0.1</version>

</dependency>

<dependency>

<groupId>org.eclipse.jdt.core.compiler</groupId>

<artifactId>ecj</artifactId>

<version>4.5.1</version>

</dependency>



</dependencies>

<build>

<finalName>employees-app</finalName>

<resources>

<resource>

<directory>src/main/webapp</directory>

<targetPath>META-INF/resources</targetPath>

</resource>

</resources>

<plugins>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-compiler-plugin</artifactId>

<version>3.5.1</version>

<inherited>true</inherited>

<configuration>

<source>1.8</source>

<target>1.8</target>

</configuration>



</plugin>

<plugin>

<groupId>org.apache.maven.plugins</groupId>

<artifactId>maven-assembly-plugin</artifactId>

<configuration>

<descriptorRefs>

<descriptorRef>jar-with-dependencies</descriptorRef>

</descriptorRefs>

<finalName>employees-app-${project.version}</finalName>

<archive>

<manifest>

<mainClass>com.example.employees.Main</mainClass>

</manifest>

</archive>

</configuration>

<executions>

<execution>

<phase>package</phase>

<goals>

<goal>single</goal>

</goals>

</execution>

</executions>

</plugin>

</plugins>

</build>

</project>

日志:

type Exception report

message Unable to compile class for JSP

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: Unable to compile class for JSP

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:600)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

root cause

java.lang.NullPointerException

org.apache.jasper.JspCompilationContext.getTldResourcePath(JspCompilationContext.java:551)

org.apache.jasper.compiler.Parser.parseTaglibDirective(Parser.java:420)

org.apache.jasper.compiler.Parser.parseDirective(Parser.java:479)

org.apache.jasper.compiler.Parser.parseElements(Parser.java:1435)

org.apache.jasper.compiler.Parser.parse(Parser.java:139)

org.apache.jasper.compiler.ParserController.doParse(ParserController.java:227)

org.apache.jasper.compiler.ParserController.parse(ParserController.java:100)

org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:199)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:356)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:336)

org.apache.jasper.compiler.Compiler.compile(Compiler.java:323)

org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:585)

org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:363)

org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:396)

org.apache.jasper.servlet.JspServlet.service(JspServlet.java:340)

javax.servlet.http.HttpServlet.service(HttpServlet.java:729)

************ 添加了

enter image description here

最佳答案

这可能是由于 servlet.jar jar 冲突。 Tomcat 已经带有 servlet api 库。尝试添加 <scope>provided</provide>对于 pom.xml 中的 servlet 依赖项。

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>

此依赖项已由您的容器(即 Tomcat)提供,因此您不必再次将其包含在已部署的应用程序中。

您可以尝试的其他事情:

  • 尝试项目 -> 清理并重新运行项目
  • 您应该至少需要 JSP 2.1 并在您的 POM 中将其标记为已提供

    <dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jsp-api</artifactId>
    <version>2.1</version>
    <scope>provided</scope>
    </dependency>
  • 一个可能的原因是临时文件无法写入当前目录。

  • this article特定于 1.8 版本的 JDK。
  • In Tomcat 8, Jasper initialization is implemented as a standard ServletContainderInitializer (check Servlet specification). So you should ensure that org.apache.jasper.servlet.JasperInitializer.onStartup is invoked during web app startup. Source

  • 设置上下文,context.addServletContainerInitializer(new JasperInitializer(), null);
  • 相关其他https://github.com/spring-projects/spring-boot/issues/962 & https://github.com/bmuschko/gradle-tomcat-plugin/issues/132

关于java - 碧 Jade 异常 : Unable to compile class for JSP - Tomcat 8 plus Spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35884641/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com