1
votes

Using SpringMVC + Thymeleaf, how does one bind an integer based model attribute, to an input field in a form using th:value or th:field, without having a value of '0' show in the field itself.

2

2 Answers

2
votes

The issue is not with Thymeleaf, it is on the EL implementation of Tomcat. It doesn't respect the difference between the nullable Integer and primitive int. It is always coercing null values to 0. You can turn off this behavior using this VM argument :

-Dorg.apache.el.parser.COERCE_TO_ZERO=false

or programatically by

System.setProperty("org.apache.el.parser.COERCE_TO_ZERO", "false");

If you choose the programatic way, make sure that you invoke it during initialization of ServletContext (ServletContextListener#contextInitialized)

0
votes

Just remove th:field attribute and write id and name attributes with corresponding logical name.