在Thymeleaf中,将一个值用作另一个值的参数

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

Using a value as an argument into another value in Thymeleaf

问题

所以假设我有一个 @ModelAttribute,名为 userCredentials,它是一个 List<String> 对象。

还有一个类型为 Map<String, String> 的另一个 ModelAttribute,名为 roles

我可以在 HTML 中使用 Thymeleaf 分别访问它们,如下:

${userCredentials.contains('<Hardcoded-value>')}

问题是,我想要将硬编码的值替换,并且例如使用:

${userCredentials.contains('roles.client')}

你知道如何成功地将一个模型属性作为另一个模型属性的参数使用吗?它在使用硬编码值时有效。

英文:

So lets say I have a @ModelAttribute of userCredentials which is a List&lt;String&gt; object.

and I have another ModelAttribute of type Map&lt;String,String&gt; of roles.

I can access them separately in HTML using Thymeleaf with:

${userCredentials.contains(&#39;&lt;Hardcoded-value&gt;&#39;)}

The problem i want the hardcoded value replaced and to use for example:

${userCredentials.contains(&#39;roles.client&#39;)}

Do you know how can i successfully use a model attribute as a parameter to the other model attribute. It works with the hardcoded values

答案1

得分: 0

你可以使用Thymeleaf预处理

${userCredentials.contains(__${roles.get('client')}__)}

Thymeleaf会在第一次处理时执行预处理部分。因此,它将使用以下内容替换已有内容:

${userCredentials.contains(<roles.get()调用的结果在此处>)}

然后执行模板渲染。

英文:

You can use Thymeleaf pre-processing:

${userCredentials.contains(__${roles.get(&#39;client&#39;)}__)}

Thymeleaf executes the preprocessing part in a first pass. So it will replace what is there with:

${userCredentials.contains(&lt;result of roles.get() call here&gt;)}

And then execute the template rendering.

huangapple
  • 本文由 发表于 2020年8月31日 16:06:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63667055.html
匿名

发表评论

匿名网友

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

确定