(1)首先导入依赖
<!-- 引入jsp依赖 -->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<!-- 引入jstl依赖 -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
(2)在application.yml文件中进行配置 ,下面mvc子树下的配置(标红区域)
spring:
profiles:
active: prod
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://localhost:3306/recruit?useUnicode=true&characterEncoding=UTF-8
username: root
password: admin
jpa:
hibernate:
ddl-auto: update
show-sql: true
mvc:
view:
prefix: /WEB-INF/views/
suffix: .jsp
(3)目录结构如图:
(4)然后启动文件Application里实现SpringBootServletInitializer类并重写SpringApplicationBundler方法
@SpringBootApplication
public class RecruitApplication extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(RecruitApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(RecruitApplication.class, args);
}
}
(5)在对应的controller层中书写代码时,返回值改为与SpringMVC一样的用法,返回String或者ModelAndView进行Jsp文件的跳转,如:
@GetMapping(value = "/getAllStudent")
public ModelAndView getAllStudent(){
ModelAndView modelAndView = new ModelAndView();
List<Student> list = stuService.getAllStu();
modelAndView.addObject("students",list);
modelAndView.setViewName("index");
return modelAndView;
}