📝1.增加配置
pom依赖
java
<!--springboot-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!--activiti-->
<!-- Activiti启动器 需要排除mybatis依赖 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter-basic</artifactId>
<version>5.22.0</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Activiti流程图 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-diagram-rest</artifactId>
<version>5.22.0</version>
</dependency>
<!-- Activiti在线设计器 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-modeler</artifactId>
<version>5.22.0</version>
</dependency>
<!--mysql-->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<!--mybatis-->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.3.1</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>4.5.16</version>
</dependency>
<!--缓存-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
增加数据库及redis配置文件
java
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/woniu-activiti?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&rewriteBatchedStatements=true&useAffectedRows=true&nullCatalogMeansCurrent=true
username: root
password:
type: com.zaxxer.hikari.HikariDataSource
hikari:
maximum-pool-size: 50 # 连接池最大连接数,默认是10
minimum-idle: 5 #最小空闲连接数量
idle-timeout: 18000 #空闲连接存活最大时间,默认600000(10分钟)
pool-name: woniuHikariCP #连接池名称
connection-test-query: SELECT 1
redis:
host: 127.0.0.1
port: 6379
timeout: 3s
# 配置slq打印日志
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath*:/mapper/**/*.xml
📜 2.启动类ActivitiDesignApplication排除安全校验注解
java
@EnableAutoConfiguration(exclude = {org.activiti.spring.boot.SecurityAutoConfiguration.class,
org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class })
启动项目后将会自动在数据库中生成表
📘3.开始整合在线设计器
官网下载源码包
如果没有梯子下载太慢可以在gitee中将github导入再下载
导入仓库时需要用仓库地址而不是具体的分支地址
复制这些文件
将activiti-explorer2包下的diagram-viewer、editor-app文件夹拷贝到项目\resources\static文件下;
将modeler.html拷贝到\resources\templates下;复制resources下stencilset.json到自己的resources下
复制
读取文字文件stencilset.json controller
StencilsetRestResource.java
读取流程文件 controller
ModelEditorJsonRestResource.java
1.页面
文字文件 汉化需要改这个文件
2.后端接口
读取文字文件stencilset.json controller
Activiti-5.22.0\modules\activiti-modeler\src\main\java\org\activiti\rest\editor\main\StencilsetRestResource.java
读取流程文件
Activiti-5.22.0\modules\activiti-modeler\src\main\java\org\activiti\rest\editor\model\ModelEditorJsonRestResource.java 这两个文件是后端需要的,模型创建的文件的接口需要自己写。
3.自己新建一个模型创建接口ActivitiModelController
java
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;
import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ActivitiException;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.apache.batik.transcoder.TranscoderInput;
import org.apache.batik.transcoder.TranscoderOutput;
import org.apache.batik.transcoder.image.PNGTranscoder;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
/**
* @ClassName: ActivitiModelController
* @Description: 工作流引擎控制器
* @Author: woniu
* @Date: 2013/11/17
**/
@Controller
@RequestMapping("model")
@Slf4j
public class ActivitiModelController {
@Autowired
private RepositoryService repositoryService;
@Autowired
private ObjectMapper objectMapper;
/**
* 新建流程
* @param request
* @param response
*/
@RequestMapping("/createModel")
public void createModel(HttpServletRequest request, HttpServletResponse response) {
try {
ObjectMapper objectMapper = new ObjectMapper();
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);
Model modelData = repositoryService.newModel();
ObjectNode modelObjectNode = objectMapper.createObjectNode();
modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, "name");
modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, "description");
modelData.setMetaInfo(modelObjectNode.toString());
modelData.setName("name");
modelData.setKey(StringUtils.defaultString("key"));
repositoryService.saveModel(modelData);
repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));
request.setAttribute("modelId", modelData.getId());
response.sendRedirect(request.getContextPath() + "/modeler.html?modelId=" + modelData.getId());
} catch (Exception e) {
log.error(e.getMessage(), e);
}
}
/**
* @Author sunt
* @Description 保存流程
* @Date 11:42 2019/7/2
* @Param [modelId, name, json_xml, svg_xml, description]
* @return void
**/
@PutMapping(value = { "/{modelId}/save" })
@ResponseStatus(HttpStatus.OK)
public void saveModel(@PathVariable String modelId, @RequestParam("name") String name,
@RequestParam("json_xml") String json_xml, @RequestParam("svg_xml") String svg_xml,
@RequestParam("description") String description) {
try {
Model model = this.repositoryService.getModel(modelId);
ObjectNode modelJson = (ObjectNode) this.objectMapper.readTree(model.getMetaInfo());
modelJson.put("name", name);
modelJson.put("description", description);
model.setMetaInfo(modelJson.toString());
model.setName(name);
this.repositoryService.saveModel(model);
this.repositoryService.addModelEditorSource(model.getId(),json_xml.getBytes("utf-8"));
InputStream svgStream = new ByteArrayInputStream(svg_xml.getBytes("utf-8"));
TranscoderInput input = new TranscoderInput(svgStream);
PNGTranscoder transcoder = new PNGTranscoder();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
TranscoderOutput output = new TranscoderOutput(outStream);
transcoder.transcode(input, output);
byte[] result = outStream.toByteArray();
this.repositoryService.addModelEditorSourceExtra(model.getId(), result);
outStream.close();
} catch (Exception e) {
throw new ActivitiException("Error saving model", e);
}
}
}
📒4.修改
1.修改 app-cfg.js
java
ACTIVITI.CONFIG = {
// 'contextRoot' : '/activiti-explorer/service',
'contextRoot' : '',
};
2.设计器汉化
将stencilset.json内容替换,原内容是英文
替换为
🔖5.最终界面展示
通过访问Controller跳转到流程创建页面 http://127.0.0.1:8080/model/createModel
🧣注意事项tips
📚6.最后的话
🖲要熟练掌握技巧,一定多多练习:纸上得来终觉浅,绝知此事要躬行。