本案例使用IDEA作为开发工具,Springboot的默认视图支持是Thymeleaf,对jsp的支持并不是特别的好。
修订版本:springboot3.4.6+idea2022.2版本配置使用JSP
https://blog.csdn.net/tangshiyilang/article/details/135283704
1、创建项目,项目结构如下
【创建工程】
注意点:此处创建的是maven工程,同时需要选中Create from archetype
【填写创建项目的相关参数】
Groupid:项目的组id,测试的时候可以随意写一个。
Artifactid:工程名称。
version:版本号,一般使用默认即可。
【创建工程时的maven信息】
说明:此处主要是配置maven工程信息,在使用之前都是提前配置好的。
有问题可以查看本博客的中,在idea中如何配置maven。
【创建项目的最后确认信息】
project name:工程名字
Project location:项目的存放地址
【工程结构(包括创建好的文件和类)】
2、导入springboot整合jsp相关包
parent:为springboot父包版本,此处使用1.5.9
核心包是:tomcat-embed-jasper,其他不需要的包可以不要
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- 添加servlet依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<!-- 添加jstl标签库依赖模块 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
<!--添加tomcat依赖模块.-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<!-- 使用jsp引擎,springboot内置tomcat没有此依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
3、创建项目配置文件application.properties文件
【在main中创建resources文件】
创建目录,并给起名字为resources
【目录创建成功后,如下】
【将resources目录转化成资源根路径】
说明:配置成资源根路径后,项目会自动的到此处找寻相关配置文件
【转化成如下图所示】
【在转化后的resources目录中创建项目配置文件--application.properties】
【在配置文件中,配置视图解析器】
说明:需要手动在WEB-INF目录中创建view目录
4、创建控制层代码,用于测试
【在main目录下创建java目录】
【将java目录转化成java根路径】
转化之后的的功能与eclipse中的src功能相同
【转化后效果如下所示,为蓝色图标】
【创建包com.txc.controller】
【在java中创建类StudentController,并放置在com.txc.controller中】
【在类中创建测试代码】
说明:idea开发工具默认的导包快捷键是 alt+enter
test方法是需要测试的方法,return 返回值中的test表示test.jsp页面。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
@EnableAutoConfiguration
public class StudentController {
@RequestMapping("test")
public String test(){
System.out.println("=====springboot整合jsp=====");
return "test";
}
public static void main(String[] args) {
SpringApplication.run(StudentController.class,args);
}
}
5、在view目录中创建测试jsp页面
6、在测试jsp页面中写入测试内容
7、启动springboot工程,并测试程序
【启动springboot项目方式--通过StudentController中的main方法启动工程】
【打开浏览器,通过地址栏访问】
测试地址:http://localhost:8080/test
最后祝大家能够整合成功,有问题可以在评论区私信我。