配置pom
springboot2.x之后不推荐使用jsp,所以需要手动加依赖
<!--用于解析jsp页面-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
或者
<!-- jsp访问/jar加载 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
都可以
验证过的springboot版本:2.1.3.RELEASE
2.7.x
<resources>
<!--加载webapp下的资源,比如jsp-->
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
<filtering>false</filtering>
</resource>
<!--加载resources下的资源-->
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
<!--加载java文件夹下的资源,比如xml写在java文件夹下的mapper,或者其他类型的文件资源-->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.json</include>
<include>**/*.ftl</include>
</includes>
</resource>
</resources>
jsp路径问题
这是jsp所在的路径,可以看到是在WEB-INF/jsp下的,那么访问可以这样:
如果不想这么麻烦,只返回success就可以的话,可以在配置文件中配置一下,前后缀:
注意:值不要加双引号
,否则找不到文件
一定要这样:
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp