activiti实战系列之动态表单 formService 自定义变量类型

时间:2022-06-14 20:49:31

activiti实战系列之动态表单 formService 自定义变量类型

目前Activiti默认支持的类型有String,long,enum,date,boolean,collection

activiti实战系列之动态表单 formService 自定义变量类型

要自定义字段类型,首先需要表单类型解析类

/**
* @Author:LJ
* @Description: activiti Javascript类型表单字段
* @Date: 2018/5/17
* @Modified By:
*/
public class JavascriptFormType extends AbstractFormType {
/**
* 把表单中的值转换为实际的对象
* @param propertyValue
* @return
*/
@Override
public Object convertFormValueToModelValue(String propertyValue) {
return propertyValue;
} /**
* 把实际对象的值转换为表单中的值
* @param modelValue
* @return
*/
@Override
public String convertModelValueToFormValue(Object modelValue) {
return (String) modelValue;
} /**
* 定义表单类型的标识符
*
* @return
*/
@Override
public String getName() {
return "javascript";
}
}

第二、在流程引擎中注册解析类

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
....
<!-- 自定义表单字段类型 -->
<property name="customFormTypes">
<list>
<bean class="me.kafeitu.activiti.form.JavascriptFormType" />
</list>
</property>
</bean>
</beans>

扩展:SpringBoot方式注册解析

package com.example.demo.config;

import com.example.demo.config.formType.JavascriptFormType;
import org.activiti.engine.form.AbstractFormType;
import org.activiti.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; import java.util.Arrays;
import java.util.List; /**
* @Author:LJ
* @Description:
* @Date: 2018/3/21
* @Modified By:
*/
@Configuration
public class ApplicationConfig extends WebMvcConfigurerAdapter {/**
* 方式一
*@Author LJ
*/
@Bean
public BeanPostProcessor activitiConfigurer() {
return new BeanPostProcessor() {
@Override
public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
if (bean instanceof SpringProcessEngineConfiguration) {
List<AbstractFormType> customFormTypes = Arrays.<AbstractFormType>asList(new JavascriptFormType());
((SpringProcessEngineConfiguration)bean).setCustomFormTypes(customFormTypes);
}
return bean;
}
@Override
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
return bean;
}
}; }
}

参考:

How to declare an Activiti custom FormType in a Spring Boot application