Thymeleaf 语法讲解
命名空间: 需要在html标签中添加该属性
xmlns:th=“http://www.thymeleaf.org”
1 URL表达式处理
在 Thymeleaf 中 URL 表达式的语法格式为
@{url}
1.1 绝对路径
<a th:href="@{http://www.baidu.com}">绝对路径</a>
1.2 相对路径
相对于当前项目的根
<a th:href="@{/show}">相对路径</a>
相对于服务器路径的根
<a th:href="@{~/project2/resourcename}">相对于服务器的根</a>
th:href: <a th:href="@{http://www.baidu.com}">绝对路径:百度一下</a><br>
th:href:<a th:href="@{/show2}">相对路径:使用Controller处理请求</a><br>
2 URL中参数的处理
2.1 处理普通格式URL参数
<a th:href="@{/show3?name=shouhe&age=18}">普通URL传递固定参数</a><br>
<a th:href="@{/show3(name=shouhe,age=18)}">普通URL传递固定参数</a><br>
<a th:href="@{'/show3?name='+${name}+'&age='+${age}}">普通URL传递作用域参数</a><br>
<a th:href="@{/show3(name=${name},age=${age})}">普通URL传递作用域参数</a><br>
2.2 处理restful格式URL参数
基本格式
<a th:href="@{/show/{name}(name=sige)}">restful传递固定参数</a>
<a th:href="@{/show44/{name}(name=sige)}">restful传递固定参数</a><br>
<a th:href="@{/show4/{name}/{age}(name=sige,age=20)}">restful传递固定参数</a><br>
<a th:href="@{/show44/{name}(name=${name})}">restful传递作用域参数</a><br>
<a th:href="@{/show5/{name}(name=${name},age=20)}">restful传递作用域参数,混合模式</a><br>
<a th:href="@{/show5/{name}(name=sige,age=20)}">restful传递固定参数,混合模式</a><br>