Java thymeleaf - 将 th:each 与 th:href 结合
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21962011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
thymeleaf - combined th:each with th:href
提问by Tom
I'm new to Thymeleaf (and webdev) and I'm trying to combine Thymeleaf iteration (th:each) with URL re-writing (th:href).
我是 Thymeleaf(和 webdev)的新手,我正在尝试将 Thymeleaf 迭代(th:each)与 URL 重写(th:href)结合起来。
<a th:each="lid : ${lists}" th:text="${lid}" th:href="@{/list?l=${lid}}">
hello
</a>
This produces the following (where lid=45):
这会产生以下结果(其中盖子 = 45):
<a href="/list?l=${lid}">45</a>
So, it did the substitution on the th:text, but not on the th:href.
因此,它在 th:text 上进行了替换,但不在 th:href 上进行了替换。
I'm not trying to do any sort of URL re-writing, I'm just using the '@' syntax because I want Thymeleaf to substitute the 'lid' attribute.
我不想做任何类型的 URL 重写,我只是使用 '@' 语法,因为我希望 Thymeleaf 替换 'lid' 属性。
I'm using the current version of Thymeleaf (2.1.2) with Google App Engine.
我在 Google App Engine 上使用当前版本的 Thymeleaf (2.1.2)。
采纳答案by Tom Verelst
If you don't want to do any url rewriting, you shouldn't use the @
syntax.
如果您不想进行任何 url 重写,则不应使用该@
语法。
You can use the pipeline (|
) syntax to do some literal substitions:
您可以使用管道 ( |
) 语法进行一些文字替换:
th:href="|/list?l=${lid}|"
Source: Thymeleaf documentation
来源:Thymeleaf 文档
回答by Iwo Kucharski
You can also try this way:
你也可以试试这种方式:
<a th:href="@{'/list?l=' + ${lid}}" th:text="${lid}">element</a>
回答by C. Springer
I don't have enough reputation to add a comment on a previous post but the Thymeleaf Source documentation link from a previous post is broken. Documentation can now be found at the following link:
我没有足够的声誉来对以前的帖子添加评论,但以前帖子中的 Thymeleaf 源文档链接已损坏。现在可以在以下链接中找到文档:
Section 9 Using Expressions in URLs in this documentation explains how you can use expressions within other expressions when generating URLs with the @ syntax. The following is an example:
本文档中的第 9 节在 URL 中使用表达式解释了在使用 @ 语法生成 URL 时如何在其他表达式中使用表达式。下面是一个例子:
<a th:href="@{/order/details(id=${modelattribute})}"/>
Will produce a link similar to:
将产生类似于以下内容的链接:
http://domain.org/context/order/details?id=1
if modelattribute had a value of 1 in the current context.
如果 modelattribute 在当前上下文中的值为 1。