导入依赖
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
applocation.yml进行模板配置
spring:
thymeleaf:
cache: false
#默认配置
#prefix: classpath:/templates/
#check-template-location: true
#suffix: .html
#encoding: utf-8
#mode: HTML5
Controller文件引用@Controller注解
@RequestMapping("/test")
public String test(Model model){
User u = userService.getUserInfo(1);
model.addAttribute("msg",u);
return "login";
}
<script th:inline="javascript">
var msg = [[${msg}]]
console.log(msg)
</script>
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta content="text/html;charset=UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>个人详情页</title>
</head>
<body>
<div class="container">
<div class="starter-template">
<h2>个人信息</h2>
<div class="form-group">
<label>姓名</label>
<input type="text" class="form-control" th:value="${msg.userName}" disabled/>
</div>
<div class="form-group">
<label>密码</label>
<input type="text" class="form-control" th:value="${msg.passWord}" disabled/>
</div>
</div>
</div>
</body>
<script th:inline="javascript">
var msg = [[${msg}]]
console.log(msg)
</script>
</html>
<style type="text/css">
body {
padding-top: 15%;
padding-left: 40%;
}
.starter-template {
width: 200px;
padding: 40px 15px;
text-align: center;
border: solid 1px #0f0f0f;
}
</style>