【问题标题】:Spring Boot 2.1.2 using classpath in .yml mvc configSpring Boot 2.1.2 在 .yml mvc 配置中使用类路径
【发布时间】:2019-01-17 15:08:12
【问题描述】:

遇到了这个我无法确定原因的奇怪问题。正在从 spring mvc 迁移到 spring boot 2 并希望继续使用 .jsp 视图。想要在resources/templates 中存储.jsp 视图。但是,如果放置在那里,它们不会被加载,但如果放置在 /WEB-INF/pages/ 中,它们就可以工作。

问题是 - 为什么会这样?我似乎有很多类似的配置,将它们存储在类路径中似乎是一种正常的做法。

错误日志示例:

2019-01-17 12:02:39,381 WARN  [http-nio-8080-exec-1] resource.ResourceHttpRequestHandler (ResourceHttpRequestHandler.java:642)  - Path represents URL or has "url:" prefix: [classpath:/templates/login.jsp]
2019-01-17 12:02:39,382 DEBUG [http-nio-8080-exec-1] resource.ResourceHttpRequestHandler (ResourceHttpRequestHandler.java:453)  - Resource not found
2019-01-17 12:02:39,383 DEBUG [http-nio-8080-exec-1] servlet.FrameworkServlet (FrameworkServlet.java:1126)  - Exiting from "FORWARD" dispatch, status 404
2019-01-17 12:02:39,384 DEBUG [http-nio-8080-exec-1] servlet.FrameworkServlet (FrameworkServlet.java:1130)  - Completed 404 NOT_FOUN

最小配置:

Spring Boot 2.1.2

application.properties(不工作):

spring.mvc.view.prefix: classpath:/templates/
spring.mvc.view.suffix: .jsp

logging.level.org.springframework.web: DEBUG

application.properties(工作):

spring.mvc.view.prefix: /WEB-INF/pages/
spring.mvc.view.suffix: .jsp

logging.level.org.springframework.web: DEBUG

分级:

plugins {
    id 'war'
    id "io.spring.dependency-management" version "1.0.6.RELEASE"
    id "org.springframework.boot" version "2.1.2.RELEASE"
}

repositories {
    maven {
        url = 'http://repo.maven.apache.org/maven2'
    }
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.boot:spring-boot-starter-log4j2'

    compile "javax.servlet:jstl:1.2"
    //providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
    providedRuntime "org.apache.tomcat.embed:tomcat-embed-jasper"
}

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
    }
}

tasks.withType(JavaCompile) {
    options.encoding = 'UTF-8'
}

更新:

我想它与tomcat有关。例如 providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat' - 即使从/WEB-INF/pages 也不会加载.jsp,而 providedRuntime "org.apache.tomcat.embed:tomcat-embed-jasper" 确实加载,但仅来自 /WEB-INF/pages

更新 2: 因此,它以某种方式与 tomcat & .jsp 扩展名相关联。 补充.jsp问题:

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/css/**").addResourceLocations("classpath:static/css/");
    registry.addResourceHandler("/js/**").addResourceLocations("classpath:static/js/");
    registry.addResourceHandler("/fonts/**").addResourceLocations("classpath:static/fonts/");
    registry.addResourceHandler("/view/**").addResourceLocations("classpath:static/view/");
    registry.addResourceHandler("/img/**").addResourceLocations("classpath:static/img/");
}

除了/view/ 中的.jsp 文件外,所有资源都在加载。如果我将 .jsp 视图文件重命名为 .html - 它们已加载。 (但是有很多.jsp逻辑我不想重写)

【问题讨论】:

    标签: java spring spring-boot gradle


    【解决方案1】:

    您的以下配置是问题

    spring.mvc.view.prefix: classpath:/templates/
    

    如下更新您的视图资源处理程序配置。

    registry.addResourceHandler("/view/**").addResourceLocations("classpath:static/view/");
    

    更新到,

    registry.addResourceHandler("/view/**").addResourceLocations("classpath:/resources/templates/view/");
    

    确保将所有 jsp 文件放在 /resources/templates/view/ 目录而不是 /static/view

    它适用于.html,因为我认为当相关的 jstl 无法找到视图时,spring boot 确实回退到了 .html 解析器。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-28
      • 1970-01-01
      • 2019-08-08
      • 2016-03-13
      相关资源
      最近更新 更多