It seems possible to overcome. That is, you want the readable Formatter for people to display yes / no to the user for logical values ββin the model. But you still want the HTML checkbox elements to work, and it seems that these HTML checkbox elements / widgets / JSP tags expect to use a true / false string (or Java boolean type), it does not use a converter to get an arbitrary yes / no string returns to the boolean type.
This problem for me manifests itself in the fact that the initial state of the checkbox is never checked when the model is set to Boolean.TRUE. This means that any read-change-update of the record (without editing this field ends the transition from "true" to "false" if the user has not changed it). This is due to the fact that the initial state in the user interface is incompatible with the model (it is always displayed without checking, i.e., False state), even when the model is a true state. The displayed value is a checked flag on the HTML record editing screen, even if the model has the value Boolean.TRUE. This is because yes is not interpreted as true by HTML elements and the default value is false (since this default value is logical).
So, define your Formatter / Converter (as you already do). But in your @Controller add:
@InitBinder public void initBinder(WebDataBinder binder) { binder.registerCustomEditor(Boolean.class, "friesWithThat", new CustomBooleanEditor(false)); }
It looks like the string display value continues to be yes / no, but the value used and passed to the element of the HTML element still remains true / false.
Now, when editing / updating a record (in CRUD), the initial state of the flag corresponds to the model and saving data (without editing any fields) does not translate the state of the flag (this is my understanding of the problem that you have).
So, from this, I think we can understand that Converters / Formatters are intended for general display of data and that PropertyEditors are intended for displaying model data, therefore data is required for a user interface widget.
source share