thymeleaf - 第:field ignores the placeholder value and sets 0

标签 thymeleaf

th:field 忽略占位符值并将默认值设置为 0。

型号:

@Entity
public class Employer implements Serializable {

    @Id
    private int id;
...
}

查看:

<input type="text" placeholder="A1234" th:field="*{emloyer.id}">

正在生成什么:

<input type="text" placeholder="A1234" value="0" id="emloyer.id" name="emloyer.id">

我想了解为什么会生成 value=0 。如果我将 int 类型更改为 String 就可以了。我什至尝试了 value=""和 th:value="",但它仍然生成为 value="0"。我知道我可以写 id="emloyer.id"name="emloyer.id"而不是 th:field="*{emloyer.id}"。但我想了解为什么它会这样。 我想显示占位符值而不是默认值。我怎样才能让它发挥作用?

最佳答案

默认情况下,int 的值为 0。它不可能没有值(value)。您可以使用设置为 nullInteger,然后 Thymeleaf 将使用 value=""

关于thymeleaf - 第:field ignores the placeholder value and sets 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55645817/

相关文章:

java - 嵌套迭代 thymeleaf

java - Thymeleaf 数组元素连接

java - 在 Spring 4 中定义异常处理程序以返回自定义 404 和 500 页面

spring boot 在 docker 上部署时找不到模板

java - 如何使用路径变量参数将请求中的特定对象值发送到 Controller

java - 隐藏字段值空白 Thymeleaf

spring - 如何在 thymeleaf 中为消息属性文件使用自定义位置?

spring - 在thymeleaf中获取spring应用环境

jquery - thymeleaf :inline ="javascript" list mix with variable jquery

java - "readonly if parameter"在 thymeleaf 中不起作用