天天看点

【activiti6】设计器的前后端集成与汉化

前言

在使用了官方提供的demo[activiti-app]后,可以考虑将官方的提供的activiti6的设计器集成到springboot项目中使用。网上大多数教程都是activiti6+activit5.22的设计器集成方案,而非activiti6+activiti6设计器。其原因还算因为activiti6设计器新增的新特性使用了新的数据表和新的持久层代码,而这部分新特性不属于官方维护,因此activiti-app中的表单引用、决策表等在纯activiti-engin是没有的,即使在activti7中也找不到这部分代码

区别

先说一下activiti5.22的设计器与activit6设计器的区别:

设计器版本/区别 activiti5.22 activiti6
模型保存数据表 act_re_model act_de_model
持久层实现方式 自己封装的持久层代码 使用jpa1.x保存
前端 可独立运行不需要登录认证 不可独立运行,需要登录权限认证

即使在activiti7中,默认保存模型也是在

act_re_model

表中,而activiti6的

activiti-engin

中提供的

RepositoryService

的保存模型方法也是在

act_re_model

表中,给人的感觉不是一拨人在维护这个代码,而实际上activiti6中的表单引用和决策表都需要将

act_re_model

进行扩展,于是就把

act_re_model

拓展成了

act_de_model

。所以一般都是使用的5.22的设计器,它集成起来更友好,方便,前端设计器拖进去就能直接使用,只需要实现几个操作模型的接口,并且不需要强行引入jpa作为持久层

集成需要做的事

  1. 将activiti-app中的jpa版本升级到2.x以上,因为jpa1.x中的

    findOne

    方法在2.x版本中被移除了,需要将所有jpa在2.x被移除的方法都修改过来(比如

    findOne

    ),强行依赖上去会出现

    NoSuchMethodException

  2. 修改activiti6设计器中对登录权限这部分限制代码
  3. 汉化实际上是修改

    stencilset_bpmn.json

    文件的内容,将文件中对应

    title

    description

    翻译过来,这个文件不可直接用5.22的设计器替代,因为新增了很多新特性,会导致显示不正常

集成步骤

  1. 升级JPA

    首先将activiti6的源码从gitlab上面拉到本地,进入modules/activiti-ui

    【activiti6】设计器的前后端集成与汉化
    编辑

    activiti-ui-root

    的pom文件,指定jpa的版本:
<dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
      <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-commons</artifactId>
        <version>2.2.6.RELEASE</version>
      </dependency>
           

这个时候重新打包就会出现编译问题,因为升级过后

findOne

方法被移除了,需要一个一个的修改过来,比如:

【activiti6】设计器的前后端集成与汉化

修改完毕后将

activiti-ui-root

执行打包命令:

mvn install -DskipTests

这个时候就能在自己的springboot项目中放肆的引用它了:

<dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-app-rest</artifactId>
            <version>6.0.1-SNAPSHOT</version>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
                <exclusion>
                    <groupId>org.jadira.usertype</groupId>
                    <artifactId>usertype.core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
           

注意:两个排除项都是必须的,不然会出现依赖冲突

2. 改造前端编辑器代码

进入

/modules/actitivi-ui/activiti-app

下:

【activiti6】设计器的前后端集成与汉化

全局删除angularJS中的权限处理代码,比如:

【activiti6】设计器的前后端集成与汉化

移除的字符串为:

resolve: {
     verify: authRouteResolver
}
           

替换完毕后,将整个webapp下的文件移动到自己项目resource/static下面,最后启动自己的项目,并访问:

http://localhost:8080/app/editor/index.html#/editor/d97598f8-86b1-484e-9c9a-479360f267d9

,如果没有被跳转到登录页面,那就是成功了

3. 编写重定向控制层代码

由于编辑器默认就是修改操作,需要先创建已有数据才能进入,因此不能粗暴直接访问编辑器页面,需要新增如下控制层代码做重定向:

@RestController
@RequestMapping("/model")
public class ActivitiController {
    @Autowired
    private ModelsResource modelsResource;

    @RequestMapping("/create")
    public void createModelNew(HttpServletRequest request, HttpServletResponse response){
        try{
            String modelName = "modelName"+ LocalDateTime.now();
            String modelKey = "modelKey"+ LocalDateTime.now();
            String description = "description";
            ModelRepresentation modelRepresentation = new ModelRepresentation();
            modelRepresentation.setName(modelName);
            modelRepresentation.setKey(modelKey);
            modelRepresentation.setDescription(description);
            modelRepresentation.setModelType(AbstractModel.MODEL_TYPE_BPMN);
            ModelRepresentation model = modelsResource.createModel(modelRepresentation);
            response.sendRedirect(request.getContextPath() + "/app/editor/index.html#/editor/" + model.getId());
        }catch (Exception e){
        }
    }
}
           

再将项目地址修改为

/app

,因为设计器所有请求都是app开头,然而activiti-app-rest的代码是以rest开头的,不加app会导致所有请求404,而他原来代码中通过springMVC的配置类,指定了不同请求的处理规则(

org.activiti.app.servlet.WebConfigurer

):

/**
     * Initializes Spring and Spring MVC.
     */
    private void initSpring(ServletContext servletContext, AnnotationConfigWebApplicationContext rootContext) {
        log.debug("Configuring Spring Web application context");
        AnnotationConfigWebApplicationContext appDispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
        appDispatcherServletConfiguration.setParent(rootContext);
        appDispatcherServletConfiguration.register(AppDispatcherServletConfiguration.class);

        log.debug("Registering Spring MVC Servlet");
        ServletRegistration.Dynamic appDispatcherServlet = servletContext.addServlet("appDispatcher", 
                new DispatcherServlet(appDispatcherServletConfiguration));
        appDispatcherServlet.addMapping("/app/*");
        appDispatcherServlet.setLoadOnStartup(1);
        appDispatcherServlet.setAsyncSupported(true);

        log.debug("Registering Activiti public REST API");
        AnnotationConfigWebApplicationContext apiDispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
        apiDispatcherServletConfiguration.setParent(rootContext);
        apiDispatcherServletConfiguration.register(ApiDispatcherServletConfiguration.class);

        ServletRegistration.Dynamic apiDispatcherServlet = servletContext.addServlet("apiDispatcher",
                new DispatcherServlet(apiDispatcherServletConfiguration));
        apiDispatcherServlet.addMapping("/api/*");
        apiDispatcherServlet.setLoadOnStartup(1);
        apiDispatcherServlet.setAsyncSupported(true);
    }
           
  1. 汉化处理

    直接将stencilset_bpmn.json添加到resource目录即可

完整项目地址已上传到github:https://github.com/Siwash/activiti6-start