通用组件 XML
component XML 元素支持通过指定全限定类名(FQN)来创建任意 UI 组件。组件属性使用内部的 property 元素定义,类似于操作(action)配置。
用例
最简便的方式是在 XML 中直接定义一个常规组件的实例:
<component id="button" class="io.jmix.flowui.kit.component.button.JmixButton">
<properties>
<property name="text" value="Save"/>
<property name="icon" value="ARCHIVE" type="ICON"/>
</properties>
</component>
框架会实例化组件类,并通过匹配的 setter 方法设置声明的属性。在此示例中,
text 属性映射到 setText(),而 ICON 属性解析器将 ARCHIVE 转换为图标实例,传递给 setIcon()。
也可以在视图控制器中订阅通用组件的事件,方式与标准声明式组件相同:
@Autowired
protected Notifications notifications;
@Subscribe(id = "button", subject = "clickListener")
public void onButtonClick(ClickEvent<JmixButton> event) {
notifications.show("Saved!");
}
同样的机制也可用于接收转换值的 setter 的自定义组件:
<component id="testComponent" class="com.company.onboarding.component.TestComponent">
<properties>
<property name="stringsList" value="a b ,c"/>
<property name="stringsSet" value="a b ,c"/>
<property name="stringsArray" value="a b ,c"/>
<property name="strings" value="a b ,c"/>
<property name="dataContainer" value="productsDc" type="CONTAINER_REF"/>
</properties>
</component>
在这个示例中,TestComponent 类定义了 stringsList、stringsSet、stringsArray、strings 和 dataContainer 的 setter。
Jmix 会将 XML 中提供的值转换为字符串值,并解析 productsDc 数据容器,
然后再调用对应的 setter 方法。
配置属性
-
name属性值会被转换为 setter 方法名称,例如:-
text→setText() -
dataLoader→setDataLoader()
-
-
框架会查找一个与
name属性值匹配且只有一个输入参数的方法来设置值。如果多个方法具有相同的名称,则不保证会使用预期的方法来设置值。 -
将用找到的方法的输入参数类型对
value属性中的字符串值进行转换,转换为相同类型的实际值。内置的转换器支持
String、Class、布尔值、数字类型、枚举、List、Set以及数组。 可变参数的 setter 也有效,因为是作为数组参数处理。集合和数组条目在转换前按空格或逗号划分。 -
type属性表示字符串值必须由某个可插拔的PropertyParserbean 进行转换:类型 解析器 描述 ICONIconPropertyParser将字符串转换为
Icon实例。CONTAINER_REFDataContainerPropertyParser通过
ID解析数据容器LOADER_REFDataLoaderPropertyParser通过
ID解析数据加载器
属性解析器
当字符串值无法直接使用参数类型进行转换时,会使用可插拔的 PropertyParser bean 处理转换。
目前有三种实现:
-
IconPropertyParser。将字符串转换为
Icon实例。-
如果传入的字符串包含
:分隔符,则使用 图标集合 和 图标名称 值创建一个新的Icon。 -
否则,该字符串被视为
VaadinIcon常量名称。示例:
-
"PLUS"→VaadinIcon.PLUS图标。 -
"myicons:custom"→ 来自自定义集合"myicons"、名称为"custom"的图标。
-
-
DataContainerPropertyParser。从对应视图的
ViewData对象中返回具有给定ID的 数据容器。 -
DataLoaderPropertyParser。从对应视图的
ViewData对象中返回具有给定ID的 数据加载器。