1.創建一個maven web項目
2.添加springboot和jsp相關依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.3.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- jsp依賴 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
</dependency>
</dependencies>
3.在application.properties配置文件中添加配置
spring.mvc.view.prefix=/WEB-INF/jsp/
spring.mvc.view.suffix=.jsp
4.創建一個controller
@EnableAutoConfiguration
@ComponentScan(basePackages="com.vhukze.controller")
@RestController
public class JspDemoController {
@RequestMapping("/toJsp")
public ModelAndView toJsp() {
return new ModelAndView("index");
}
public static void main(String[] args) {
SpringApplication.run(JspDemoController.class, args);
}
}
5.在配置的路徑下新建jsp文件
<html>
<body>
<h2>Hello World!</h2>
</body>
</html>
6.啓動項目訪問controller映射路徑