欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

ACTIVITI 自定义外置表单 formkey JSP

程序员文章站 2022-05-21 14:00:24
...

使用Activiti的时候,发现外外置表单需要将.form文件和流程一起打包部署。

实际应用时,感觉好麻烦。不能将流程和表单完全分离开,现将配置formkey 为JSP文件的做法分享,参考如下:

一、定义java Test FormEngine:
package me.kafeitu.activiti.chapter6.form;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.delegate.VariableScope;
import org.activiti.engine.form.FormData;
import org.activiti.engine.form.StartFormData;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.impl.context.Context;
import org.activiti.engine.impl.form.FormEngine;
import org.activiti.engine.impl.persistence.entity.ResourceEntity;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.activiti.engine.impl.scripting.ScriptingEngines;
import java.io.UnsupportedEncodingException;
public class TestFormEngine implements FormEngine {
private static final String NOT_FOUND_FORM_CONTENT_FLAG = "____NOT_FOUND_FORM___";
public TestFormEngine() {
System.out.print("TestFormEngine init...............");
}
public String getName() {
return "juel";
}
public Object renderStartForm(StartFormData startForm) {
System.out.print("renderStartForm...............");
if(startForm.getFormKey() == null) {
return null;
} else {
String formTemplateString = this.getFormTemplateString(startForm, startForm.getFormKey());
System.out.print("formTemplateString..............."+formTemplateString);
if(NOT_FOUND_FORM_CONTENT_FLAG.equals(formTemplateString)){
return startForm.getFormKey();
}
ScriptingEngines scriptingEngines = Context.getProcessEngineConfiguration().getScriptingEngines();
return scriptingEngines.evaluate(formTemplateString, "juel", (VariableScope)null);
}
}
public Object renderTaskForm(TaskFormData taskForm) {
if(taskForm.getFormKey() == null) {
return null;
} else {
String formTemplateString = this.getFormTemplateString(taskForm, taskForm.getFormKey());
if(NOT_FOUND_FORM_CONTENT_FLAG.equals(formTemplateString)){
return taskForm.getFormKey();
}
ScriptingEngines scriptingEngines = Context.getProcessEngineConfiguration().getScriptingEngines();
TaskEntity task = (TaskEntity)taskForm.getTask();
return scriptingEngines.evaluate(formTemplateString, "juel", task.getExecution());
}
}
protected String getFormTemplateString(FormData formInstance, String formKey) {
String deploymentId = formInstance.getDeploymentId();
ResourceEntity resourceStream = Context.getCommandContext().getResourceEntityManager().findResourceByDeploymentIdAndResourceName(deploymentId, formKey);
if(resourceStream == null) {
// throw new ActivitiObjectNotFoundException("Form with formKey \'" + formKey + "\' does not exist", String.class);
return NOT_FOUND_FORM_CONTENT_FLAG;
} else {
byte[] resourceBytes = resourceStream.getBytes();
String encoding = "UTF-8";
String formTemplateString = "";
try {
formTemplateString = new String(resourceBytes, encoding);
return formTemplateString;
} catch (UnsupportedEncodingException var9) {
throw new ActivitiException("Unsupported encoding of :" + encoding, var9);
}
}
}
}
二、Controller 中的方法如下:
/**
* 读取启动流程的表单字段
*/
@RequestMapping(value = "getform/start/{processDefinitionId}")
public ModelAndView readStartForm(@PathVariable("processDefinitionId") String processDefinitionId) throws Exception {
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionId(processDefinitionId).singleResult();
boolean hasStartFormKey = processDefinition.hasStartFormKey();

// 根据是否有formkey属性判断使用哪个展示层
String viewName = "chapter6/start-process-form";
ModelAndView mav = new ModelAndView(viewName);
// 判断是否有formkey属性
if (hasStartFormKey) {
Object renderedStartForm = formService.getRenderedStartForm(processDefinitionId);
//Object renderedStartForm = formService.getRenderedStartForm(processDefinition.getId(), "juel");

mav.addObject("startFormData", renderedStartForm);
mav.addObject("processDefinition", processDefinition);
} else { // 动态表单字段
StartFormData startFormData = formService.getStartFormData(processDefinitionId);
mav.addObject("startFormData", startFormData);
}
mav.addObject("hasStartFormKey", hasStartFormKey);
mav.addObject("processDefinitionId", processDefinitionId);
return mav;
}
三、JSP中的配置如下:
 
<c:if test="${hasStartFormKey}">
<c:if test="${fn:endsWith(startFormData, '.jsp')}">
<c:import url="${startFormData}" ></c:import>
</c:if>
<c:if test="${!fn:endsWith(startFormData, '.jsp')}">
${startFormData}
</c:if>
</c:if>
四、在流程定义中可以formKey:
<startEvent id="startevent1" name="Start" activiti:initiator="applyUserId" activiti:formKey="chapter6/leave-formkey/leave-start.form"></startEvent>
 
五、activiti.cfg.xml 中的配置如下:
<bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">
   <property name="jdbcUrl" value="jdbc:h2:file:~/activiti-in-action-chapter6;AUTO_SERVER=TRUE" />
   <property name="jdbcDriver" value="org.h2.Driver" />
   <property name="jdbcUsername" value="sa" />
   <property name="jdbcPassword" value="" />
   <property name="databaseSchemaUpdate" value="true" />
   <property name="jobExecutorActivate" value="false" />
<!-- 自定义表单引擎 -->
<property name="customFormEngines">
      <list>
         <bean class="me.kafeitu.activiti.chapter6.form.TestFormEngine" />
      </list>
   </property>
</bean>