springboot 使用JSP


springboot 使用JSP

1. 添加依赖

<!-- 引入 springboot 内嵌的 tomcat 对 JSP 的解析包 -->
<dependency>
    <groupId>org.apache.tomcat.embed</groupId>
    <artifactId>tomcat-embed-jasper</artifactId>
</dependency>

<!-- servlet 依赖的 jar 包 start -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
</dependency>

<dependency>
    <groupId>javax.servlet.jsp</groupId>
    <artifactId>javax.servlet.jsp-api</artifactId>
    <version>2.3.1</version>
</dependency>

<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>jstl</artifactId>
</dependency>

2. 创建 src/main/webapp 文件夹,在 application.properties 文件中配置springMVC 视图展示为 jsp

#前缀
spring.mvc.view.prefix=/
#后缀
spring.mvc.view.suffix=.jsp

3. 在 webapp 下创建 jsp 文件

这里访问页面时会出现如下页面:
springboot 使用JSP

产生这个的原因是 idea 没有将jsp文件编译到 classes 中:
springboot 使用JSP

<build> </build> 中添加配置信息,将 webapp 下的所有文件编译到 META-INF/resources 下:

<resource>
    <directory>src/main/webapp</directory>
    <targetPath>META-INF/resources</targetPath>
    <includes>
        <include>**/*.*</include>
    </includes>
</resource>

添加后编译结果:
springboot 使用JSP

4. 成功访问

springboot 使用JSP