比如说现在申请人需要往下一个审批人传一些值告诉他我是谁谁谁,然后审批人获取该值,那么可以这么做,具体操作如下:
1) 使用processEngine.getTaskService().setVariable(String taskId, String variableName, Object value)来设置传递值:
在上一篇原有上加processEngine.getTaskService().setVariable("57505","name", "我是menco");//第一个为任务id,第二个为所谓的自定义标示key,第三个为value
@Test
publicvoid completeTask(){
Map<String,Object> map= newHashMap<String,Object>();
map.put("user", "cwh");
map.put("day",3);
String taskId = "57505";
processEngine.getTaskService().setVariable(taskId, "name", "我是menco");
processEngine.getTaskService().complete(taskId,map);//完成任务
System.out.println("完成任务,任务ID"+taskId);
}
这样提交之后经理审批就可以同variableName来拿到对应的value了
2) 使用processEngine.getTaskService().getVariable(String taskId, StringvariableName)来获取值
@Test
publicvoid getVariable(){
TaskService taskService = processEngine.getTaskService();
String taskid = "55003";
String name =(String) taskService.getVariable(taskid, "name");//“name”对应前面set的variableName
System.out.println(name);
}
运行后得到:
Ok这样就可以了,但是平时我们需要的是一个对象来传递,比如申请人在页面填写申请表单,然后提交上去,这样的话我们后台传的是一个表单对象,那么对应对象怎么传值取值,其实和上面差不多,不过要注意一点就是传的对象类需要实现系列化Serializable,具体操作如下:
1) 创建一个taskform.java:
package com.cwh.activiti;
import java.io.Serializable;
publicclass Taskform implements Serializable{
privatestaticfinallongserialVersionUID= 8571222190258948731L;
privateintday;
private String type;
private String reason;
public Taskform(int day, String type, String reason) {
super();
this.day = day;
this.type = type;
this.reason = reason;
}
publicint getDay() {
returnday;
}
publicvoid setDay(intday) {
this.day = day;
}
public String getType() {
returntype;
}
publicvoid setType(String type) {
this.type = type;
}
public String getReason() {
returnreason;
}
publicvoid setReason(String reason) {
this.reason = reason;
}
@Override
public String toString() {
return"Taskform[day=" + day + ", type="+ type+ ", reason=" + reason
+ "]";
}
}
2) 操作提交申请
@Test
publicvoid completeTask(){
Map<String,Object> map= newHashMap<String,Object>();
Taskform taskform = new Taskform(3, "事假", "今天老子不想上班");
map.put("user", "cwh");
map.put("day", taskform.getDay());
String taskId = "57505";
processEngine.getTaskService().setVariable(taskId,"taskform", taskform);
processEngine.getTaskService().complete(taskId,map);//完成任务
System.out.println("完成任务,任务ID"+taskId);
}
3) 获取对象值:
/**
* 获取流程变量
*/
@Test
publicvoid getVariable(){
TaskService taskService = processEngine.getTaskService();
String taskid = "65006";
Taskform taskform =(Taskform) taskService.getVariable(taskid, "taskform");
System.out.println(taskform.toString());
}
运行后: