Activiti外置表单简单应用

 引用地址:http://blog.csdn.net/hejingyuan6/article/details/46409451


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


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




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




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




对应生成的xml


[html]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <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">  
  3.   <process id="myProcess" name="My process" isExecutable="true">  
  4.     <startEvent id="startevent1" name="Start"></startEvent>  
  5.     <userTask id="usertask1" name="显示用户详细信息" activiti:formKey="userinfo.form"></userTask>  
  6.     <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>  
  7.     <exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway"></exclusiveGateway>  
  8.     <sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>  
  9.     <userTask id="usertask3" name="耳机验证" activiti:formKey="ear.form"></userTask>  
  10.     <sequenceFlow id="flow4" name="耳机验证" sourceRef="exclusivegateway1" targetRef="usertask3">  
  11.       <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="ear"}]]></conditionExpression>  
  12.     </sequenceFlow>  
  13.     <userTask id="usertask4" name="二次登陆" activiti:formKey="secondtime.form"></userTask>  
  14.     <sequenceFlow id="flow5" name="存在登陆信息" sourceRef="exclusivegateway1" targetRef="usertask4">  
  15.       <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="exist"}]]></conditionExpression>  
  16.     </sequenceFlow>  
  17.     <exclusiveGateway id="exclusivegateway2" name="Exclusive Gateway"></exclusiveGateway>  
  18.     <sequenceFlow id="flow6" sourceRef="usertask4" targetRef="exclusivegateway2"></sequenceFlow>  
  19.     <sequenceFlow id="flow7" name="密码错误" sourceRef="exclusivegateway2" targetRef="usertask4">  
  20.       <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="no"}]]></conditionExpression>  
  21.     </sequenceFlow>  
  22.     <sequenceFlow id="flow8" name="密码正确" sourceRef="exclusivegateway2" targetRef="usertask3">  
  23.       <conditionExpression xsi:type="tFormalExpression"><![CDATA[${result=="yes"}]]></conditionExpression>  
  24.     </sequenceFlow>  
  25.     <userTask id="usertask5" name="键盘验证" activiti:formKey="keyboard.form"></userTask>  
  26.     <sequenceFlow id="flow9" sourceRef="usertask3" targetRef="usertask5"></sequenceFlow>  
  27.     <endEvent id="endevent1" name="End"></endEvent>  
  28.     <sequenceFlow id="flow10" sourceRef="usertask5" targetRef="endevent1"></sequenceFlow>  
  29.   </process>  
  30.   <bpmndi:BPMNDiagram id="BPMNDiagram_myProcess">  
  31.     <bpmndi:BPMNPlane bpmnElement="myProcess" id="BPMNPlane_myProcess">  
  32.       <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">  
  33.         <omgdc:Bounds height="35.0" width="35.0" x="75.0" y="79.0"></omgdc:Bounds>  
  34.       </bpmndi:BPMNShape>  
  35.       <bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">  
  36.         <omgdc:Bounds height="55.0" width="105.0" x="155.0" y="69.0"></omgdc:Bounds>  
  37.       </bpmndi:BPMNShape>  
  38.       <bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">  
  39.         <omgdc:Bounds height="40.0" width="40.0" x="305.0" y="77.0"></omgdc:Bounds>  
  40.       </bpmndi:BPMNShape>  
  41.       <bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">  
  42.         <omgdc:Bounds height="55.0" width="105.0" x="415.0" y="70.0"></omgdc:Bounds>  
  43.       </bpmndi:BPMNShape>  
  44.       <bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4">  
  45.         <omgdc:Bounds height="55.0" width="105.0" x="273.0" y="178.0"></omgdc:Bounds>  
  46.       </bpmndi:BPMNShape>  
  47.       <bpmndi:BPMNShape bpmnElement="exclusivegateway2" id="BPMNShape_exclusivegateway2">  
  48.         <omgdc:Bounds height="40.0" width="40.0" x="447.0" y="185.0"></omgdc:Bounds>  
  49.       </bpmndi:BPMNShape>  
  50.       <bpmndi:BPMNShape bpmnElement="usertask5" id="BPMNShape_usertask5">  
  51.         <omgdc:Bounds height="55.0" width="105.0" x="600.0" y="70.0"></omgdc:Bounds>  
  52.       </bpmndi:BPMNShape>  
  53.       <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">  
  54.         <omgdc:Bounds height="35.0" width="35.0" x="750.0" y="80.0"></omgdc:Bounds>  
  55.       </bpmndi:BPMNShape>  
  56.       <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">  
  57.         <omgdi:waypoint x="110.0" y="96.0"></omgdi:waypoint>  
  58.         <omgdi:waypoint x="155.0" y="96.0"></omgdi:waypoint>  
  59.       </bpmndi:BPMNEdge>  
  60.       <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">  
  61.         <omgdi:waypoint x="260.0" y="96.0"></omgdi:waypoint>  
  62.         <omgdi:waypoint x="305.0" y="97.0"></omgdi:waypoint>  
  63.       </bpmndi:BPMNEdge>  
  64.       <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">  
  65.         <omgdi:waypoint x="345.0" y="97.0"></omgdi:waypoint>  
  66.         <omgdi:waypoint x="415.0" y="97.0"></omgdi:waypoint>  
  67.         <bpmndi:BPMNLabel>  
  68.           <omgdc:Bounds height="14.0" width="48.0" x="347.0" y="80.0"></omgdc:Bounds>  
  69.         </bpmndi:BPMNLabel>  
  70.       </bpmndi:BPMNEdge>  
  71.       <bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">  
  72.         <omgdi:waypoint x="325.0" y="117.0"></omgdi:waypoint>  
  73.         <omgdi:waypoint x="325.0" y="178.0"></omgdi:waypoint>  
  74.         <bpmndi:BPMNLabel>  
  75.           <omgdc:Bounds height="14.0" width="72.0" x="245.0" y="151.0"></omgdc:Bounds>  
  76.         </bpmndi:BPMNLabel>  
  77.       </bpmndi:BPMNEdge>  
  78.       <bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">  
  79.         <omgdi:waypoint x="378.0" y="205.0"></omgdi:waypoint>  
  80.         <omgdi:waypoint x="447.0" y="205.0"></omgdi:waypoint>  
  81.       </bpmndi:BPMNEdge>  
  82.       <bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">  
  83.         <omgdi:waypoint x="467.0" y="225.0"></omgdi:waypoint>  
  84.         <omgdi:waypoint x="466.0" y="309.0"></omgdi:waypoint>  
  85.         <omgdi:waypoint x="389.0" y="309.0"></omgdi:waypoint>  
  86.         <omgdi:waypoint x="325.0" y="309.0"></omgdi:waypoint>  
  87.         <omgdi:waypoint x="325.0" y="233.0"></omgdi:waypoint>  
  88.         <bpmndi:BPMNLabel>  
  89.           <omgdc:Bounds height="14.0" width="48.0" x="469.0" y="267.0"></omgdc:Bounds>  
  90.         </bpmndi:BPMNLabel>  
  91.       </bpmndi:BPMNEdge>  
  92.       <bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">  
  93.         <omgdi:waypoint x="467.0" y="185.0"></omgdi:waypoint>  
  94.         <omgdi:waypoint x="467.0" y="125.0"></omgdi:waypoint>  
  95.         <bpmndi:BPMNLabel>  
  96.           <omgdc:Bounds height="14.0" width="48.0" x="468.0" y="166.0"></omgdc:Bounds>  
  97.         </bpmndi:BPMNLabel>  
  98.       </bpmndi:BPMNEdge>  
  99.       <bpmndi:BPMNEdge bpmnElement="flow9" id="BPMNEdge_flow9">  
  100.         <omgdi:waypoint x="520.0" y="97.0"></omgdi:waypoint>  
  101.         <omgdi:waypoint x="600.0" y="97.0"></omgdi:waypoint>  
  102.       </bpmndi:BPMNEdge>  
  103.       <bpmndi:BPMNEdge bpmnElement="flow10" id="BPMNEdge_flow10">  
  104.         <omgdi:waypoint x="705.0" y="97.0"></omgdi:waypoint>  
  105.         <omgdi:waypoint x="750.0" y="97.0"></omgdi:waypoint>  
  106.       </bpmndi:BPMNEdge>  
  107.     </bpmndi:BPMNPlane>  
  108.   </bpmndi:BPMNDiagram>  
  109. </definitions>  

代码实现:


[java]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. import java.io.BufferedReader;  
  2. import java.io.IOException;  
  3. import java.io.InputStream;  
  4. import java.io.InputStreamReader;  
  5. import java.util.HashMap;  
  6. import java.util.Map;  
  7. import java.util.zip.ZipInputStream;  
  8.   
  9. import org.activiti.engine.FormService;  
  10. import org.activiti.engine.ProcessEngine;  
  11. import org.activiti.engine.ProcessEngineConfiguration;  
  12. import org.activiti.engine.RepositoryService;  
  13. import org.activiti.engine.RuntimeService;  
  14. import org.activiti.engine.TaskService;  
  15. import org.activiti.engine.repository.Deployment;  
  16. import org.activiti.engine.repository.ProcessDefinition;  
  17. import org.activiti.engine.repository.ProcessDefinitionQuery;  
  18. import org.activiti.engine.runtime.ProcessInstance;  
  19. import org.activiti.engine.task.Task;  
  20.   
  21. import com.tgb.itoo.activiti.controller.CommonDelegate;  
  22. import com.tgb.itoo.activiti.controller.milaoshi;  
  23. import com.tgb.itoo.activiti.controller.tanghuan;  
  24.   
  25. public class formprocess {  
  26.   
  27.     private static String readDataFromConsole(String prompt) {  
  28.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));  
  29.         String str = null;  
  30.         try {  
  31.             System.out.print(prompt);  
  32.             str = br.readLine();  
  33.   
  34.         } catch (IOException e) {  
  35.             e.printStackTrace();  
  36.         }  
  37.         return str;  
  38.     }  
  39.   
  40.     public static void main(String[] args) {  
  41.   
  42.         String str = readDataFromConsole("Please input string:");  
  43.         System.out.println("The information from console: " + str);  
  44.           
  45.         ProcessEngineConfiguration processEngineConfiguration = ProcessEngineConfiguration  
  46.                 .createStandaloneProcessEngineConfiguration();  
  47.         // 连接数据库的配置  
  48.         processEngineConfiguration.setJdbcDriver("com.mysql.jdbc.Driver");  
  49.         processEngineConfiguration  
  50.                 .setJdbcUrl("jdbc:mysql://localhost:3306/activitiexam?useUnicode=true&characterEncoding=utf8");  
  51.         processEngineConfiguration.setJdbcUsername("root");  
  52.         processEngineConfiguration.setJdbcPassword("root");  
  53.         processEngineConfiguration.setDatabaseSchemaUpdate("create");  
  54.         ProcessEngine processEngine = processEngineConfiguration  
  55.                 .buildProcessEngine();  
  56.   
  57.         System.out.println(processEngine);  
  58.         // 获取流程存储服务组件  
  59.         RepositoryService repositoryService = processEngine  
  60.                 .getRepositoryService();  
  61.   
  62.         // 获取运行时服务组件  
  63.         RuntimeService runtimeService = processEngine.getRuntimeService();  
  64.   
  65.         // 获取流程任务组件  
  66.         TaskService taskService = processEngine.getTaskService();  
  67.   
  68.         //获取流程的表单组件  
  69.         FormService formService=processEngine.getFormService();  
  70.           
  71.         // 1、部署流程文件  
  72.         InputStream in =repositoryService.createDeployment().name("MyProcess")  
  73.         .getClass().getClassLoader().getResourceAsStream("diagrams/"+ str +".zip");  
  74.         ZipInputStream zipInputStream = new ZipInputStream(in);  
  75.         Deployment deployment = processEngine.getRepositoryService()// 与流程定义和部署对象相关的Service  
  76.                 .createDeployment()// 创建一个部署对象  
  77.                 .name("流程定义")// 添加部署的名称  
  78.                 .addZipInputStream(zipInputStream)// 指定zip格式的文件完成部署  
  79.                 .deploy();// 完成部署  
  80.                   
  81.           
  82.         // 2、启动流程--方式一  
  83.         /*ProcessInstance processInstance = runtimeService 
  84.                 .startProcessInstanceByKey("myProcess"); 
  85.         */  
  86.           
  87.         /** 
  88.          * 2、启动流程--方式二 
  89.          * 此处使用这种方式,可能有问题,当所有的流程定义的key值均为myProcess时,查询出来的数据为多条 
  90.          * 但是真正使用时,我们是根据前台显示的多条中选择其中之一,就可以直接获取processDefinitionID故也没有问题 
  91.          */  
  92.         ProcessDefinitionQuery query = repositoryService  
  93.                 .createProcessDefinitionQuery()  
  94.                 .processDefinitionKey("myProcess").active()  
  95.                 .orderByProcessDefinitionVersion().desc();  
  96.         ProcessDefinition  processDefinition = query.list().get(0);   
  97.         Map<String, String> formProperties = new HashMap<String, String>();  
  98.           
  99.         formProperties.put("numberOfDays""20150601");  
  100.         //此处如果第二个参数为null值则报错,即使不放入任何数据也要使用map格式的对象  
  101.         ProcessInstance processInstance = formService  
  102.                 .submitStartFormData(processDefinition.getId(), formProperties);  
  103.   
  104.         String end="1";  
  105.         while (end.equalsIgnoreCase("1")) {  
  106.               
  107.             // 3、查询任务  
  108.             Task task = taskService.createTaskQuery()  
  109.                     .processInstanceId(processInstance.getId()).singleResult();  
  110.             ;             
  111.                           
  112.             if (task != null) {  
  113.                 Object renderedTaskForm = formService.getRenderedTaskForm(task.getId());  
  114.                 System.out.println("表单内容---"+renderedTaskForm.toString());  
  115.                 System.out.println("任务名称---"+task.getName());                             
  116.                   
  117.                 /** 
  118.                  * 如果整个流程都是顺序执行可以不暂停的,则去掉此段代码即可 
  119.                  * 注:现实中存在的是页面流转过程中是需要点击下一步操作的,可以将此暂停理解为下一步的操作 
  120.                  */  
  121.                 str = readDataFromConsole("Please input next:");  
  122.                 Map<String, Object> variables = new HashMap<String, Object>();  
  123.                 variables.put("result", str);  
  124.                   
  125.                 //方式一启动流程的完成任务,如果下一个任务需要参数使用map传递,此处的变量均是给下一个任务设置的  
  126.                 taskService.complete(task.getId(),variables);  
  127.                 //方式二启动流程完成任务  
  128.                 //formService.submitTaskFormData(task.getId(), formProperties);  
  129.                   
  130.                 //注:以上的两种方式可以混用  
  131.             }else {  
  132.                 end="0";  
  133.                 System.out.println("任务已经完成");  
  134.             }   
  135.   
  136.         }  
  137.   
  138.     }  
  139.   
  140. }  

执行结果:




总结:

 

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值