Spring Boot 4不渲染抛出404的JSP


问题内容

我有以下项目结构

-src
  -main
    -java
      -com
        -test
          Application.java
          -controllers
            MyController.java
    -webapp
      -WEB-INF
        -jsp
          main.jsp

我想要做类似的东西这个,但我有我的控制器以下

@Controller
@RequestMapping("/my/**")
public class MyController {
    @RequestMapping("/home")
    public String loadHomePage(Model m) {
        m.addAttribute("name", "CodeTutr");
        System.out.println("Test the view controller");
        return "main";
    }
}

当我去的时候,http://localhost:8080/my/home我得到了日志消息和一个404。我以为在spring4中,我不需要视图解析器,但是如果需要的话,如何配置它。

更新资料

我创建了一个application.properties与以下内容…

spring.view.prefix: /WEB-INF/jsp/
spring.view.suffix: .jsp
application.message: Hello Phil

但是它仍然无法正常工作。

更新2

提供的Spring示例似乎也无法通过以下build.gradle …失败。

buildscript {
  repositories {
    mavenLocal()
    mavenCentral()
    maven { url "http://repo.spring.io/snapshot" }
    maven { url "http://repo.spring.io/milestone" }
  }
  dependencies {
    classpath("org.springframework.boot:spring-boot-gradle-plugin:1.1.4.BUILD-SNAPSHOT")
  }
}
apply plugin: 'war'
apply plugin: 'spring-boot'
apply plugin: 'groovy'
war { baseName='itext' }
repositories {
  mavenLocal()
  mavenCentral()
  maven { url "http://repo.spring.io/snapshot" }
  maven { url "http://repo.spring.io/milestone" }
  maven { url "http://repo.spring.io/libs-release" }
}
dependencies {
  compile("org.springframework.boot:spring-boot-starter-web")
  compile("org.springframework.boot:spring-boot-starter-websocket")
  compile("org.springframework:spring-messaging")
  providedRuntime("org.springframework.boot:spring-boot-starter-tomcat")
  testCompile("org.springframework.boot:spring-boot-starter-test")
  compile 'org.codehaus.groovy:groovy-all:2.2.0'
  compile 'com.lowagie:itext:4.2.1'
  compile 'com.google.code.gson:gson:2.2.4'
}

更新资料

我也确实尝试过明确…

@Bean
public ViewResolver getViewResolver(){
    InternalResourceViewResolver resolver = new InternalResourceViewResolver();
    resolver.setPrefix("/WEB-INF/jsp/");
    resolver.setSuffix(".jsp");
    return resolver;
}

这似乎也不起作用。


问题答案:

将此添加到build.gradle似乎可以解决此问题…

compile 'com.google.code.gson:gson:2.2.4'
compile 'javax.servlet.jsp.jstl:jstl:1.2',
      'taglibs:standard:1.1.2'
providedRuntime 'org.apache.tomcat.embed:tomcat-embed-jasper:8.0.8'