Java Spring-Boot 在 WAR 文件中找不到 JSP 页面
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20199609/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring-Boot Not Finding JSP Pages in WAR File
提问by node42
When running a spring-boot project (java -jar /path/to/war.war) .jsp files are not found.
运行 spring-boot 项目 (java -jar /path/to/war.war) 时,找不到 .jsp 文件。
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.
用@ResponseBody 注释的方法工作正常。视图解析器提供了 JSP 页面的正确路径,但未找到它们。这个项目只有一个配置类,没有 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
2013-11-25 09:08:28.714 错误 1549 --- [tp1945397783-20] org.apache.jasper.servlet.JspServlet:PWC6117:文件“%2FUsers%2Fnode42%2FDevelopment%2Flib2Fmock %2Fmock-security-ui-2.06-SNAPSHOT.war%2FWEB-INF%2Fjsp%2Falivepage.jsp”未找到
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.
日志条目中.war文件的路径正确,war文件(WEB-INF/jsp/alivepage.jsp)中的路径正确。无论使用 Jetty 还是 Tomcat,响应都是相同的(以上日志来自 Jetty)。我也试过不使用视图解析器,如上指定一个,或通过属性设置视图解析器。我完全不知所措,因为除了这个小细节之外,一切看起来都在工作。控制器中的@ResponseBody 注释方法工作正常。
If anyone has any suggestions I'd certainly appreciate the input!
如果有人有任何建议,我当然会感谢您的投入!
回答by Dave Syer
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 Bootif you want something to compare - works up to a point (the JSP is resolved and rendered).
我认为 Spring Boot 中还没有完全支持可执行存档中的 JSP(它在列表中),所以我会尝试首先使用 a) 部署的 WAR,和/或 b) 分解的存档(或运行从 IDE),或从源代码运行。一旦它起作用了,您可能仍然需要等待添加完整的 JSP 支持(欢迎贡献),但至少您会知道它是有效的。您在部署的 WAR(无映射)中看到的错误表明还有其他事情发生。请注意,如果您想要比较某些内容,Spring Boot中有一个JSP 示例- 工作到一定程度(JSP 已解析和呈现)。
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.
编辑:Spring taglibs、JSTL 和 EL 支持似乎在上面的示例中起作用。我刚刚更新它以添加 JSTL 并从 IDE 和作为可执行的 WAR 进行测试。
回答by Nick Spacek
I was having a similar problem, caused by the default servlet not being mapped. I had to do this in my extends DelegatingWebMvcConfiguration
class:
我遇到了类似的问题,这是由未映射默认 servlet 引起的。我必须在extends DelegatingWebMvcConfiguration
课堂上这样做:
@Override
protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
回答by Sezin Karli
I don't know your pom.xml but this seems similar to this thread
我不知道你的 pom.xml 但这似乎类似于这个线程
JSP file not rendering in Spring Boot web application
JSP 文件未在 Spring Boot Web 应用程序中呈现
Try adding dependency list to your pom please.
请尝试将依赖项列表添加到您的 pom。
回答by Adrian Lopez
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:
Spring Boot 默认不包含 Jasper,因此除非您明确包含该库,否则 JSP 渲染不起作用:
For Gradle:
对于摇篮:
compile("org.apache.tomcat.embed:tomcat-embed-jasper")
For Maven:
对于 Maven:
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
回答by Vijendra Kumar Kulhade
I solved this issue by minor correction in the Bean definition.
我通过对 Bean 定义的小幅修正解决了这个问题。
@Bean
InternalResourceViewResolver internalResourceViewResolver () {
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver()
viewResolver.setPrefix("WEB-INF/jsp/")
viewResolver.setSuffix(".jsp")
return viewResolver;
}