我们都知道springboot项目创建之后在web依赖之中已经内嵌了tomcat 即实现了对servelet的支持,但是官方的前端模板是themleaf 不建议使用jsp 因为jsp效率比较低. 而如果我们想使用jsp 那么需要引入支持jsp的依赖:
注意: jsp页面的路径必须是在webapp下面 themleaf的默认跟路径是在sources/templates下面; 还有配置application.property中 配置 spring.mvc.view.prefix=/ spring.mvc.view.suffix=.jsp
<dependency> <!-- 支持jsp的依赖-->
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<! --jsp的标签库支持-->
<dependency>
<groupId>javax.servlet.jsp.jstl</groupId>
<artifactId>jstl-api</artifactId>
<version>1.2</version>
</dependency>
之所以scope用的是provided 即在本地运行的时候有效, 是因为在maven-plugin 进行编译的时候会自动加入这个依赖如果需要的话.
一般在war包时对内嵌的tomcat进行如下处理:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>