Spring Boot 4 Not Rendering JSPs throwing 404

2019-05-20 04:50发布

问题:

I have the following project structure

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

I want to do something similar to this but I have the following in my controller

@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";
    }
}

and when I go to http://localhost:8080/my/home I get the log message and a 404. I thought in spring 4 I did not need the view resolver but if I do, how do I configure it.

Update

I created a application.properties with the following...

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

But it is still not working.

Update 2

The provided Spring sample also seems to fail in the same way with the following 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'
}

Update

I also did try to be explicit...

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

This doesn't appear to work either.

回答1:

Adding this to the build.gradle seems to fix the issue...

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'


回答2:

I got the same problem and solved with adding this piece to pom.xml:

    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>

I don't under stand why ...



回答3:

When you add following dependencies, you may not have any error. It worked for me.

As far as I got , jsp page is not rendered with embedded tomcat if you don't add some dependencies.

<dependencies>
     <dependency>
         <groupid>org.springframework.boot</groupid>
         <artifactid>spring-boot-starter-web</artifactid>
     </dependency>
     <dependency>
         <groupid>org.apache.tomcat.embed</groupid>
         <artifactid>tomcat-embed-jasper</artifactid>
         <scope>provided</scope>
     </dependency>
     <dependency>
         <groupid>javax.servlet</groupid>
         <artifactid>jstl</artifactid>
     </dependency>
   </dependencies>


回答4:

Created a template for the same and shared on Github https://github.com/nagasrinu88/spring-boot-template

<dependency>
   <groupId>org.apache.tomcat.embed</groupId>
   <artifactId>tomcat-embed-jasper</artifactId>            
</dependency>

Got the same Issue and solved by adding the above lines using spring boot 1.4.0.RELEASE



回答5:

Do you have an InternalViewResolver defined in your spring configuration?

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
    <property name="prefix">
        <value>/WEB-INF/jsp/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>