6

When running a spring-boot project (java -jar /path/to/war.war) .jsp files are not found.

Methods annotated with @ResponseBody work fine. The view resolver is coming up with the correct path to the JSP pages, but they are not found. This project has one configuration class and no web.xml.

Configuration Class:

@Configuration
@EnableAutoConfiguration
@EnableWebMvc
@ComponentScan (basePackages = "org.ghc.security.web")
class ScMain extends WebMvcConfigurerAdapter {


    // SpringBoot BootStrap...
    static void main (String[] args) {
        ApplicationContext ctx = SpringApplication.run (ScMain, args)

        System.out.println("Let's inspect the beans provided by Spring Boot:");

        String[] beanNames = ctx.getBeanDefinitionNames();
        Arrays.sort(beanNames);
        beanNames.each { beanName ->
            System.out.println(beanName);
        }
    }


    @Bean
    InternalResourceViewResolver internalResourceViewResolver () {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver()
        viewResolver.setPrefix("/WEB-INF/jsp/")
        viewResolver.setSuffix(".jsp")
        viewResolver
    }
}

Controller

@Controller
class Running {

    @RequestMapping ("/alive")  // This works fine
    @ResponseBody
    String amIAlive () {
        "ALIVE!"
    }


    @RequestMapping ("/alive/page")  // Path to page resolved, but file not found!
    ModelAndView amIAlivePage () {
        new ModelAndView("alivepage")
    }
}

Error Log

2013-11-25 09:08:28.714 ERROR 1549 --- [tp1945397783-20] org.apache.jasper.servlet.JspServlet : PWC6117: File "%2FUsers%2Fnode42%2FDevelopment%2Fmock-security-ui%2Fbuild%2Flibs%2Fmock-security-ui-2.06-SNAPSHOT.war%2FWEB-INF%2Fjsp%2Falivepage.jsp" not found

The path to the .war file in the log entry is correct, and the path in the war file (WEB-INF/jsp/alivepage.jsp) is correct. The response is the same whether using Jetty or Tomcat (the above log was from Jetty). I have also tried not using the view resolver, specifying one as above, or setting the view resolver through properties. I am completely flummoxed as everything actually looks like it is working, except for this one little detail. And the @ResponseBody annotated method in the controller works fine.

If anyone has any suggestions I'd certainly appreciate the input!

node42
  • 695
  • 4
  • 10
  • 19
  • Try extracting the war and running the program from there. – Sotirios Delimanolis Nov 25 '13 at 18:12
  • It is a fair question. Details to follow, but the result yields: `2013-11-25 11:35:37.967 WARN 4924 --- [nio-8080-exec-1] o.s.web.servlet.PageNotFound : No mapping found for HTTP request with URI [/WEB-INF/jsp/alivepage.jsp] in DispatcherServlet with name 'dispatcherServlet'` – node42 Nov 25 '13 at 19:37
  • The command line (abbreviated) was `java -cp .:WEB-INF/lib/validation-api-1.0.0.GA.jar:WEB-INF/classes org/springframework/boot/loader/WarLauncher` where there were a lot more WEB-INF/lib entries. – node42 Nov 25 '13 at 19:41
  • I think you should [register an issue](https://github.com/spring-projects/spring-boot/issues?page=1&state=open). My guess something is escaping the URL (notice the escaped / -> %2F) which screws the path. I had this problem a while ago but only on Jetty not on Tomcat. – M. Deinum Nov 26 '13 at 07:10
  • 1
    I was having the same problem and [this answer](http://stackoverflow.com/a/20602011/2047962) helped me fix my problems. – RustyTheBoyRobot May 13 '14 at 04:13

5 Answers5

18

I had the same issue and in my case it happened because I was missing a library in the classpath.

Spring Boot does not include Jasper as default and therefore JSP rendering doesn't work unless you explicitly include the library:

For Gradle:

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

For Maven:

<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
    <scope>provided</scope>
</dependency>
Adrian Lopez
  • 1,776
  • 1
  • 17
  • 35
  • Thank you. Gradle with version: `compile group: 'org.apache.tomcat.embed', name: 'tomcat-embed-jasper', version: '9.0.0.M26'` – Edgard Leal Sep 25 '17 at 00:37
3

I don't think JSPs in executable archives are fully supported yet in Spring Boot (it's on the list), so I would try and make it work first with a) a deployed WAR, and/or b) an exploded archive (or running from the IDE), or running from source. Once that is working you might still have to wait for the full JSP support to be added (contributions welcome), but at least you will know that it works. The error you are seeing in the deployed WAR (no mapping) suggests that there is something else going on. Note that there is a JSP sample in Spring Boot if you want something to compare - works up to a point (the JSP is resolved and rendered).

Edit: Spring taglibs, JSTL and EL support seem to be working in the sample above. I just updated it to add JSTL and tested from IDE and as an executable WAR.

Dave Syer
  • 56,583
  • 10
  • 155
  • 143
2

I was having a similar problem, caused by the default servlet not being mapped. I had to do this in my extends DelegatingWebMvcConfiguration class:

@Override
protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
    configurer.enable();
}
Nick Spacek
  • 4,717
  • 4
  • 39
  • 42
2

I solved this issue by minor correction in the Bean definition.

@Bean
    InternalResourceViewResolver internalResourceViewResolver () {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver()
        viewResolver.setPrefix("WEB-INF/jsp/")
        viewResolver.setSuffix(".jsp")
        return viewResolver;
    }
Amir Azizkhani
  • 1,662
  • 17
  • 30
Vijendra Kumar Kulhade
  • 2,217
  • 3
  • 15
  • 25
  • I had to do this in order to get my executable war to run correctly when deployed. It would run fine in Intellij otherwise, but not the deployed env. – Daniel Bower Jul 28 '17 at 13:26
0

I don't know your pom.xml but this seems similar to this thread

JSP file not rendering in Spring Boot web application

Try adding dependency list to your pom please.

Community
  • 1
  • 1
Sezin Karli
  • 2,517
  • 19
  • 24