转自:https://www.cnblogs.com/liuqing576598117/p/9814953.html

1.获取
//通过流程定义ID获取表单字段集合
StartFormData startFormData = formService.getStartFormData(processDefinitionId);

TaskFormData taskFormData = formService.getTaskFormData(taskId);//内置表单(直接在流程中定义)
Object renderedTaskForm = formService.getRenderedTaskForm(taskId);//这个方法是返回一个纯文本的(外置表单(一个.form结尾的文件,),可以是一个div标签)
外置表单判断:
流程启动时:boolean hasStartFormKey = processDefinition.hasStartFormKey();
流程中:TaskFormData taskFormData = formService.getTaskFormData(taskId);
boolean key = taskFormData.getFormKey()


2.任务签收
taskService.claim(taskId, userId);

//数据展示

复制代码
TaskFormData taskFormData = formService.getTaskFormData(taskId);//内置表单(直接在流程中定义)
if (taskFormData.getFormKey() != null) {
  Object renderedTaskForm = formService.getRenderedTaskForm(taskId);//这个方法是返回一个纯文本的(外置表单(一个.form结尾的文件,),可以是一个div标签)
  Task task = taskService.createTaskQuery().taskId(taskId).singleResult();
  mav.addObject("task", task);
  mav.addObject("taskFormData", renderedTaskForm);
  mav.addObject("hasFormKey", true);
} else {
  mav.addObject("taskFormData", taskFormData);
}
复制代码

 

3.自定义表单引擎

复制代码
/**
 * Javascript表单字段
 *
 */
public class JavascriptFormType extends AbstractFormType {
 
    @Override
    public String getName() {
        return "javascript";
    }
 
    @Override
    public Object convertFormValueToModelValue(String propertyValue) {
        return propertyValue;
    }
 
    @Override
    public String convertModelValueToFormValue(Object modelValue) {
        return (String) modelValue;
    }
}
复制代码
复制代码
/**
 * 自定义表单引擎
 *
 */
public class MyFormEngine implements FormEngine {
 
    @Override
    public String getName() {
        return "myformengine";
    }
 
    @Override
    public Object renderStartForm(StartFormData startForm) {
        javax.swing.JButton jButton = new javax.swing.JButton();
        jButton.setName("My Start Form Button");
        return jButton;
    }
 
    @Override
    public Object renderTaskForm(TaskFormData taskForm) {
        javax.swing.JButton jButton = new javax.swing.JButton();
        jButton.setName("My Task Form Button");
        return jButton;
    }
复制代码

自定义表单引擎 : public class MyFormEngine implements FormEngine (供C/S架构使用,eg:Swing,AWT,SWT)
Javascript表单字段: public class JavascriptFormType extends AbstractFormType

复制代码
<!-- 流程引擎配置的bean -->
<bean id="processEngineConfiguration"
class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<property name="jdbcUrl" value="jdbc:mysql://localhost:3306/act" />
<property name="jdbcDriver" value="com.mysql.jdbc.Driver" />
<property name="jdbcUsername" value="root" />
<property name="jdbcPassword" value="123456" />
<property name="databaseSchemaUpdate" value="true" />

<property name="jobExecutorActivate" value="false" />

<!-- 自定义表单字段类型 -->
<property name="customFormTypes">
<list>
  <bean class="me.kafeitu.activiti.chapter6.form.JavascriptFormType" />
</list>
</property>

<!-- 自定义表单引擎 -->
<property name="customFormEngines">
<list>
  <bean class="me.kafeitu.activiti.chapter6.form.MyFormEngine" />
</list>
</property>
</bean>
复制代码

 

5. 提交表单字段并启动一个新的流程实例

复制代码
//从请求中获取表单数据,然后启动流程
Map<String, String> formValues = new HashMap<String, String>();
if (hasStartFormKey) { // formkey表单
  Map<String, String[]> parameterMap = request.getParameterMap();
  Set<Entry<String, String[]>> entrySet = parameterMap.entrySet();
  for (Entry<String, String[]> entry : entrySet) {
    String key = entry.getKey();
    formValues.put(key, entry.getValue()[0]);
  }
} else { // 动态表单
  // 先读取表单字段在根据表单字段的ID读取请求参数值
  StartFormData formData = formService.getStartFormData(pdid);

  // 从请求中获取表单字段的值
  List<FormProperty> formProperties = formData.getFormProperties();
  for (FormProperty formProperty : formProperties) {
    String value = request.getParameter(formProperty.getId());
    formValues.put(formProperty.getId(), value);
  }
}
ProcessInstance processInstance = formService.submitStartFormData(pdid, formValues