1

I have a spring boot app with a jsp in src/main/webapp/WEB-INF/view/index.jsp

I have an MVC config:

@Configuration
public class MSMVCConfigurtionBeans extends WebMvcConfigurerAdapter {

    @Override
    public void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/res/**").addResourceLocations("/static/");
    }

    @Bean()
    DispatcherServlet dispatcherServlet() {
        DispatcherServlet dispatcherServlet = new DispatcherServlet();
        return dispatcherServlet;
    }

    @Bean()
    ServletRegistrationBean dispatcherServletRegistration() {
        ServletRegistrationBean registration = new ServletRegistrationBean(dispatcherServlet(), "/");
        registration.setName("MVCDispatcher");
        return registration;
    }

    @Bean()
    ViewResolver viewResolver(){
        InternalResourceViewResolver irvr = new InternalResourceViewResolver();
        irvr.setPrefix("/WEB-INF/view/");
        irvr.setSuffix(".jsp");
        return irvr;
    }
}

I run gradle bootRun and load the index page and see this in the log:

2018-01-17 13:36:17.623  INFO 50410 --- [nio-8080-exec-1] com.s.IndexController     : Running index from MVC, returning index view
2018-01-17 13:36:17.625 DEBUG 50410 --- [nio-8080-exec-1] o.s.w.servlet.view.BeanNameViewResolver  : No matching bean found for view name 'index'
2018-01-17 13:36:17.636 DEBUG 50410 --- [nio-8080-exec-1] o.s.w.servlet.view.InternalResourceView  : Forwarding to resource [/WEB-INF/view/index.jsp] in InternalResourceView 'index'

The first message is generated by my controller:

    @RequestMapping({"/", "/index"})
    public String index(){
        LOGGER.info("Running index from MVC, returning index view");
        return "index";
    }

Here's my gradle build file:

buildscript {
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.9.RELEASE")
    }
}

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: "jacoco"
apply plugin: 'war'

jar {
    baseName = 'i-rest'
    version = '0.1.1'
}

repositories {
    mavenCentral()
    maven {
        url "https://dl.bintray.com/michaelklishin/maven/"
    }
}

sourceCompatibility = 1.8
targetCompatibility = 1.8

dependencies {
    compile("org.springframework.boot:spring-boot-starter-data-rest")
    compile("org.springframework.boot:spring-boot-starter-validation")
    compile("org.springframework.boot:spring-boot-starter-data-mongodb")
    compile("org.springframework.boot:spring-boot-starter-security")

    compile group: 'org.hibernate', name: 'hibernate-validator', version: '5.4.2.Final'
    compile("org.springframework:spring-tx")
    compile 'org.springframework.security:spring-security-web'
    compile("org.springframework:spring-context-support")
    compile("org.quartz-scheduler:quartz:2.2.1")
    compile("org.projectlombok:lombok")
    compile("com.novemberain:quartz-mongodb:2.0.0")
    testCompile("org.springframework.boot:spring-boot-starter-test")
}

ls -al src/main/webapp/WEB-INF/view/
total 8
drwxr-xr-x  3 mike  staff   96 Jan 16 19:16 .
drwxr-xr-x  3 mike  staff   96 Jan 17 13:22 ..
-rw-r--r--  1 mike  staff  751 Jan 15 13:38 index.jsp

So - I have my controller being called and it's printing the jsp that it's dispatching to (which exists), why the 404? Am I missing something in gradle that is not copying the WEB-INF directory to the pwd for the webapp that's running? With Maven - the webapp directory gets put into target/ so it is on the classpath. What's missing?

EDIT

I added the following dependencies to the build.gradle file to resolve this:

compile("org.apache.tomcat.embed:tomcat-embed-jasper")
compile("javax.servlet:jstl")

See also: Spring Boot JSP 404

mikeb
  • 10,578
  • 7
  • 62
  • 120

1 Answers1

-1

Instead of

return "index"

As a String, can you try something like below

@RequestMapping({"/", "/index"}) 
 public ModelandView index(){ 
     LOGGER.info("Running index from           MVC, returning index view"); 
    return new ModelandView("index"); 
 }
gkrthk
  • 331
  • 1
  • 7
  • I have tried that, not to mention they are both the same in this context. See here https://stackoverflow.com/questions/7175509/which-is-better-return-modelandview-or-string-on-spring3-controller#7175586 – mikeb Jan 17 '18 at 19:37