【问题标题】:Spring Boot Index jsp works on localhost, but not found in linux serverSpring Boot Index jsp 在本地主机上工作,但在 linux 服务器中找不到
【发布时间】:2020-03-02 13:58:52
【问题描述】:

我创建了 Spring Boot 应用程序。它在我的本地主机中成功运行。 但是当我在linux centos服务器索引页面中部署和运行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>

【问题讨论】:

  • 我将 war 插入 pom.xml 并解决了问题:)

标签: linux spring maven spring-boot jsp


【解决方案1】:

由于 tomcat 中的硬编码模式,Springboot 和 jsp 无法与 jar 打包一起正常工作。因此,如果您使用 jsp 作为视图渲染并在 tomcat 中部署,最好使用 war 打包和 springboot。

问题是,当您使用jar 部署 springboot 应用程序时,jsp 文件将不会出现在 tomcat 中,并且在尝试服务请求时您将收到 404 PAGE NOT FOUND强>。这是因为 jar 打包,jsp 文件没有从 WEB-INF 文件夹中复制。如果在使用 jar 作为打包时将 jsp 文件保存在 META-INF/resources 文件夹下,它应该可以工作。但是 springboot 文档本身可以做到很明显,最好将 war 与 jsp 一起用作打包。

使用 Jetty 和 Tomcat,如果您使用 war 打包,它应该可以工作。一个 当使用 java -jar 启动时,可执行的 war 将起作用,并且还将 可部署到任何标准容器。不支持 JSP 时 使用可执行的 jar。

春天Doc

相关帖子:Why does Spring boot not support jsp while it can render the page if we add proper jar reference

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多