thymeleaf中link标签的用法
时间: 2024-01-14 09:04:42 浏览: 28
Thymeleaf中link标签用于创建超链接。
以下是link标签的一些常用属性:
- th:href:指定链接的目标URL,可以是一个URL字符串或Thymeleaf表达式。
- th:hreflang:指定链接的语言。
- th:media:指定链接的媒体类型。
- th:type:指定链接的MIME类型。
- th:title:指定链接的标题,显示在鼠标悬停时。
例如,下面的代码将创建一个指向Google主页的超链接:
```
<a th:href="@{'https://www.google.com/'}">Go to Google</a>
```
还可以使用Thymeleaf表达式来动态生成链接:
```
<a th:href="@{${url}}">Go to URL</a>
```
相关问题
thymeleaf的a标签的${}传数组参数
在Thymeleaf中,可以使用th:each指令来遍历数组中的元素,并将每个元素作为参数传递给a标签。
例如,可以将一个数组作为参数传递给a标签的href属性,如下所示:
```html
<a th:href="@{/path/to/page(param=${array})}" >Link</a>
```
在这个例子中,${array}是一个数组,传递给了名为param的参数。在处理这个链接时,Thymeleaf将自动将数组转换为逗号分隔的字符串,并将其作为参数传递给链接。
thymeleaf的a标签的${}传多个数字例子
假设有两个数字a和b需要传递到thymeleaf的a标签中,可以使用如下方式:
```html
<a th:href="@{/example(a=${a},b=${b})}">Link</a>
```
其中,`@{/example}`是thymeleaf中的URL语法,表示跳转到`example`页面;`(a=${a},b=${b})`是URL参数,表示将`a`和`b`两个变量传递到`example`页面中。
在Controller中接收参数的方式如下:
```java
@GetMapping("/example")
public String example(@RequestParam("a") int a, @RequestParam("b") int b, Model model) {
// 在这里对a和b进行处理
model.addAttribute("result", a + b);
return "example";
}
```
在`example`页面中可以使用`${result}`获取到处理后的结果。