linux - Spring Boot Index jsp 在本地主机上工作,但在 linux 服务器上找不到

标签 linux spring maven spring-boot jsp

我创建了 Spring Boot 应用程序。它在我的本地主机上成功运行。 但是当我在linux centos server index page中部署运行jar文件时找不到 (我得到:出现意外错误(类型=未找到,状态=404)。 /WEB-INF/jsp/index.jsp)

这是我的代码结构和 application.properties :

code structure

这是我的 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/

相关文章:

c++ - clock_nanosleep - 请求和保留的相同指针?

c - 为什么 "sys_clone"定义在 "./arch/h8300/kernel/process.c"中?

java - 以前使用的属性总是显示在 RequestBody 示例 (SwaggerUI) 和 ResponseBody(json) 中

java.lang.ClassFormat错误: Invalid code attribute name index 0

linux - 使用 linux 命令删除和替换一行

python - 删除 Flask Web 应用程序的终端依赖性

java - spring 生成的电子表格扩展名错误

java - CreateQuery 在没有 Activity 事务的情况下无效

java - 创建 Spring MVC 应用程序

java - Maven 站点插件问题