【问题标题】:Using a value as an argument into another value in Thymeleaf使用一个值作为 Thymeleaf 中另一个值的参数
【发布时间】:2020-12-19 08:26:28
【问题描述】:

假设我有一个 userCredentials 中的 @ModelAttribute,这是一个 List<String> 对象。

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

我可以使用 ThymeleafHTML 中单独访问它们:

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

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

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

你知道我怎样才能成功地使用一个模型属性作为另一个模型属性的参数。它适用于硬编码值

【问题讨论】:

    标签: java spring spring-boot thymeleaf modelattribute


    【解决方案1】:

    你可以使用Thymeleaf pre-processing:

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

    Thymeleaf 在第一遍中执行预处理部分。所以它将替换现有的:

    ${userCredentials.contains(<result of roles.get() call here>)}
    

    然后执行模板渲染。

    【讨论】:

      猜你喜欢
      • 2014-04-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-24
      • 1970-01-01
      • 2020-10-01
      • 2015-12-27
      相关资源
      最近更新 更多