【应用篇】Activiti外置表单简单应用(三)

Activiti的简单应用,使用外置表单的方式将业务页面绑定到工作流的结点上,当执行到当前结点时,打印出绑定表单的内容。

新建4个form页面,页面内容随便写些内容即可:

按照下图的方式依次绑定:

流程变量设置如图,其他的类似:

对应生成的xml:

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.activiti.org/test">
  <process id="myProcess" name="My process" isExecutable="true">
    <startEvent id="startevent1" name="Start"></startEvent>
    <userTask id="usertask1" name="显示用户详细信息" activiti:formKey="userinfo.form"></userTask>
    <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
    <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>
    <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
    <userTask id="usertask3" name="耳机验证" activiti:formKey="ear.form"></userTask>
    <sequenceFlow id="flow4" name="耳机验证" sourceRef="exclusivegateway1" targetRef="usertask3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="ear"}]]></conditionExpression>
    </sequenceFlow>
    <userTask id="usertask4" name="二次登陆" activiti:formKey="secondtime.form"></userTask>
    <sequenceFlow id="flow5" name="存在登陆信息" sourceRef="exclusivegateway1" targetRef="usertask4">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="exist"}]]></conditionExpression>
    </sequenceFlow>
    <exclusiveGateway id="exclusivegateway2" name="Exclusive Gateway"></exclusiveGateway>
    <sequenceFlow id="flow6" sourceRef="usertask4" targetRef="exclusivegateway2"></sequenceFlow>
    <sequenceFlow id="flow7" name="密码错误" sourceRef="exclusivegateway2" targetRef="usertask4">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="no"}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow8" name="密码正确" sourceRef="exclusivegateway2" targetRef="usertask3">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="yes"}]]></conditionExpression>
    </sequenceFlow>
    <userTask id="usertask5" name="键盘验证" activiti:formKey="keyboard.form"></userTask>
    <sequenceFlow id="flow9" sourceRef="usertask3" targetRef="usertask5"></sequenceFlow>
    <endEvent id="endevent1" name="End"></endEvent>
    <sequenceFlow id="flow10" sourceRef="usertask5" targetRef="endevent1"></sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">
    <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">
      <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="75.0" y="79.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
        <omgdc:Bounds height="55.0" width="105.0" x="155.0" y="69.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
        <omgdc:Bounds height="40.0" width="40.0" x="305.0" y="77.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
        <omgdc:Bounds height="55.0" width="105.0" x="415.0" y="70.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4">
        <omgdc:Bounds height="55.0" width="105.0" x="273.0" y="178.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="exclusivegateway2" id="BPMNShape_exclusivegateway2">
        <omgdc:Bounds height="40.0" width="40.0" x="447.0" y="185.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="usertask5" id="BPMNShape_usertask5">
        <omgdc:Bounds height="55.0" width="105.0" x="600.0" y="70.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
        <omgdc:Bounds height="35.0" width="35.0" x="750.0" y="80.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="110.0" y="96.0"></omgdi:waypoint>
        <omgdi:waypoint x="155.0" y="96.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="260.0" y="96.0"></omgdi:waypoint>
        <omgdi:waypoint x="305.0" y="97.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
        <omgdi:waypoint x="345.0" y="97.0"></omgdi:waypoint>
        <omgdi:waypoint x="415.0" y="97.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="48.0" x="347.0" y="80.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
        <omgdi:waypoint x="325.0" y="117.0"></omgdi:waypoint>
        <omgdi:waypoint x="325.0" y="178.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="72.0" x="245.0" y="151.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
        <omgdi:waypoint x="378.0" y="205.0"></omgdi:waypoint>
        <omgdi:waypoint x="447.0" y="205.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
        <omgdi:waypoint x="467.0" y="225.0"></omgdi:waypoint>
        <omgdi:waypoint x="466.0" y="309.0"></omgdi:waypoint>
        <omgdi:waypoint x="389.0" y="309.0"></omgdi:waypoint>
        <omgdi:waypoint x="325.0" y="309.0"></omgdi:waypoint>
        <omgdi:waypoint x="325.0" y="233.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="48.0" x="469.0" y="267.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
        <omgdi:waypoint x="467.0" y="185.0"></omgdi:waypoint>
        <omgdi:waypoint x="467.0" y="125.0"></omgdi:waypoint>
        <bpmndi:BPMNLabel>
          <omgdc:Bounds height="14.0" width="48.0" x="468.0" y="166.0"></omgdc:Bounds>
        </bpmndi:BPMNLabel>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">
        <omgdi:waypoint x="520.0" y="97.0"></omgdi:waypoint>
        <omgdi:waypoint x="600.0" y="97.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">
        <omgdi:waypoint x="705.0" y="97.0"></omgdi:waypoint>
        <omgdi:waypoint x="750.0" y="97.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

代码实现:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipInputStream;

import org.activiti.engine.FormService;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngineConfiguration;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.repository.ProcessDefinitionQuery;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;

import com.tgb.itoo.activiti.controller.CommonDelegate;
import com.tgb.itoo.activiti.controller.milaoshi;
import com.tgb.itoo.activiti.controller.tanghuan;

public class formprocess {

	private static String readDataFromConsole(String prompt) {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		String str = null;
		try {
			System.out.print(prompt);
			str = br.readLine();

		} catch (IOException e) {
			e.printStackTrace();
		}
		return str;
	}

	public static void main(String[] args) {

		String str = readDataFromConsole("Please input string:");
		System.out.println("The information from console: " + str);

		ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration
				.createStandaloneProcessEngineConfiguration();
		// 连接数据库的配置
		processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");
		processEngineConfiguration
				.setJdbcUrl("jdbc:mysql://localhost:3306/activitiexam?useUnicode=true&characterEncoding=utf8");
		processEngineConfiguration.setJdbcUsername("root");
		processEngineConfiguration.setJdbcPassword("root");
		processEngineConfiguration.setDatabaseSchemaUpdate("create");
		ProcessEngine processEngine = processEngineConfiguration
				.buildProcessEngine();

		System.out.println(processEngine);
		// 获取流程存储服务组件
		RepositoryService repositoryService = processEngine
				.getRepositoryService();

		// 获取运行时服务组件
		RuntimeService runtimeService = processEngine.getRuntimeService();

		// 获取流程任务组件
		TaskService taskService = processEngine.getTaskService();

		//获取流程的表单组件
		FormService formService=processEngine.getFormService();

		// 1、部署流程文件
		InputStream in =repositoryService.createDeployment().name("MyProcess")
		.getClass().getClassLoader().getResourceAsStream("diagrams/"+ str +".zip");
		ZipInputStream zipInputStream = new ZipInputStream(in);
		Deployment deployment = processEngine.getRepositoryService()// 与流程定义和部署对象相关的Service
				.createDeployment()// 创建一个部署对象
				.name("流程定义")// 添加部署的名称
				.addZipInputStream(zipInputStream)// 指定zip格式的文件完成部署
				.deploy();// 完成部署

		// 2、启动流程--方式一
		/*ProcessInstance processInstance = runtimeService
				.startProcessInstanceByKey("myProcess");
		*/

		/**
		 * 2、启动流程--方式二
		 * 此处使用这种方式,可能有问题,当所有的流程定义的key值均为myProcess时,查询出来的数据为多条
		 * 但是真正使用时,我们是根据前台显示的多条中选择其中之一,就可以直接获取processDefinitionID故也没有问题
		 */
		ProcessDefinitionQuery query = repositoryService
				.createProcessDefinitionQuery()
				.processDefinitionKey("myProcess").active()
				.orderByProcessDefinitionVersion().desc();
		ProcessDefinition  processDefinition = query.list().get(0);
		Map<String, String> formProperties = new HashMap<String, String>();

		formProperties.put("numberOfDays", "20150601");
		//此处如果第二个参数为null值则报错,即使不放入任何数据也要使用map格式的对象
		ProcessInstance processInstance = formService
				.submitStartFormData(processDefinition.getId(), formProperties);

		String end="1";
		while (end.equalsIgnoreCase("1")) {

			// 3、查询任务
			Task task = taskService.createTaskQuery()
					.processInstanceId(processInstance.getId()).singleResult();
			;			

			if (task != null) {
				Object renderedTaskForm = formService.getRenderedTaskForm(task.getId());
				System.out.println("表单内容---"+renderedTaskForm.toString());
				System.out.println("任务名称---"+task.getName());							

				/**
				 * 如果整个流程都是顺序执行可以不暂停的,则去掉此段代码即可
				 * 注:现实中存在的是页面流转过程中是需要点击下一步操作的,可以将此暂停理解为下一步的操作
				 */
				str = readDataFromConsole("Please input next:");
				Map<String, Object> variables = new HashMap<String, Object>();
				variables.put("result", str);

				//方式一启动流程的完成任务,如果下一个任务需要参数使用map传递,此处的变量均是给下一个任务设置的
				taskService.complete(task.getId(),variables);
				//方式二启动流程完成任务
				//formService.submitTaskFormData(task.getId(), formProperties);

				//注:以上的两种方式可以混用
			}else {
				end="0";
				System.out.println("任务已经完成");
			} 

		}

	}

}

执行结果:

总结:

以上实现了结点绑定页面的操作,当执行到当前结点时,会打印出绑定页面的内容,这样页面的执行顺序就不需要我们去管理,而可以通过简单的配置实现,以上只是一个简单的main函数实现,下面会继续实现一个简单的demo。

时间: 2024-10-10 06:28:53

【应用篇】Activiti外置表单简单应用(三)的相关文章

【应用篇】Activiti外置表单实例demo(四)

在这里我想说的外置表单.是说我们将我们自己的jsp(.form,.html)等页面上传到工作流的数据库中,当任务运行到当前结点时.给我们像前台发送绑定好的表单. 此处是给表单绑定表单的过程 不允许为:${deptLeaderPass =='false'} 以下我们看相应的页面内容: start.form简单的html页面: <table border="1"> <tr> <td>请假类型:</td> <td> <sele

Activiti 工作流表单设计及开发

一.前言 Activiti 5对表单的支持目前还是比较弱的,表现在对表单的开发还需要写Freemark模板,并且它的模板还需要跟class文件一起打包发布.这使得流程的表单设计必须由开发人员来开发处理.因而,开发一套易用性强的流程表单功能就显得很有必要. 二.需求 用户一般都希望能有如Microsoft的Office套件中的InfoPath那样,可以自己进行设计,并且能与工作流程绑在一起进行流转处理.如下所示: 表单中每个字段有固定的数据类型,并由不同的数据控件展示,如日期.数字.单选或多选.下

表单提交(三)——阻止Eneter键提交表单

当用户在文本框中进行编辑时,按下键盘Enter键,会触发表单提交.为了防止这种意外,有一种方法就是拒绝所有表单提交, 然后通过单击指定的提交命令按钮才能提交表单. 首先,将"return false"绑定到表单的onsubmit事件,来阻止所有表单提交. 第二,使用input="button"通过onclick事件,以this.form.submit()方法提交,而不会触发onsubmit事件. 所以不能使用jquery方式$("#myForm"

vue表单校验(三)

vue表单校验(三) 每当看到heyui的这个表单校验,我就一直想将element的校验也做类似的功能,终于有了方式,虽然不是很完美,但是可以使用,能满足要求了 实现方式 基于element-ui实现 通过表单提交时,触发校验,未通过的表单会添加is-error,之后滚动到对应的错误位置即可 页面视图 实现逻辑 触发条件 在提交时,若是未通过则开始进行判断,由于是依赖于is-error的class类名,因而需要等form表单错误的元素添加完is-error类名后再进行判断 submitForm1

第一篇,js表单验证模板

下面是对于一个email的表单验证的基本模板<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"> <style type="text/css" > .init{ border: 1px solid black; font-weight: bold; } .right{ border: 1px solid green; f

【FunnyBear的Java之旅 - Spring篇】Spring表单验证

尝试对Spring提供的表单进行了验证 1. 创建表单 在jsp页面中添加如下表单.其中action对应我们准备的Controller,commandName用于指定在pageContext中表单所对应的对象.Spring会自动把表单数据填充到对象中.sf:input和sf:error的path对应对象的属性. <%@ taglib prefix="sf" uri="http://www.springframework.org/tags/form"%>

2018-06-25 js表单事件、三个高度和Ajax异步通讯技术

表单事件: onfocus -> 表单控件得到焦点时触发: obj_ipt.onfocus=function(){}; onblur -> 表单控件失去焦点时: onchange -> 表单控件内容被改变时: onselect -> 控件被选择时: onsubmit -> 当表单提交时: onreset -> 当表单重置时: 表单对象方法: select() -> 对象被选中: ipt_obj.select(); blur() -> 失去焦点 focus()

关于表单序列化的三种方法

表单序列化: 方法1:serialize(): 就是把表单信息序列化成一个字符串 (认为最常用 的方法) <html> <head> <script type="text/javascript" src="/jquery/jquery.js"></script> <script type="text/javascript"> $(document).ready(function(){ $

bookstrap form表单简单-smart-form

代码: <form class="smart-form" id="smartForm"> <fieldset> <legend>新建抽取任务</legend> <div class="row"> <section class="col col-2 text-right"> <label class="text">数据源:&l