Spring Boot2.x 整合Thymeleaf模板

  • 添加依赖
 <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
      <version>2.0.1.RELEASE</version>
 </dependency>
复制代码
  • 默认路径下添加模板 src/main/resources/templates

index.html

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="UTF-8">
  <!-- 可以看到 thymeleaf 是通过在标签里添加额外属性来绑定动态数据的 -->
  <title th:text="${title}">Title</title>
  <!-- 在/resources/static/js目录下创建一个hello.js 用如下语法依赖即可-->
  <script type="text/javascript" th:src="@{/js/hello.js}"></script>
</head>
<body>
<h1 th:text="${desc}">Hello World</h1>
<p th:text="${person?.name}"></p>
<p th:text="${person?.age}"></p>
<button onclick="click(this)">点击</button>
</body>
</html>
复制代码
  • 编写控制器
@Controller
public class ThymeleafController {


	@RequestMapping("index2")
	public String index(HttpServletRequest model){

		model.setAttribute("title","你阿豪");
		model.setAttribute("desc","描述");


		Person person = new Person();

		person.setName("zhangsan");
		person.setAge(14);

		model.setAttribute("person",person);

		return "index";
	}


	class Person{
		private String name;
		private int age;

		public String getName() {
			return name;
		}

		public void setName(String name) {
			this.name = name;
		}

		public int getAge() {
			return age;
		}

		public void setAge(int age) {
			this.age = age;
		}
	}


}
复制代码

转载于:https://juejin.im/post/5cb426b76fb9a0688144f318

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值