Spring Boot 无法访问JSP页面

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。

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

评论

暂无评论