如何使用th:each在thymeleaf中加载Spring message.properties

问题描述 投票:1回答:1

我有一个枚举包含4个值,如创建,保存,批准,enter code hererejected,对于这4个值,我有文本显示在message.properties的前视图页面,如i1 =已创建,i2 =已保存,i3 =已批准,i4 =已拒绝

我的代码看起来像这样。

@RequestMapping(value = "/" , method = RequestMethod.GET, 
produces = {"text/html;charset=UTF-8"})
public String getClaimProcess(Model  model,Locale locale)
{

    EntityTypes[] entityTypeArray = entityTypes.values();
    model.addAttribute("entityTypes", entityTypeArray);
    return "ClaimProcess";
}

<select style="width:60%;" class="form-control" id="sel1">
                <option th:each="entity , index : ${entityTypes}"
                        th:value="${index.index}"
                        th:text="#{i1}"></option> 
</select>

所以我想将i1增加到i2,i3,i4等等......请帮帮我

spring-boot thymeleaf
1个回答
0
投票

为了获得带有动态密钥的消息,您可以使用#messages对象,执行以下操作:

<option th:each="entity , index : ${entityTypes}"
    th:value="${index.index}"
    th:text="${#messages.msg('i' + index.index)}"></option> 

也就是说,构造要使用的消息密钥的字符串,并将其传递给#messages.msg方法。

© www.soinside.com 2019 - 2024. All rights reserved.