【发布时间】:2019-05-05 08:14:23
【问题描述】:
我在 Spring-MVC 中显示 jsp 页面时遇到问题。 这是一个带有 Gradle 和 IntelliJ CE 的基本 hello world Spring-MVC:
我收到以下错误页面:
这是我的 build.gradle:
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.0.5.RELEASE")
}
}
plugins {
id 'java'
}
group 'com.helloct'
version '1.0-SNAPSHOT'
sourceCompatibility = 1.8
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
bootJar {
baseName = 'gs-serving-web-content'
version = '0.1.0'
}
dependencies {
testCompile group: 'junit', name: 'junit', version: '4.12'
compile("org.springframework.boot:spring-boot-starter-web")
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
compile("org.springframework.boot:spring-boot-devtools")
compile("org.springframework.boot:spring-boot-starter")
compile("org.springframework:spring-jdbc")
compile("com.h2database:h2")
compile("com.fasterxml.jackson.core:jackson-databind")
compile('javax.servlet:jstl')
compile('org.apache.tomcat.embed:tomcat-embed-jasper')
compile 'javax.servlet.jsp:javax.servlet.jsp-api'
testCompile("junit:junit")
}
视图解析器文件:
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "hello")
public class WebConfig implements WebMvcConfigurer {
@Bean
public ViewResolver viewResolver() {
InternalResourceViewResolver resolver = new InternalResourceViewResolver();
resolver.setViewClass(JstlView.class);
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
}
控制器页面:
@Controller
public class JSPController {
@GetMapping("/jspPage")
public String home(){
return "jspPage";
}
}
jsp page 位置:
application.properties 文件的内容:
spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
使用默认模板引擎,页面显示正确,但使用jsp,就不行了
日志错误:
https://hastebin.com/lijekesoti.apache
注意:我知道 Thymleaf 是 Spring 的推荐模板,但出于某种原因我想使用 JSP
更新
在paulsm4答案的帮助下阅读this post后,删除以下行:
compile("org.springframework.boot:spring-boot-starter-thymeleaf")
删除视图解析器文件解决了我的问题。
【问题讨论】:
-
您忘记显示最重要的信息,即
views文件夹中的文件。那里有jspPage.jsp文件吗? -
@Andreas 是的,有一个 JSP 文件,只是修复了屏幕截图并添加了指向其内容的链接。
-
问:这到底是怎么回事?你解决问题了吗?我的回复对您的解决方案有帮助和/或对应吗?
标签: java spring spring-mvc jsp spring-boot