自定义表单类型
使用动态表单,需要定义表单的字段以及属性,在WEB流程设计器中没有Default字段(Eclipse中有),给设计带来了一定的困难,下面介绍如何在WEB流程设计器中扩展表单。
首先在stencilset.json文件中添加扩展类型,添加的位置就是formproperty_type下,将会出现在下拉框中
{ "id" : "formproperty_type", "name" : "Type", "name_de" : "Typ", "type" : "Choice", "value" : "", "width" : 100, "optional" : false, "items" : [ { "id" : "c1", "title" : "String", "title_de" : "String", "value" : "string", "refToView" : "" }, { "id" : "c2", "title" : "Date", "title_de" : "Date", "value" : "date", "refToView" : "" }, { "id" : "c3", "title" : "Long", "title_de" : "Long", "value" : "long", "refToView" : "" }, { "id" : "c4", "title" : "Boolean", "title_de" : "Boolean", "value" : "boolean", "refToView" : "" }, { "id" : "c5", "title" : "Enum", "title_de" : "Enum", "value" : "enum", "refToView" : "" }, { "id" : "c6", "title" : "SmartRef", "title_de" : "SmartRef", "value" : "smartref", "refToView" : "" }, { "id" : "c7", "title" : "Group", "title_de" : "Group", "value" : "group", "refToView" : "" } ] } |
构建自定义的GroupFormType,如果只要一般的扩展,则不用对加入values
public class GroupFormType extends AbstractFormType { protected Map<String, String> values; public GroupFormType(){} public GroupFormType(Map<String, String> values) { this.values = values; } @Override public String getName() { return ActivitiConstants.GROUP_TYPE; } @Override public Object convertFormValueToModelValue(String propertyValue) { return propertyValue; } @Override public String convertModelValueToFormValue(Object modelValue) { return modelValue == null ? null : modelValue.toString(); } @Override public Object getInformation(String key) { if (ActivitiConstants.VALUES_KEY.equals(key)) { return values; } else { return null; } } } |
扩展FormTypes,使得自定义类型能够处理FormValues
public class CustomFormTypes extends FormTypes { @Override public AbstractFormType parseFormPropertyType(FormProperty formProperty) { if (ActivitiConstants.GROUP_TYPE.equals(formProperty.getType())) { Map<String, String> values = new LinkedHashMap<String, String>(); for (FormValue formValue : formProperty.getFormValues()) { values.put(formValue.getId(), formValue.getName()); } return new GroupFormType(values); } else if (ActivitiConstants.SMART_REF_TYPE.equals(formProperty.getType())) { Map<String, String> values = new LinkedHashMap<String, String>(); for (FormValue formValue : formProperty.getFormValues()) { values.put(formValue.getId(), formValue.getName()); } return new SmartRefFormType(values); } else { return super.parseFormPropertyType(formProperty); }
} } |
在流程引擎参数中设置,需要注意的是,如果扩展了FormTypes,则需要把所有的类型都加入到参数中(包括自带类型),否则会报找不到类型
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration"> <property name="formTypes"> <bean class="com.pera.dwf.web.activiti.formtype.CustomFormTypes" /> </property> <property name="customFormTypes"> <list> <bean class="org.activiti.engine.impl.form.StringFormType" /> <bean class="org.activiti.engine.impl.form.LongFormType" /> <bean class="org.activiti.engine.impl.form.DateFormType"> <constructor-arg value="yyyy-MM-dd" /> </bean> <bean class="org.activiti.engine.impl.form.BooleanFormType" /> <bean class="com.pera.dwf.web.activiti.formtype.GroupFormType"></bean> <bean class="com.pera.dwf.web.activiti.formtype.SmartRefFormType"></bean> </list> </property> |
经过处理后,最终的动态表单的返回值,可以得到FormValues的值
0: { infos: { 5ef89d50-95ff-4e33-8aa1-48df354c6c52: "不合格品文档" } - fp: { id: "Excel1" name: "不合格品文档" type: { name: "smartref" } - value: null required: false writable: true readable: true } - } |