首先,添加加载jsp文件的依赖包:

<!--jsp依赖 对应springboot版本为2.1.4-->
<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>

再添加webapp/WEB-INF/jsp文件夹,如下图:

spring boot jsp位置 springboot怎么用jsp_spring

之后,我们在properties文件中添加

spring.mvc.view.prefix=/
spring.mvc.view.suffix=.jsp

最后一步最重要,在pom.xml文件中的build节点下添加如下内容,谨记:

<resources>
    <resource>
        <directory>src/main/webapp/WEB-INF/jsp</directory>
        <!--如果使用springboot自带的tomcat启动,则使用如下配置jsp路径-->
        <targetPath>META-INF/resources</targetPath>
        <!--如果使用maven启动本地tomcat启动,则使用如下配置-->
        <!--<targetPath>/WEB-INF/jsp</targetPath>-->
        <includes>
            <include>**/*.*</include>
        </includes>
    </resource>
</resources>

注意以上几点后,即可在controller层访问jsp文件了。