我创建了 Spring Boot 应用程序。它在我的本地主机上成功运行。 但是当我在linux centos server index page中部署运行jar文件时找不到 (我得到:出现意外错误(类型=未找到,状态=404)。 /WEB-INF/jsp/index.jsp)
这是我的代码结构和 application.properties :
这是我的 pom.xml 依赖项:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<version>6.1.0.jre8</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/javax.servlet/jstl -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
最佳答案
由于 tomcat 中的硬编码模式,Springboot 和 jsp 不能正常处理 jar 包装。因此,如果您使用 jsp 作为 View 渲染并部署在 tomcat 中,最好使用 war 打包和 springboot。
问题是,当您使用 jar
部署 springboot 应用程序时,jsp 文件不会出现在 tomcat 中,并且在尝试处理请求时您将得到一个 404找不到页面。这是因为 jar 打包,jsp 文件没有从 WEB-INF 文件夹中复制。如果在使用 jar 打包时将 jsp 文件保存在 META-INF/resources
文件夹下应该可以。但是 springboot 文档本身清楚地表明最好将 war 与 jsp 一起用作包装。
With Jetty and Tomcat, it should work if you use war packaging. An executable war will work when launched with java -jar, and will also be deployable to any standard container. JSPs are not supported when using an executable jar.
Spring Doc
相关帖子:Why does Spring boot not support jsp while it can render the page if we add proper jar reference
关于linux - Spring Boot Index jsp 在本地主机上工作,但在 linux 服务器上找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58724142/