Automatic Binding
webforJ offers several features that streamline the configuration and automatic binding process for developers. This section demonstrates how to use these features effectively.
Using BindingContext.of
The BindingContext.of
method automatically binds UI components to the properties of a specified bean class, simplifying the binding process and reducing manual setup. It aligns bindable components, declared as fields within a form or app, with bean properties based on their names.
public class HeroRegistration extends App {
// Bindable components
TextField name = new TextField("Text Field");
ComboBox power = new ComboBox("Power");
// ...
@Override
public void run() throws WebforjException {
BindingContext<Hero> context = BindingContext.of(this, Hero.class, true);
// ...
}
}
public class Hero {
private String name;
private String power;
// Setters and getters
}
UseProperty
annotation
Use the UseProperty
annotation to specify the bean property name when the UI field name doesn't match the bean property name.
public class HeroRegistration extends App {
// Bindable components
@UseProperty("name")
TextField nameField = new TextField("Text Field");
ComboBox power = new ComboBox("Power");
// ...
}
In the example above, the UI field name is nameField
, but the bean property is name
. You can annotate the UI field with the bean property name to ensure proper binding.
BindingExclude
annotation
Use the BindingExclude
annotation to exclude a component from automatic binding configurations when you prefer to bind it manually or exclude it altogether.
public class HeroRegistration extends App {
// Bindable components
@UseProperty("name")
TextField nameField = new TextField("Text Field");
@BindingExclude
ComboBox power = new ComboBox("Power");
// ...
}
UseValidator
annotation
Use the UseValidator
annotation to declare validators that enforce additional validation rules during binding. Validators apply in the order you specify them.
public class UserRegistration extends App {
@UseValidator(EmailValidator.class)
TextField email = new TextField("Email Address");
}
UseTransformer
annotation
Use the UseTransformer
annotation to declare a transformer class directly on a UI field. The BindingContext
automatically applies the specified transformer.
public class UserRegistration extends App {
@UseProperty("date")
@UseTransformer(DateTransformer.class)
DateField dateField = new DateField("Date Field");
}
BindingReadOnly
annotation
Use the BindingReadOnly
to mark a binding as readonly.
public class UserRegistration extends App {
@BindingReadOnly
TextField IDField = new TextField("User ID");
}
BindingRequired
annotation
Use the BindingReadOnly
to mark a binding as required. See also required binding detections.
public class UserRegistration extends App {
@BindingRequired
TextField emailField = new TextField("User Email");
}