百里香叶 - 如何将 <select> th:value 与消息bundle链接?



我有这个选择:

<select class="form-control" th:field="*{objId}" name="objId" >
    <option th:each="obj : ${objList}"
            th:value="${obj.getId()}"
            th:selected="${objList.contains(obj)}"                              
            th:text="${obj.getDescription()}">
    </option>
</select>

默认对象obj的描述为"object not valid"。

有可能使其价值国际化吗?所以,当对象出现在这个列表上时,它读起来,例如,在葡萄牙语中,"Objeto"?

要使用国际化,您应该为支持的每种语言使用单独的消息文件,并显示如下描述:

th:text="#{${obj.getDescription()}}">

但在这种情况下,obj.getDescription()应该返回消息密钥作为其值(例如object.description.message),并且该密钥必须存在于messages_pt.properties文件中,例如:

object.description.message="Objecto"

也许我没有正确理解你,当描述为空时,你只想显示不同的值。在这种情况下,下面的代码应该可以解决问题:

th:text="${obj.getDescription()}?: 'Description is null...'"

th:text="${obj.getDescription()}?: #{message.property.key.here}"

HTH

相关内容