如题,在学习springboot整合jsp页面时,发现配置了如下配置
spring.mvc.view.prefix=/WEB-INF/view/
spring.mvc.view.suffix=.jsp
调试页面会出现页面找不到的情况。报错大致为:
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Thu Apr 21 00:44:20 CST 2022
There was an unexpected error (type=Not Found, status=404).
No message available
网上各种方案,包括edit configurations下添加working directory,还是configuration类里面添加如下方法:
@Bean public WebServerFactoryCustomizer<TomcatServletWebServerFactory> customizer() { return (factory) -> { factory.addContextCustomizers(context -> { String relatePath = "src/main/webapp"; context.setDocBase(new File(relatePath).getAbsolutePath()); }); }; }
都是不行的。但是我debug,发现到了我的controller里面了,而且最后console还打印了页面地址:
Path with "WEB-INF" or "META-INF": [WEB-INF/view/page.jsp]。
依然找了各种答案,最后看到一个文章说springboot默认不使用jsp,需要在pom.xml中引用jsp的编译程序。然后我引入了依赖项,重新编译启动后顺利成功。
依赖项如下:
<!--jsp页面使用jstl标签 --> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> </dependency> <!--用于编译jsp --> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <scope>provided</scope> </dependency>
任何一个小细节都可能导致失败呀。