简体   繁体   中英

Spring-Boot Not Finding JSP Pages in WAR File

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!

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>

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.

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();
}

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM