You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Instead of using Java Beans, it's possible to use the [classname]`Binder` with Java Records. Since Java Records are immutable, only manual reading and writing can be used when the datatype is a record. This means that methods relying on bean datatypes -- such as `writeBeanAsDraft`, `writeBean`, and `setBean` -- throw an exception when called for a Binder with a record datatype. Additionally, since records can only be read via `readBean`, the `refreshFields` method clears all of the Binder fields.
214
+
Instead of using Java Beans, it's possible to use the [classname]`Binder` with Java Records. Since Java Records are immutable, only manual reading and writing can be used when the datatype is a record. This means that methods relying on bean datatypes -- such as `writeBeanAsDraft`, `writeBean` and `setBean` -- throw an exception when called for a Binder with a record datatype. Additionally, the `refreshFields` method clears all of the Binder fields.
215
215
216
-
For reading a record, the `readRecord` method should be used as shown in the example below.
216
+
You have to use reflection based on record property names to bind values.
217
+
To use reflection, create a Binder by providing the record class, for example `new Binder<>(Person.class);`.
218
+
You can't bind the field based on setters.
219
+
220
+
For reading a record, the `readRecord` or `readBean` method should be used as shown in the example below.
217
221
218
222
[source,java]
219
223
----
220
224
public record Person(String firstName, String lastName) {}
221
225
226
+
Binder<Person> binder = new Binder<>(Person.class);
227
+
// Bind based on property names
228
+
binder.bind(firstNameField, "firstName");
229
+
binder.bind(lastNameField, "lastName");
230
+
222
231
binder.readRecord(new Person("John", "Doe"));
223
232
----
224
233
225
-
This method throws an exception, if used with beans.
234
+
The `readRecord` method throws an exception, if used with beans.
226
235
227
236
For writing a record, the `writeRecord` method should be used. Calling this method applies field and binder level validators, and either returns a new record instance with the current state of the binder, or throws a `ValidationException`.
0 commit comments