解决了这个奇怪的问题,原因是我无法确定。正在从spring mvc迁移到spring boot 2,并希望继续使用.jsp
视图。想要在.jsp
中存储resources/templates
视图。但是,如果放置在/WEB-INF/pages/
中,则不会加载它们。
问题是-为什么会这样?我似乎有很多类似的配置,并且将它们存储在classpath中似乎是正常的做法。
示例错误日志:
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'
-即使从.jsp
也不会加载/WEB-INF/pages
,而
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/");
}
除.jsp
中的/view/
个文件外,所有资源都正在加载。如果我将.jsp
重命名为.html
,则视图文件已加载。 (但是,我不想重写很多.jsp
逻辑)
答案 0 :(得分:0)
您的以下配置是问题
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无法找到视图时,春季启动确实会退回到.html解析器。