SpringBoot整合Jsp

SpringBoot与JSP

Springboot 官方并不推荐我们使用jsp,它只支持freemaker、thymeleaf等相关的模板引擎,springboot 更想让开发者们采用前后分离的开发模式,那么如果项目中想要使用jsp,需要如下几步:
1.pom文件

<!-- 支持jsp的jar包 -->
		<dependency>
			<groupId>org.apache.tomcat.embed</groupId>
			<artifactId>tomcat-embed-jasper</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>jstl</artifactId>
			<scope>provided</scope>
		</dependency>
		<dependency>
			<groupId>javax.servlet</groupId>
			<artifactId>javax.servlet-api</artifactId>
			<scope>provided</scope>
		</dependency>
		

2.application.properties
增加以下配置

spring.mvc.view.prefix=/WEB-INF/views/
spring.mvc.view.suffix=.jsp
spring.mvc.static-path-pattern=/static/**

3.在src/java下创建webapp文件,如果项目中没有webapp目录,则需要自己手动创建
SpringBoot整合Jsp
4.编写jsp页面 index.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h1>Hello Wolrd~</h1>
</body>
</html>

5.编写后台代码

@Controller
@RequestMapping("/")
public class IndexController {

	@RequestMapping("/")
	public ModelAndView index() {
		return new ModelAndView("index");
	}

}

6.启动
SpringBoot整合Jsp
如此springboot整合jsp就整合完成了。尽情使用它吧!