为什么“href"和“th:href"同时存在? [英] Why "href" and "th:href" exist at the same time?

查看:27
本文介绍了为什么“href"和“th:href"同时存在?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Thymeleaf 阅读页面.

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

<html lang="en" xmlns:th="http://www.thymeleaf.org"><头><meta charset="UTF-8"/><title>用户</title><link rel="stylesheet" th:href="@{/css/bootstrap.css}"></link><body class="容器"><br/><h1>修改用户</h1><br/><br/><div class="with:80%"><form class="form-horizo​​ntal" 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 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="密码"/>

<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 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">返回</a>

</表单>

</html>

很明显,th:href"是为了回去.关于属性href"的功能有什么选择吗?

解决方案

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

返回</a>

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

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

<tr><th>NAME</th><th>价格</th><th>有货</th></tr><tr th:each="prod : ${prods}" th:class="${prodStat.odd}?'odd'"><td th:text="${prod.name}">洋葱</td><td th:text="${prod.price}">2.41</td><td th:text="${prod.inStock}?#{true} : #{false}">是</td></tr>

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

I am reading a page using Thymeleaf.

In "Edit page", there is a "Back" button for going back to "User List Page". The strange thing for me is this button has "href" and "th:href" at the same time. image detail of the button

<!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>

It is obvious that "th:href" is for going back. Is there any opition on what is the function of the atrribute "href"?

解决方案

ThymeLeaf is designed to use the same file for both as prototype you can view in your browser as well as a working template file. What this means in practice is that if you want, you can open the template file in a browser without actually running it and it still looks okay. For example, in this code:

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

If you open the file directly in a browser, the browser will ignore the th:href (because it doesn't know what to do with it) and instead use href="/toAdd". However, when you run it through the templating engine on a server, href="/toAdd" is replaced with the result of the dynamic expression th:href="@{/list}".

This is more easily shown with a table. Like this:

<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>

When you open that in a browser, you'll see a table with a single row (Onions, 2.41, yes). But when you run it through the server, the actual content of the table is replaced with whatever data exists in the ${prods} variable.

这篇关于为什么“href"和“th:href"同时存在?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆