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

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

问题描述

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

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

配置类:

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


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

System.out.println(让我们来看看Spring Boot提供的bean:);

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



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

控制器

  @Controller 
class运行{

@RequestMapping(/ alive)// This work fine
@ResponseBody
String amIAlive(){
活!
}


@RequestMapping(/ alive / page)//解决页面路径,但找不到文件!
ModelAndView amIAlivePage(){
New ModelAndView(alivepage)
}
}

错误日志


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

日志条目中.war文件的路径是正确,并且war文件(WEB-INF / jsp / alivepage.jsp)中的路径是正确的。无论使用Jetty还是Tomcat(上述日志来自Jetty),响应都是相同的。我也尝试不使用视图解析器,指定一个如上,或通过属性设置视图解析器。除了这个小小的细节之外,所有的东西看起来都像是在工作,我完全无法接受。

如果有人有任何建议,我一定会喜欢这个输入!

解决方案

我有同样的问题,在我的情况下,它是因为我在类路径中缺少一个库。



Spring Boot不包含Jasper作为默认设置,因此除非您明确包含库,否则JSP呈现不起作用:



对于Gradle:

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

 <$>  

C $ C><依赖性>
< groupId> org.apache.tomcat.embed< / groupId>
< artifactId> tomcat-embed-jasper< / artifactId>
< scope>提供< / scope>
< /依赖关系>


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>

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

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