场景
springboot不建议使用jsp,使用模板引擎,比如thymeleaf,velocity,freemarker。
项目搭建专栏:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/column/info/35688
实现
在项目中引入相关依赖
<!-- springboot整合thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
springboot会默认在resource下的templates下去寻找模板。
新建test.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<h1 th:text="${name}"></h1>
</body>
</html>
在Controller包下新建TestThymeleaf .java
package com.example.demo.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestFreeMarker {
@RequestMapping("/freemarker")
public String show(Model model) {
model.addAttribute("name","霸道流氓气质");
return "show";
}
}
运行效果
源码下载
https://download.csdn.net/download/badao_liumang_qizhi/11089093