Spring-Boot 在 WAR 文件中找不到 JSP 页面 [英] Spring-Boot Not Finding JSP Pages in WAR File

查看:26
本文介绍了Spring-Boot 在 WAR 文件中找不到 JSP 页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行 spring-boot 项目时 (java -jar/path/to/war.war) 找不到 .jsp 文件.

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

用@ResponseBody 注释的方法工作正常.视图解析器提供了 JSP 页面的正确路径,但未找到它们.这个项目只有一个配置类,没有web.xml.

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

错误日志

2013-11-25 09:08:28.714 错误 1549 --- [tp1945397783-20] org.apache.jasper.servlet.JspServlet:PWC6117:文件%2FUsers%2Fnode42%2FockDevelopment%2Fm2Fbuild%2Flibs%2Fmock-security-ui-2.06-SNAPSHOT.war%2FWEB-INF%2Fjsp%2Falivepage.jsp"未找到

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

日志条目中.war文件的路径正确,war文件(WEB-INF/jsp/alivepage.jsp)中的路径正确.无论使用 Jetty 还是 Tomcat,响应都是相同的(以上日志来自 Jetty).我也试过不使用视图解析器,如上指定一个,或通过属性设置视图解析器.我完全不知所措,因为一切实际上看起来都在工作,除了这个小细节.并且控制器中的@ResponseBody 注释方法工作正常.

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 默认不包含 Jasper,因此除非您明确包含该库,否则 JSP 渲染不起作用:

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

对于 Gradle:

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

对于 Maven:

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

这篇关于Spring-Boot 在 WAR 文件中找不到 JSP 页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆