Case
使用spring boot框架开发一个web app时,通常都会引入dependency:spring-boot-starter-web或spring-boot-starter-tomcat,其默认使用tomcat作为web容器。使用该配置作为Spring Boot App运行时,访问Rest API没问题,但是却无法访问JSP页面。
Cause
当工程作为Spring Boot App运行时,根据dependency: spring-boot-starter-web或spring-boot-starter-tomcat使用tomcat作为内置的web服务器,但是由于starter dependency包默认仅包含tomcat-embed-core,tomcat-embed-el,tomcat-core-websocket,并不支持jsp的编译和解析。
Solution
添加tomcat jsp engine: jasper和jstl dependency。
1<dependency> 2 <groupId>org.apache.tomcat.embed</groupId> 3 <artifactId>tomcat-embed-jasper</artifactId> 4 <scope>provided</scope> 5 </dependency> 6 <dependency> 7 <groupId>javax.servlet</groupId> 8 <artifactId>jstl</artifactId> 9 </dependency>