Activiti moduler + spring web环境构建(二)
项目结构:基于activiti 5.21版本,可下载附件demo项目
maven 配置:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.activiti.examples</groupId> <artifactId>activiti-examples</artifactId> <version>1.0-SNAPSHOT</version> <packaging>jar</packaging> <name>BPMN 2.0 with Activiti - Examples</name> <properties> <activiti-version>5.21.0</activiti-version> <spring-version>4.3.3.RELEASE</spring-version> </properties> <dependencies> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-engine</artifactId> <version>${activiti-version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-modeler</artifactId> <exclusions> <exclusion> <groupId>xalan</groupId> <artifactId>xalan</artifactId> </exclusion> </exclusions> <version>${activiti-version}</version> </dependency> <!-- 查看流程详细定义 --> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-diagram-rest</artifactId> <version>${activiti-version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-explorer</artifactId> <exclusions> <exclusion> <artifactId>vaadin</artifactId> <groupId>com.vaadin</groupId> </exclusion> <exclusion> <artifactId>dcharts-widget</artifactId> <groupId>org.vaadin.addons</groupId> </exclusion> <exclusion> <artifactId>activiti-simple-workflow</artifactId> <groupId>org.activiti</groupId> </exclusion> </exclusions> <version>${activiti-version}</version> </dependency> <dependency> <groupId>org.activiti</groupId> <artifactId>activiti-spring</artifactId> <version>${activiti-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-tx</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-beans</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${spring-version}</version> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>provided</scope> </dependency> <dependency> <groupId>javax.servlet</groupId> <artifactId>jstl</artifactId> <version>1.2</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <version>5.1.39</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.6</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-jdk14</artifactId> <version>1.7.6</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>2.3.2</version> <configuration> <source>1.6</source> <target>1.6</target> </configuration> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-eclipse-plugin</artifactId> <inherited>true</inherited> <configuration> <classpathContainers> <classpathContainer>org.eclipse.jdt.USER_LIBRARY/Activiti Designer Extensions</classpathContainer> </classpathContainers> </configuration> </plugin> </plugins> </build> </project>
web配置
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <distributable /> <!-- To load the Spring context --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <!-- 编码过滤器 放在filter的第一位最佳--> <filter> <filter-name>encodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--springMVC配置,该servlet会拦截所有匹配的URL请求--> <servlet> <servlet-name>springMVC</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:springMVC.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>springMVC</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!-- Session timeout on one day --> <session-config> <session-timeout>480</session-timeout> </session-config> </web-app>
spring基础配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:property-placeholder location="classpath:db.properties" file-encoding="utf-8" ignore-unresolvable="true"/> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${jdbc.driver}"/> <property name="url" value="${jdbc.url}"/> <property name="username" value="${jdbc.username}"/> <property name="password" value="${jdbc.password}"/> <property name="defaultAutoCommit" value="false"/> </bean> <bean id="processEngineConfiguration" class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration"> <property name="dataSource" ref="dataSource"/> <property name="databaseType" value="${db}"/> <property name="databaseSchemaUpdate" value="true"/> <property name="jobExecutorActivate" value="false"/> <!--<property name="enableDatabaseEventLogging" value="true" /> <property name="customFormTypes"> <list> <bean class="org.activiti.explorer.form.UserFormType"/> <bean class="org.activiti.explorer.form.ProcessDefinitionFormType"/> <bean class="org.activiti.explorer.form.MonthFormType"/> </list> </property>--> </bean> <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean"> <property name="processEngineConfiguration" ref="processEngineConfiguration"/> </bean> <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/> <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/> <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService"/> <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService"/> <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService"/> <bean id="identityService" factory-bean="processEngine" factory-method="getIdentityService" /> <bean id="formService" factory-bean="processEngine" factory-method="getFormService" /> <!-- json处理,这里不可改成其他类,有依赖关系 --> <bean id="objectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"></bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> </beans>
spring MVC配置
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd "> <!--配置扫描controller包,注册pojo (org.activiti.rest.editor,org.activiti.rest.diagram)--> <context:component-scan base-package="com.bpm.controller,org.activiti.rest.editor"></context:component-scan> <!--使用注解的方式--> <mvc:annotation-driven/> <!-- 避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动Spring MVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter"/><!-- json转换器 --> </list> </property> </bean> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/> <property name="prefix" value="/WEB-INF/views/"/> <property name="suffix" value=".jsp"></property> </bean> <!-- 资源访问处理器 --> <mvc:resources mapping="/resources/**" location="/resources/" /> </beans>
基础操作
@RequestMapping(value = "/create", method = RequestMethod.POST, produces = "application/json;charset=utf-8")
public String create(HttpServletRequest request, HttpServletResponse response) {
String id = "";
try {
String name = request.getParameter("name"),
key = request.getParameter("key"),
description = request.getParameter("category");
// 元数据信息
ObjectNode modelObjectNode = objectMapper.createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, name);
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION,
org.apache.commons.lang3.StringUtils
.defaultString(description));
Model newModel = repositoryService.newModel();
newModel.setMetaInfo(modelObjectNode.toString());
newModel.setName(name);
newModel.setKey(key);
newModel.setCategory(description);
repositoryService.saveModel(newModel);
// 创建所必须的JSON数据
ObjectNode editorNode = objectMapper.createObjectNode();
editorNode.put("id", "canvas");
editorNode.put("resourceId", "canvas");
ObjectNode stencilSetNode = objectMapper.createObjectNode();
stencilSetNode.put("namespace", "http://b3mn.org/stencilset/bpmn2.0#");
editorNode.put("stencilset", stencilSetNode);
repositoryService.addModelEditorSource(newModel.getId(), editorNode.toString().getBytes("utf-8"));
id = newModel.getId();
} catch (Exception e) {
e.printStackTrace();
}
return "redirect:/demo/editor?modelId=" + id;
}
@RequestMapping("/deploy/{modelId}")
@ResponseBody
public Result deploy(@PathVariable String modelId) throws IOException {
Result result = new Result();
try {
// 将创建的模型转为json
JsonNode editorNode = new ObjectMapper().readTree(repositoryService.getModelEditorSource(modelId));
BpmnJsonConverter jsonConverter = new BpmnJsonConverter();
BpmnModel modelCt = jsonConverter.convertToBpmnModel(editorNode);
// 获取流程模型
Model model = repositoryService.createModelQuery().modelId(modelId).singleResult();
// 发布流程模型
String processName = model.getName() + ".bpmn20.xml";
repositoryService.createDeployment().name(model.getName()).addBpmnModel(processName, modelCt).deploy();
} catch (Exception e) {
e.printStackTrace();
result.setSuccess(false);
result.setMsg(e.getMessage());
}
return result;
}
@RequestMapping("/process/start/{processDefinitionId}")
@ResponseBody
public Map<String, Object> startProcess(@PathVariable String processDefinitionId, ModelMap mav, HttpServletRequest request, HttpServletResponse response) throws IOException {
Map<String, Object> result = new HashMap<String, Object>();
result.put("success", true);
try {
Object obj = request.getSession().getAttribute("user");
// 查询是否有表单,有的话弹出表单,否则的话直接提交任务
FormDataImpl formData = (FormDataImpl) formService.getStartFormData(processDefinitionId);
List<FormProperty> propertyList = formData.getFormProperties();
result.put("hasForm", propertyList.isEmpty() ? false : true);
if (propertyList.isEmpty()) {
// 定义map用于往工作流数据库中传值。
Map variables = new HashMap();
variables.put("applyUserName", ((User) obj).getFirstName());
identityService.setAuthenticatedUserId(((User) obj).getId());
// 启动实例
runtimeService.startProcessInstanceById(processDefinitionId, variables);
}
} catch (Exception e) {
e.printStackTrace();
result.put("success", false);
result.put("msg", e.getMessage());
}
return result;
}
/**
* 获取表单用于展示
*
* @param type start 表示启动时的表单;task 表示任务中的表单
* @param id 类型为start时,为流程定义ID;类型为task时,为任务ID
* @param modelMap 返回前台数据
* @return String 页面
* @author DS
*/
@RequestMapping(value = "/process/getForm/{type}/{id}")
public String getForm(@PathVariable String type, @PathVariable String id, ModelMap modelMap) {
FormDataImpl formData = null;
if ("start".equals(type)) {
formData = (FormDataImpl) formService.getStartFormData(id);
} else if ("task".equals(type)) {
formData = (FormDataImpl) formService.getTaskFormData(id);
}
// 获取属性设置
List<FormProperty> formProperties = formData.getFormProperties();
Object values;
Map<String,Object> attributes = new HashMap<String, Object>();
for (FormProperty formProperty : formProperties) {
if("date".equals(formProperty.getType().getName()) || "enum".equals(formProperty.getType().getName())){
values = formProperty.getType().getInformation("values");
if (values != null) {
attributes.put(formProperty.getId(), values);
}else {
values = formProperty.getType().getInformation("datePattern");
if (values != null){
attributes.put(formProperty.getId(),values);
}
}
}
}
modelMap.put("attributes",attributes);
modelMap.put("formList", formProperties);
modelMap.put("type",type);
modelMap.put("id",id);
return "apply";
}
/**
* 保存表单数据
*
* @param type start 表示启动时的表单;task 表示任务中的表单
* @param id 类型为start时,为流程定义ID;类型为task时,为任务ID
* @param request 请求
* @param response 响应
* @return Result 保存结果
* @author DS
*/
@RequestMapping(value = "/process/saveForm/{type}/{id}")
@ResponseBody
public Result submitStartFormAndStartProcessInstance(@PathVariable String type, @PathVariable("id") String id,
HttpServletRequest request, HttpServletResponse response) throws IOException {
Result result = new Result();
Map<String, String> formProperties = new HashMap<String, String>();
// 从request中读取参数然后转换
Map<String, String[]> parameterMap = request.getParameterMap();
Set<String> keySet = parameterMap.keySet();
for (String key : keySet) {
formProperties.put(key, parameterMap.get(key)[0]);
}
// 用户未登录不能操作,实际应用使用权限框架实现,例如Spring Security、Shiro等
Object obj = request.getSession().getAttribute("user");
try {
if("start".equals(type)){
formProperties.put("applyUserName",((User)obj).getFirstName());
formProperties.put("applyUserId",((User)obj).getId());
identityService.setAuthenticatedUserId(((User)obj).getId());
ProcessInstance processInstance = formService.submitStartFormData(id, formProperties);
logger.debug("流程启动成功: {}", processInstance.getId());
}else if ("task".equals(type)){
formService.submitTaskFormData(id,formProperties);
logger.debug("流程进入下一步");
}
}catch (Exception e){
result.setSuccess(false);
result.setMsg(e.getMessage());
e.printStackTrace();
}finally {
identityService.setAuthenticatedUserId(null);
}
result.setResult(id);
return result;
}