SpringBoot整合JSP(支持return和直接访问jsp)
环境
SpringBoot版本,java版本1.8,spring-boot-maven-plugin:1.4.2.RELEASE
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/>
</parent>
<packaging>jar</packaging> # 这里也可以不写
修改pom文件
添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
<!--配置jsp jstl的支持-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
添加编译插件
<!-- springboot 编译插件-->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>1.4.2.RELEASE</version>
<configuration>
<fork>true</fork>
<addResources>true</addResources>
<mainClass>com.mirac.AppServerApplication</mainClass><!--主类的路径-->
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
<!-- 指定资源文件的位置,否则maven打包的时候不会jsp页面打包进jar -->
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/**</include>
</includes>
</resource>
<resource>
<directory>src/main/webapp</directory>
<targetPath>META-INF/resources</targetPath>
<includes>
<include>**/**</include>
</includes>
</resource>
</resources>
</build>
创建webapp
配置文件
配置文件默认不写,也可以通过配置spring.mvc.view.prefix/suffix
并调整文件位置。
运行并访问
return方式:直接输入:localhost:port进入index.jsp页面
直接访问方式:浏览器输入:http://localhost:port/index.jsp
注意
注意检查打包后的资源文件,META-INF/resources文件夹和static文件夹都是springboot支持的直接访问资源的路径