thymleaf用法例子
原创
©著作权归作者所有:来自51CTO博客作者dream666uping的原创作品,请联系作者获取转载授权,否则将追究法律责任
学习视频:https://www.bilibili.com/video/BV1gW411W76m?t=1978&p=31
之前的网页开发。为了进行数据交互。必须整成jsp页面!然而,springboot不支持jsp.
thymleaf,就是为了弥补上面这个缺点,可以直接与HTML页面交互数据!
最常用的就是
th:text="${}" 这个不支持转义!
th:utext="${}"这个支持转义!
由于thymleaf,会自动解析templates下面的静态文件。
所以,一定要注意文件命名!
接下来:
HelloController:
package com.swq.springbootwebcrud.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;
@Controller
public class HelloController {
// @ResponseBody
// @RequestMapping("/swq")
// public String hello(){
// return "hello swq";
// }
@RequestMapping("/swq")
public String success(Map<String, Object> map){
map.put("hello","<h1> 大哥!</h1>");
map.put("users", Arrays.asList("xiao","pang","hu"));
return "swq";
}
}
swq.html:【解释代码意思,都放在代码里面啦!】
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>succ</title>
</head>
<body>
<hr>
SWQ!
<div th:text="${hello}"></div>
<hr>
<div th:utext="${hello}"></div>
<hr>
<!--th:each每次遍历都会生成当前这个标签!-->
<h4 th:text="${user000}" th:each="user000:${users}"></h4>
<!--th:text="${user000}"--将后面的变量对应的值,放在h4标签中。是显示出来的东西!-->
<hr>
<h4>
<span th:each = "user:${users}">[[${user}]]你好!</span>
<!-- [[${user}]]:行内写法,另外一种取出数值的方法!-->
</h4>
</body>
</html>
上面就是说了一下两种用法!
运行截图:
另外的用法:
效果: