thymeleaf模板引擎的运用
1.先导入依赖(spring boot 框架)
2. controller层 (用model绑定值)
public String get(Long id,Model model) {
Student student = iStudentService.get(id);
model.addAttribute(“student”,student);
return “showById”;
}
3. 前端页面接受值并赋值给表单(语法: th:value="
s
t
u
d
e
n
t
.
n
a
m
e
"
)
姓
名
:
<
i
n
p
u
t
c
l
a
s
s
=
"
b
"
t
y
p
e
=
"
t
e
x
t
"
i
d
=
"
n
a
m
e
"
t
h
:
v
a
l
u
e
=
"
{student.name}") 姓名: <input class="b" type="text" id="name" th:value="
student.name")姓名:<inputclass="b"type="text"id="name"th:value="{student.name}">
前端页面接受值并循环展示列表
<tr th:each="stu : ${student}">
<td th:text="${stu.id}"></td>
<td th:text="${stu.name}"></td>
<td th:text="${stu.age}"></td>
<td th:text="${stu.weight}"></td》
</tr>
4.thymeleaf拼接超链接语法
例:点击按钮跳转页面,同时带走这一行的id值(通过id 执行selectById语句,显示在页面上)
<td><a th:href="@{/toupd(id=${stu.id})}">修改</a></td> (toupd是方法名,stu是模板定义的对象)
百度的语法:<dd><a th:href="@{/get/{id}(id=${user.id})}">基本资料</a></dd>
字符串模板
语法:String text
(两个反单引号)
String text ${expression} string text string text line2 ${expression} string text
多行字符串并且取表达式的值
例:var a=20;
var b="jquery";
var str = `My age is $(a) and i love ${c}`; console.log(str);
原始方法:var str = “My age is” + a “and i love ” + c`; console.log(str);