Spring Boot 整合 JSP
Spring Boot 与视图层的整合:
1、JSP
2、Thymeleaf
Java Server Page,是 Java 提供的⼀种动态网页技术,底层是 Servlet,可以直接在 HTML 中插⼊ Java代码。
JSP 底层原理:
JSP 是⼀种中间层组件,开发者可以在这个组件中将 Java 代码与 HTML 代码进行整合,由 JSP 引擎将组件转为 Servlet,再把开发者定义在组件中的混合代码翻译成 Servlet 的响应语句,输出给客户端。
1、创建基于 Maven 的 Web 项目,pom.xml
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.4.RELEASE</version>
</parent> <dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.2.4.RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<version>9.0.19</version>
</dependency>
</dependencies>
2、创建 Handler
package com.southwind.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
@RequestMapping("/hello")
public class HelloHandler {
@GetMapping("/index")
public ModelAndView index(){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
modelAndView.addObject("mess","Hello Spring Boot");
return modelAndView;
}
}
3、JSP
<html> <head>
<title>Title</title>
</head> <body>
<h1>Index</h1>
${mess}
</body>
</html
4、application.yml
server:
port: 8181
spring:
mvc:
view:
prefix: /
suffix: .jsp
5、Application
package com.southwind;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args