【问题标题】:Having issue displaying JSP page in Spring-MVC在 Spring-MVC 中显示 JSP 页面时出现问题
【发布时间】: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


【解决方案1】:

事实证明,让 JSP 与 Spring Boot 一起工作并非易事。事实证明,Spring Boot 1.x(Spring Boot/JSP 的大部分教程都写到了)和 Spring Boot 2.x 之间也发生了重大变化。

我发现这些资源很有帮助:

我让 JSP 与 Spring Boot 1.x 和 2.x 以及 Maven 和 Gradle 一起工作。我的项目在 GitHub 上:

这些是我需要做的重点:

  1. 我使用 Eclipse STS 创建了我的启动项目。

    指定“War”包装很重要(与默认的“Jar”相比)

  2. 我在build.gradle 中添加了以下依赖项:

    dependencies {
      compile('org.springframework.boot:spring-boot-starter-web')
      compile('javax.servlet:jstl')
      compile('javax.servlet:javax.servlet-api')
      compile('org.apache.tomcat.embed:tomcat-embed-jasper')
      compile('org.webjars:bootstrap:4.1.0')
      testImplementation('org.springframework.boot:spring-boot-starter-test')
    

    原来'tomcat-embedded'需要被指定(它默认包含在spring-boot-starter-web中)。

    但事实证明,除非您明确包含 tomcat-embed-jasper,否则 Embedded Tomcat 不会处理 JSP。

    不要指定“thymeleaf”依赖项——它会与“jasper”冲突。

  3. 根据其他教程,我在application.properties 中添加了这些行:

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

    我还添加了这些行到我的根类中:

    @SpringBootApplication
    public class Test7Application extends SpringBootServletInitializer {
       ...  
       @Override
       protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
          return application.sources(Test7Application.class);
       }
    
       public static void main(String[] args) {
          SpringApplication.run(Test7Application.class, args);
       }
    
  4. 不幸的是,“其他教程”经常说要创建文件夹src/main/webapp/WEB-INF/jsp/。这将工作。

    相反,我将我的 test.jsp 文件放在文件夹 src/main/resources/META-INF/resources/WEB-INF/jsp 中。

    这些链接解释了原因:

  5. 此时,我已经成功地能够使用 Spring Boot 显示静态页面和 .jsp 页面了。

希望对你有帮助!

【讨论】:

  • 1.我猜我们正在同时编辑帖子,所以我可能会意外删除您的编辑。随意编辑它,我不想包含它,因为帖子已经太长了。我不确定你在 3.b 和 3.c 中的意思
  • 如果还不清楚,那么阅读教程可能会帮助您更好地理解 3b 和 3c。
  • 我可以将 JSP 文件放在两个不同的文件夹中吗? “资源 > 静态”和“webapp > jsp”?
  • 我想我做到了,只是使用了 views 文件夹而不是 jsp 文件夹。我错了吗?
  • 不,您的文件夹是“webapps > WEB-INF > views”。我建议“webapps > 视图”。但更重要的是,请浏览我引用的链接(如果您还没有的话)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-21
  • 2022-11-15
  • 2020-06-10
  • 2011-08-09
  • 1970-01-01
相关资源
最近更新 更多