Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
464 views
in Technique[技术] by (71.8m points)

java - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...