Search code examples
thymeleaf

th:field ignores the placeholder value and sets 0


th:field ignores the placeholder value and sets the default value to 0.

Model:

@Entity
public class Employer implements Serializable {

    @Id
    private int id;
...
}

View:

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

What is being generated:

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

I want to understand why is value=0 being generated. If I change type of int to String it is ok. I even tried value="" and th:value="", but it is still being generated as value="0". I know that I can write id="emloyer.id" name="emloyer.id" instead of th:field="*{emloyer.id}". But I want to understand why it is behaves like that. I want to show placeholder value instead of default. How can I make it work?


Solution

  • By default an int has the value 0. It can't have no value. You could use an Integer set to null, then Thymeleaf will use value="".