Thymeleaf th:each 循环中访问参数

huangapple go评论78阅读模式
英文:

Thymeleaf th:each access parameter in loop

问题

我正在尝试在HTML表格中对一个数字进行上标处理,该表格将显示在电子邮件中。

到目前为止,我的代码如下:

<th:block th:each="param, rowStat: ${someList}">
    <th style="..." th:utext="#{some.translation(${param}, ${rowStat.count})}">superscript</th>
</th:block>

"someList" 是一个简单的字符串列表,"some.translation" 如下所示:

{0} (moreInfo)<sup>{1}</sup>

我想要实现的效果类似于:

String1 (moreInfo)<sup>1</sup><br>
String2 (moreInfo)<sup>2</sup>

我的问题是Thymeleaf抛出一个异常,说在这个上下文中访问变量"param"是被禁止的。访问"rowStat.count" 正常工作。

如果有一种更简单的方法来显示上标数字,对我来说也可以。

英文:

I am trying to superscript a number in a html-table which will be displayed in an E-Mail.

my code so far is the following:

&lt;th:block th:each=&quot;param, rowStat: ${someList}&quot;&gt;
    &lt;th style=&quot;...&quot; th:utext=&quot;#{some.translation(${param}, ${rowStat.count})}&quot;&gt;superscript&lt;/th&gt;
&lt;/th:block&gt;

"someList" is a simple list of strings and "some.translation" is the following:

{0} (moreInfo)&lt;sup&gt;{1}&lt;/sup&gt;

What I am trying to achieve is something like:

String1 (moreInfo)<sup>1</sup><br>
String2 (moreInfo)<sup>2</sup>

My problem is a thymeleaf exception saying that the access to variable "param" is forbidden in this context. Accessing "rowStat.count" is working fine.

If there is a simpler way to display a superscript number it would also be fine for me.

答案1

得分: 2

您不能使用param,因为它是Thymeleaf的保留字,用于检索请求参数。尝试将其更改为其他名称:

&lt;th:block th:each=&quot;myParam, rowStat: ${someList}&quot;&gt;
    &lt;th style=&quot;...&quot; th:utext=&quot;#{some.translation(${myParam}, ${rowStat.count})}&quot;&gt;superscript&lt;/th&gt;
&lt;/th:block&gt;

请参阅Thymeleaf文档中的Request parameters了解更多信息。

param用于检索请求参数。${param.foo}是一个String[],其中包含foo请求参数的值,因此通常会使用${param.foo[0]}来获取第一个值。

英文:

Your use of param is not allowed, because that is a reserved word for Thymeleaf, for retrieving request parameters. Try changing it to something else:

&lt;th:block th:each=&quot;myParam, rowStat: ${someList}&quot;&gt;
    &lt;th style=&quot;...&quot; th:utext=&quot;#{some.translation(${myParam}, ${rowStat.count})}&quot;&gt;superscript&lt;/th&gt;
&lt;/th:block&gt;

See also the Thymeleaf documentation here: Request parameters.

> param : for retrieving request parameters. ${param.foo} is a String[] with the values of the foo request parameter, so ${param.foo[0]} will normally be used for getting the first value.

huangapple
  • 本文由 发表于 2020年8月10日 21:45:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/63341463.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定