【问题标题】:th:field ignores the placeholder value and sets 0th:field 忽略占位符值并设置为 0
【发布时间】:2019-09-02 21:01:15
【问题描述】:

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}"。但我想了解它为什么会这样。 我想显示占位符值而不是默认值。我怎样才能让它发挥作用?

【问题讨论】:

    标签: thymeleaf


    【解决方案1】:

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

    【讨论】:

    • 您能否说得更具体一些。我必须写如下:@Id private Integer id
    • @paro 是的,这是正确的。如果你想让我更具体,你也需要更具体。你知道Java中int和Integer的区别吗?
    猜你喜欢
    • 2018-05-16
    • 2011-07-09
    • 1970-01-01
    • 2013-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-07
    相关资源
    最近更新 更多