1

我正在使用 Thymeleaf 阅读页面。

在“编辑页面”中,有一个用于返回“用户列表页面”的“返回”按钮。对我来说奇怪的是这个按钮同时有“href”和“th:href”。 按钮的图像细节

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8"/>
    <title>user</title>
    <link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link>
</head>
<body class="container">
<br/>
<h1>修改用户</h1>
<br/><br/>
<div class="with:80%">
    <form class="form-horizontal"   th:action="@{/edit}" th:object="${user}"  method="post">
        <input type="hidden" name="id" th:value="*{id}" />
        <div class="form-group">
            <label for="userName" class="col-sm-2 control-label">userName</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="userName"  id="userName" th:value="*{userName}" placeholder="userName"/>
            </div>
        </div>
        <div class="form-group">
            <label for="password" class="col-sm-2 control-label" >Password</label>
            <div class="col-sm-10">
                <input type="password" class="form-control" name="password" id="password"  th:value="*{password}" placeholder="Password"/>
            </div>
        </div>
        <div class="form-group">
            <label for="age" class="col-sm-2 control-label">age</label>
            <div class="col-sm-10">
                <input type="text" class="form-control" name="age"  id="age" th:value="*{age}" placeholder="age"/>
            </div>
        </div>
        <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <input type="submit" value="Submit" class="btn btn-info" />
                &nbsp; &nbsp; &nbsp;
                <a href="/toAdd" th:href="@{/list}" class="btn btn-info">Back</a>
            </div>

        </div>
    </form>
</div>
</body>
</html>

很明显,“th:href”是用来返回的。关于属性“href”的功能有什么选择吗?

4

1 回答 1

1

ThymeLeaf 旨在将相同的文件用作您可以在浏览器中查看的原型以及工作模板文件。这在实践中意味着,如果您愿意,您可以在浏览器中打开模板文件而无需实际运行它,它看起来仍然没问题。例如,在这段代码中:

<a href="/toAdd" th:href="@{/list}" class="btn btn-info">Back</a>

如果您直接在浏览器中打开文件,浏览器将忽略th:href(因为它不知道如何处理它)而使用href="/toAdd". 但是,当您通过服务器上的模板引擎运行它时,href="/toAdd"将替换为动态表达式的结果th:href="@{/list}"

这用表格更容易显示。像这样:

<table>
  <tr>
    <th>NAME</th>
    <th>PRICE</th>
    <th>IN STOCK</th>
  </tr>
  <tr th:each="prod : ${prods}" th:class="${prodStat.odd}? 'odd'">
    <td th:text="${prod.name}">Onions</td>
    <td th:text="${prod.price}">2.41</td>
    <td th:text="${prod.inStock}? #{true} : #{false}">yes</td>
  </tr>
</table>

当你在浏览器中打开它时,你会看到一个单行表(洋葱,2.41,是的)。但是当您通过服务器运行它时,表的实际内容将替换为${prods}变量中存在的任何数据。

于 2019-02-11T20:23:05.660 回答