像js一样 const object = {test: 'template'}; const str = 'test'l console.log(object[str])
后台modal中有两个对象,一个strList,一个map,
比如我又一个循环, th:each="str : strList"
然后类似${map[${str}]}这样的方式获取map中的值,应该怎么写
thymeleaf如何获取动态属性值
- 写回答
- 好问题 0 提建议
- 关注问题
- 邀请回答
-
1条回答 默认 最新
WaitIKnowYou 2022-12-12 17:03关注Thymeleaf是一个Java模板引擎,用于渲染HTML页面。如果您想在Thymeleaf模板中获取动态属性值,可以使用方括号语法。假设您的modal中的字符串列表如下:
List strList = Arrays.asList("foo", "bar", "baz");
和map如下:Map<String, String> map = new HashMap<>(); map.put("foo", "template1"); map.put("bar", "template2"); map.put("baz", "template3");您可以在Thymeleaf模板中使用以下代码来获取map中的值:
<ul> <li th:each="str : ${strList}"> <span th:text="${map[str]}">template1</span> </li> </ul>该模板将生成以下HTML:
<ul> <li> <span>template1</span> </li> <li> <span>template2</span> </li> <li> <span>template3</span> </li> </ul>请注意,在获取map中的值时,您必须使用方括号语法,并且您需要在方括号中提供字符串列表中的值作为键。希望这能帮助您。
解决 无用评论 打赏 举报 编辑记录