Automatic Binding
webforJ 提供了一些功能,可以简化开发人员的配置和自动绑定过程。本节演示了如何有效使用这些功能。
使用 BindingContext.of
BindingContext.of 方法自动将 UI 组件绑定到指定 bean 类的属性,简化了绑定过程并减少了手动设置。它根据名称将声明为字段的可绑定组件与 bean 属性对齐。
public class HeroRegistration extends App {
// 可绑定组件
TextField name = new TextField("文本字段");
ComboBox power = new ComboBox("能力");
// ...
@Override
public void run() throws WebforjException {
BindingContext<Hero> context = BindingContext.of(this, Hero.class, true);
// ...
}
}
public class Hero {
private String name;
private String power;
// 设置器和获取器
}
UseProperty 注解
当您想要将 bean 属性绑定到具有不同名称的 UI 组件时,请使用 UseProperty 注解。
此注解在将 bean 属性绑定到 UI 组件时提供更高的精确度,特别是在处理 嵌套 bean 属性 时。
public class HeroRegistration extends App {
// 绑定到 name 属性
@UseProperty("name")
TextField nameField = new TextField("