1.修改在线编辑器的项目访问名称与项目名一致(qfxActivitiExplorerDemo是我现在的项目名称),如图:

springMVC集成activiti-explorer5.22(二)_第1张图片

2.创建整合在线编辑器需要的java类,如图

springMVC集成activiti-explorer5.22(二)_第2张图片

2.1 在线编辑器入口类:Test.java
package com.hello;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.activiti.editor.constants.ModelDataJsonConstants;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Model;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;

@Controller  
@Scope("prototype")   
@RequestMapping("/user")  
public class Test  {
        /**
         * 创建模型
         */
        @RequestMapping("create")
        public void create(HttpServletRequest request, HttpServletResponse response) {
                try {
                    ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();

                    RepositoryService repositoryService = processEngine.getRepositoryService();

                        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, "新建流程");
                        modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);
                        String description = "请输入流程描述信息~";
                        modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION, description);
                        modelData.setMetaInfo(modelObjectNode.toString());
                        modelData.setName("lutiannan");
                        modelData.setKey("12313123");

                        //保存模型
                        repositoryService.saveModel(modelData);
                        repositoryService.addModelEditorSource(modelData.getId(), editorNode.toString().getBytes("utf-8"));

                        // 编辑流程模型时,只需要直接跳转此url并传递上modelId即可
                        response.sendRedirect(request.getContextPath() + "/views/activiti-edit/modeler.html?modelId=" + modelData.getId());
                } catch (Exception e) {
                        System.out.println("创建模型失败:");
                }
        }
}
2.2 在线编辑器读取核心文件 ModelEditorJsonRestResource.java
package com.rest.editor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
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.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ModelEditorJsonRestResource implements ModelDataJsonConstants
{
    protected static final Logger LOGGER = LoggerFactory.getLogger(ModelEditorJsonRestResource.class);

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping(value={"/service/model/{modelId}/json"}, method={org.springframework.web.bind.annotation.RequestMethod.GET}, produces={"application/json"})
    public ObjectNode getEditorJson(@PathVariable String modelId) { ObjectNode modelNode = null;

        System.out.println("---------------------------ModelEditorJsonRestResource.getEditorJson---------");
        Model model = this.repositoryService.getModel(modelId);

        if (model != null) {
            try {
                if (StringUtils.isNotEmpty(model.getMetaInfo())) {
                    modelNode = (ObjectNode)this.objectMapper.readTree(model.getMetaInfo());
                } else {
                    modelNode = this.objectMapper.createObjectNode();
                    modelNode.put("name", model.getName());
                }
                modelNode.put("modelId", model.getId());
                ObjectNode editorJsonNode = (ObjectNode)this.objectMapper.readTree(new String(this.repositoryService
                    .getModelEditorSource(model
                    .getId()), "utf-8"));
                modelNode.put("model", editorJsonNode);
            }
            catch (Exception e) {
                LOGGER.error("Error creating model JSON", e);
                throw new ActivitiException("Error creating model JSON", e);
            }
        }
        return modelNode;
    }
}
2.3 在线编辑器保存流程 ModelSaveRestResource.java
package com.rest.editor;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
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.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ModelSaveRestResource implements ModelDataJsonConstants
{
    protected static final Logger LOGGER = LoggerFactory.getLogger(ModelSaveRestResource.class);

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private ObjectMapper objectMapper;

    @RequestMapping(value={"/service/model/{modelId}/save"}, method={org.springframework.web.bind.annotation.RequestMethod.PUT})
    @ResponseStatus(HttpStatus.OK)
    public void saveModel(@PathVariable String modelId, @RequestBody MultiValueMap values) { try { Model model = this.repositoryService.getModel(modelId);
            System.out.println("ModelSaveRestResource.saveModel----------");
            ObjectNode modelJson = (ObjectNode)this.objectMapper.readTree(model.getMetaInfo());

            modelJson.put("name", (String)values.getFirst("name"));
            modelJson.put("description", (String)values.getFirst("description"));
            model.setMetaInfo(modelJson.toString());
            model.setName((String)values.getFirst("name"));

            this.repositoryService.saveModel(model);

            this.repositoryService.addModelEditorSource(model.getId(), ((String)values.getFirst("json_xml")).getBytes("utf-8"));

            InputStream svgStream = new ByteArrayInputStream(((String)values.getFirst("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)
        {
            LOGGER.error("Error saving model", e);
            throw new ActivitiException("Error saving model", e);
        }
    }
}
2.4 读取在线编辑器核心配置文件 StencilsetRestResource.java
package com.rest.editor;

import java.io.InputStream;
import org.activiti.engine.ActivitiException;
import org.apache.commons.io.IOUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class StencilsetRestResource {
    @RequestMapping(value = { "/service/editor/stencilset" }, method = { org.springframework.web.bind.annotation.RequestMethod.GET }, produces = { "application/json;charset=utf-8" })
    @ResponseBody
    public String getStencilset() {
        System.out.println("StencilsetRestResource.getStencilset-----------");

        // 文件位置需要跟stencilset.json文件放置的路径匹配,否则进入到在线编辑器页面会是一片空白,没有菜单等显示信息
        InputStream stencilsetStream = getClass().getClassLoader().getResourceAsStream("activiti/stencilset.json");
        try {
            return IOUtils.toString(stencilsetStream, "utf-8");
        } catch (Exception e) {
            throw new ActivitiException("Error while loading stencil set", e);
        }
    }
}

3.至此,整合完毕,整合并发布你的项目,开始测试吧!

特别注意:
    如果要修改在线编辑器点击“保存并关闭”或者“关闭”功能后,页面是一片空白或者想要关闭当前页面,请修改
     activiti-edit\editor-app\configuration 目录下的 toolbar-default-actions.js 文件

springMVC集成activiti-explorer5.22(二)_第3张图片

3.1修改关闭页面要跳转的页面:
// 关闭编辑页面
closeEditor: function(services) {
        // 跳转到指定页面
        // window.location.href="http://pannijingling.blog.163.com/blog/./";
        // 关闭当前页面
        window.opener = null;
        window.open('', '_self');
        window.close();
},
3.2修改保存并关闭页面要跳转的页面:
// 保存并关闭页面
$scope.saveAndClose = function() {
        $scope.save(function() {
                // 跳转到指定页面
                // window.location.href="http://pannijingling.blog.163.com/blog/./";
                // 关闭当前页面
                window.opener = null;
                window.open('', '_self');
                window.close();
        });
};

4.OK,至此大功告成~最后附上需要的jar包信息

activiti-bpmn-converter-5.22.0.jar
activiti-bpmn-layout-5.22.0.jar
activiti-bpmn-model-5.22.0.jar
activiti-common-rest-5.22.0.jar
activiti-crystalball-5.22.0.jar
activiti-diagram-rest-5.22.0.jar
activiti-engine-5.22.0.jar
activiti-explorer-5.22.0.jar
activiti-image-generator-5.22.0.jar
activiti-json-converter-5.22.0.jar
activiti-modeler-5.22.0.jar
activiti-process-validation-5.22.0.jar
activiti-simple-workflow-5.22.0.jar
activiti-spring-5.22.0.jar
aopalliance-1.0.jar
aspectjrt-1.8.10.jar
aspectjweaver-1.8.10.jar
batik-anim-1.7.jar
batik-awt-util-1.7.jar
batik-bridge-1.7.jar
batik-codec-1.7.jar
batik-css-1.7.jar
batik-dom-1.7.jar
batik-ext-1.7.jar
batik-gvt-1.7.jar
batik-js-1.7.jar
batik-parser-1.7.jar
batik-script-1.7.jar
batik-svg-dom-1.7.jar
batik-svggen-1.7.jar
batik-transcoder-1.7.jar
batik-util-1.7.jar
batik-xml-1.7.jar
c3p0-0.9.5.2.jar
cglib-nodep-3.2.4.jar
commons-collections-3.2.2.jar
commons-dbcp-1.4.jar
commons-fileupload-1.3.2.jar
commons-io-2.0.1.jar
commons-lang3-3.3.2.jar
commons-logging-1.2.jar
commons-pool-1.6.jar
druid-1.0.27.jar
fastjson-1.2.39.jar
jackson-annotations-2.8.6.jar --因为spring采用4.3以上版本,因此升级到此版本以作兼容,否则保存流程模型时会发生异常
jackson-core-2.8.6.jar --因为spring采用4.3以上版本,因此升级到此版本以作兼容,否则保存流程模型时会发生异常
jackson-databind-2.8.6.jar --因为spring采用4.3以上版本,因此升级到此版本以作兼容,否则保存流程模型时会发生异常
javaGeom-0.11.1.jar
jcl-over-slf4j-1.7.6.jar
joda-time-2.6.jar
jsqlparser-1.0.jar
log4j-1.2-api-2.8.2.jar
log4j-api-2.8.2.jar
log4j-core-2.8.2.jar
log4j-web-2.8.2.jar
mchange-commons-java-0.2.12.jar
mybatis-3.3.0.jar
mysql-connector-java-5.1.5-bin.jar
ojdbc7.jar
slf4j-api-1.7.6.jar
slf4j-log4j12-1.7.6.jar
spring-aop-4.3.4.RELEASE.jar
spring-beans-4.3.4.RELEASE.jar
spring-context-4.3.4.RELEASE.jar
spring-core-4.3.4.RELEASE.jar
spring-expression-4.3.4.RELEASE.jar
spring-jdbc-4.3.4.jar
spring-tx-4.3.4.jar
spring-web-4.3.4.RELEASE.jar
spring-webmvc-4.3.4.RELEASE.jar
xml-apis-1.3.04.jar
xml-apis-ext-1.3.04.jar