有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Spring引导在哪里需要JSP

我做了this教程,将一个旧的jsp应用程序迁移到spring boot。但我用的不是maven而是gradle。在IntelliJ Idea中,一切都是明摆着的。 现在我想为这个应用程序制作一个docker容器,因此我使用了jib gradle插件。但是当我运行这个docker容器时,我得到了一个错误“出现了一个意外错误(type=notfound,status=404)。/WEB-INF/jsp/index.jsp”。实际上,docker容器中缺少/WEB-INF/jsp目录。所以我通过jib参数添加了它,但仍然没有找到它。那么Spring在哪里查找这些文件呢?我必须把文件放在哪里?是否可以使用属性更改此目录


共 (2) 个答案

  1. # 1 楼答案

    经过数小时的调试,我发现可以通过设置ServletWebServerFactory的文档根来设置jsp文件的目录

    @Configuration
    public class TomcatConfig {
    
      @Value("${tomcat.server.document-root}")
      String documentRoot;
    
      @Bean
      public ServletWebServerFactory servletContainer() {
        TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory();
        File documentRootFile = new File(documentRoot);
        tomcat.setDocumentRoot(documentRootFile);
        return tomcat;
      }
    }
    

    这为我解决了问题

  2. # 2 楼答案

    春天说

    JSP Limitations

    When running a Spring Boot application that uses an embedded servlet container (and is packaged as an executable archive), there are some limitations in the JSP support.

    • With Jetty and Tomcat, it should work if you use war packaging. ... JSPs are not supported when using an executable jar.

    你也可以在这个SO question中获得更多信息

    我查看了您提供的教程链接,发现项目打包是jar,而不是war。可能需要通过应用Gradle WAR插件将项目转换为war项目