首页 > 代码库 > activiti自己定义流程之Spring整合activiti-modeler实例(一):环境搭建

activiti自己定义流程之Spring整合activiti-modeler实例(一):环境搭建

项目中须要整合activiti-modeler自己定义流程,找了非常多资料后,最终成功的跳转到activiti-modeler流程设计界面。下面是记录:

一、整合基础:eclipse4.4.1、tomcat7、jdk1.7、mysql5.6.25、maven3.2.5、activiti5.16.3、spring4.0.9
二、步骤
   1、下载activiti-5.16.3.zip: http://www.activiti.org/download.html
   2、解压zip文件,解压后的文件夹打开例如以下:
    技术分享
   

  3、打开上图中wars,可看到例如以下:
 技术分享
   

   4、把上图中的api、editor、explorer、libs拷贝到项目的webapp下,如图:
 技术分享

   5、进入第三步所看到的的WEB-INF的classes文件夹,可看到如图:
 技术分享

   6、把上图中的editor.html、stencilset.json、plugins.xml拷贝到项目的src/main/sources中,(spring.xml是自己建立的)如图:
 技术分享
  

 7、配置web.xml文件,整个文件内容例如以下:

[html] view plain copy
 技术分享技术分享
  1. <?

    xml version="1.0" encoding="UTF-8"?

    >  

  2. <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.     xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"  
  5.     id="WebApp_ID" version="2.5">      
  6.     <filter>  
  7.         <description>字符集过滤器</description>  
  8.         <filter-name>encodingFilter</filter-name>  
  9.         <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
  10.         <init-param>  
  11.             <description>字符集编码</description>  
  12.             <param-name>encoding</param-name>  
  13.             <param-value>UTF-8</param-value>  
  14.         </init-param>  
  15.     </filter>  
  16.     <filter-mapping>  
  17.         <filter-name>encodingFilter</filter-name>  
  18.         <url-pattern>/*</url-pattern>  
  19.     </filter-mapping>  
  20.   
  21.   
  22.     <servlet>  
  23.     <servlet-name>RestletServlet</servlet-name>  
  24.     <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>  
  25.     <init-param>  
  26.       <param-name>org.restlet.application</param-name>  
  27.       <param-value>org.activiti.rest.editor.application.ModelerRestApplication</param-value>  
  28.     </init-param>  
  29.   </servlet>  
  30.   <servlet-mapping>  
  31.     <servlet-name>RestletServlet</servlet-name>  
  32.     <url-pattern>/service/*</url-pattern>  
  33.   </servlet-mapping>  
  34.       
  35.       
  36.     <servlet>    
  37.         <servlet-name>springMVC</servlet-name>    
  38.         <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>    
  39.         <init-param>    
  40.             <param-name>contextConfigLocation</param-name>    
  41.             <param-value>classpath:spring.xml</param-value>    
  42.         </init-param>      
  43.     </servlet>    
  44.     <servlet-mapping>    
  45.         <servlet-name>springMVC</servlet-name>    
  46.         <url-pattern>*.do</url-pattern>    
  47.     </servlet-mapping>  
  48.     <welcome-file-list>  
  49.         <welcome-file>activiti.html</welcome-file>  
  50.         <welcome-file>activiti.htm</welcome-file>  
  51.         <welcome-file>activiti.jsp</welcome-file>  
  52.         <welcome-file>default.html</welcome-file>  
  53.         <welcome-file>default.htm</welcome-file>  
  54.         <welcome-file>default.jsp</welcome-file>  
  55.     </welcome-file-list>    
  56. </web-app>    


8、使用maven导入须要的pring、activiti以及mysql的jar包。Pom.xml内容例如以下:
[plain] view plain copy
 技术分享技术分享
  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.   <modelVersion>4.0.0</modelVersion>  
  4.   <groupId>activitiTest1</groupId>  
  5.   <artifactId>activitiTest1</artifactId>  
  6.   <packaging>war</packaging>  
  7.   <version>0.0.1-SNAPSHOT</version>  
  8.   <name>activitiTest1 Maven Webapp</name>  
  9.   <url>http://maven.apache.org</url>  
  10.   <dependencies>  
  11.     <dependency>  
  12.       <groupId>junit</groupId>  
  13.       <artifactId>junit</artifactId>  
  14.       <version>3.8.1</version>  
  15.       <scope>test</scope>  
  16.     </dependency>  
  17.     <dependency>  
  18.         <groupId>org.springframework</groupId>  
  19.         <artifactId>spring-beans</artifactId>  
  20.         <version>4.0.9.RELEASE</version>  
  21.     </dependency>  
  22.     <dependency>  
  23.         <groupId>org.springframework</groupId>  
  24.         <artifactId>spring-core</artifactId>  
  25.         <version>4.0.9.RELEASE</version>  
  26.     </dependency>  
  27.     <dependency>  
  28.         <groupId>org.springframework</groupId>  
  29.         <artifactId>spring-aop</artifactId>  
  30.         <version>4.0.9.RELEASE</version>  
  31.     </dependency>  
  32.     <dependency>  
  33.         <groupId>org.springframework</groupId>  
  34.         <artifactId>spring-context</artifactId>  
  35.         <version>4.0.9.RELEASE</version>  
  36.     </dependency>  
  37.     <dependency>  
  38.         <groupId>org.springframework</groupId>  
  39.         <artifactId>spring-webmvc</artifactId>  
  40.         <version>4.0.9.RELEASE</version>  
  41.     </dependency>  
  42.     <dependency>  
  43.         <groupId>org.springframework</groupId>  
  44.         <artifactId>spring-web</artifactId>  
  45.         <version>4.0.9.RELEASE</version>  
  46.     </dependency>  
  47.     <dependency>  
  48.         <groupId>org.springframework</groupId>  
  49.         <artifactId>spring-aspects</artifactId>  
  50.         <version>4.0.9.RELEASE</version>  
  51.     </dependency>  
  52.     <dependency>  
  53.         <groupId>org.springframework</groupId>  
  54.         <artifactId>spring-expression</artifactId>  
  55.         <version>4.0.9.RELEASE</version>  
  56.     </dependency>  
  57.     <dependency>  
  58.         <groupId>org.springframework</groupId>  
  59.         <artifactId>spring-tx</artifactId>  
  60.         <version>4.0.9.RELEASE</version>  
  61.     </dependency>  
  62.     <dependency>  
  63.         <groupId>javax.servlet</groupId>  
  64.         <artifactId>javax.servlet-api</artifactId>  
  65.         <version>3.1.0</version>  
  66.     </dependency>  
  67.     <dependency>  
  68.         <groupId>com.fasterxml.jackson.core</groupId>  
  69.         <artifactId>jackson-annotations</artifactId>  
  70.         <version>2.2.3</version>  
  71.     </dependency>  
  72.     <dependency>  
  73.         <groupId>com.fasterxml.jackson.core</groupId>  
  74.         <artifactId>jackson-core</artifactId>  
  75.         <version>2.2.3</version>  
  76.     </dependency>  
  77.     <dependency>  
  78.         <groupId>com.fasterxml.jackson.core</groupId>  
  79.         <artifactId>jackson-databind</artifactId>  
  80.         <version>2.2.3</version>  
  81.     </dependency>  
  82.     <dependency>  
  83.         <groupId>org.activiti</groupId>  
  84.         <artifactId>activiti-modeler</artifactId>  
  85.         <version>5.16</version>  
  86.     </dependency>  
  87.     <dependency>  
  88.         <groupId>org.activiti</groupId>  
  89.         <artifactId>activiti-engine</artifactId>  
  90.         <version>5.16</version>  
  91.     </dependency>  
  92.     <dependency>  
  93.         <groupId>org.activiti</groupId>  
  94.         <artifactId>activiti-explorer</artifactId>  
  95.         <version>5.16</version>  
  96.     </dependency>  
  97.     <dependency>  
  98.         <groupId>org.activiti</groupId>  
  99.         <artifactId>activiti-rest</artifactId>  
  100.         <version>5.16</version>  
  101.     </dependency>  
  102.     <dependency>  
  103.         <groupId>org.springframework</groupId>  
  104.         <artifactId>spring-context-support</artifactId>  
  105.         <version>4.0.9.RELEASE</version>  
  106.     </dependency>  
  107.     <dependency>  
  108.         <groupId>mysql</groupId>  
  109.         <artifactId>mysql-connector-java</artifactId>  
  110.         <version>5.1.34</version>  
  111.     </dependency>  
  112.   
  113.   
  114.     <dependency>  
  115.         <groupId>org.activiti</groupId>  
  116.         <artifactId>activiti-spring</artifactId>  
  117.         <version>5.16</version>  
  118.     </dependency>  
  119.     <dependency>  
  120.         <groupId>org.springframework</groupId>  
  121.         <artifactId>spring-jdbc</artifactId>  
  122.         <version>4.0.9.RELEASE</version>  
  123.     </dependency>  
  124.   </dependencies>  
  125.   <build>  
  126.     <finalName>activitiTest1</finalName>  
  127.   </build>  
  128. </project>  


9、建立spring的配置文件spring.xml,例如以下:
[html] view plain copy
 技术分享技术分享
  1. <?

    xml version="1.0" encoding="UTF-8"?

    >    

  2. <beans xmlns="http://www.springframework.org/schema/beans"    
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"    
  4.     xmlns:context="http://www.springframework.org/schema/context"    
  5.     xmlns:mvc="http://www.springframework.org/schema/mvc"      
  6.     xsi:schemaLocation="    
  7.         http://www.springframework.org/schema/beans     
  8.         http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    
  9.         http://www.springframework.org/schema/context     
  10.         http://www.springframework.org/schema/context/spring-context-3.0.xsd  
  11.         http://www.springframework.org/schema/mvc   
  12.         http://www.springframework.org/schema/mvc/spring-mvc.xsd">    
  13.   
  14.   
  15.     <context:annotation-config />    
  16.     <mvc:annotation-driven />  
  17.     <context:component-scan base-package="controllers" />    
  18.     <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">  
  19.          
  20.        <property name="driverClass" value="com.mysql.jdbc.Driver"></property>  
  21.        <property name="url" value="jdbc:mysql://192.168.0.201:3306/testtu?

    useUnicode=true&characterEncoding=utf8" ></property>  

  22.        <property name="username" value="root" ></property>   
  23.        <property name="password" value="123456" ></property>   
  24.     </bean>  
  25.       
  26.     <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">  
  27.        <property name="dataSource" ref="dataSource"></property>  
  28.     </bean>  
  29.       
  30.     <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">  
  31.         <property name="dataSource" ref="dataSource" />  
  32.         <property name="databaseSchemaUpdate" value="true" />  
  33.         <property name="jobExecutorActivate" value="false"/>  
  34.         <property name="history" value="full"/>  
  35.         <property name="transactionManager" ref="transactionManager" />  
  36.         <!-- 配置事务管理器。统一事务 -->  
  37.           
  38.         <!-- 设置建表策略,假设没有表,自己主动创建表 -->  
  39.     </bean>  
  40.     <!-- 创建流程引擎对象 -->  
  41.     <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">  
  42.         <property name="processEngineConfiguration" ref="processEngineConfiguration" />  
  43.     </bean>  
  44.   
  45.   
  46.     <!-- 由流程引擎对象。提供的方法,创建项目中使用的Activiti工作流的Service -->  
  47.     <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />  
  48.     <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />  
  49.     <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />  
  50.     <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />  
  51.     <bean id="formService" factory-bean="processEngine" factory-method="getFormService" />  
  52.        
  53.     <!-- 避免IE运行AJAX时,返回JSON出现下载文件 -->  
  54.     <bean id="mappingJacksonHttpMessageConverter"  
  55.         class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">  
  56.         <property name="supportedMediaTypes">  
  57.             <list>  
  58.                 <value>text/html;charset=UTF-8</value>  
  59.             </list>  
  60.         </property>  
  61.     </bean>  
  62.   
  63.   
  64.     <!-- 启动Spring MVC的注解功能,完毕请求和注解POJO的映射 -->  
  65.     <bean  
  66.         class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">  
  67.         <property name="messageConverters">  
  68.             <list>  
  69.                 <ref bean="mappingJacksonHttpMessageConverter" /><!-- json转换器 -->  
  70.             </list>  
  71.         </property>  
  72.     </bean>  
  73.       
  74. </beans>    


10、建立controller类。用来控制页面跳转到activiti-modeler流程设计页面:
[java] view plain copy
 技术分享技术分享
  1. package controllers;  
  2.   
  3.   
  4. import java.util.HashMap;  
  5. import java.util.Map;  
  6.   
  7.   
  8. import javax.servlet.http.HttpServletRequest;  
  9. import javax.servlet.http.HttpServletResponse;  
  10.   
  11.   
  12. import org.activiti.editor.constants.ModelDataJsonConstants;  
  13. import org.activiti.engine.RepositoryService;  
  14. import org.activiti.engine.repository.Model;  
  15. import org.springframework.beans.factory.annotation.Autowired;  
  16. import org.springframework.stereotype.Controller;  
  17. import org.springframework.web.bind.annotation.RequestMapping;  
  18. import org.springframework.web.bind.annotation.RequestMethod;  
  19. import org.springframework.web.bind.annotation.RequestParam;  
  20. import org.springframework.web.bind.annotation.ResponseBody;  
  21.   
  22.   
  23. import com.fasterxml.jackson.databind.ObjectMapper;  
  24. import com.fasterxml.jackson.databind.node.ObjectNode;  
  25.   
  26.   
  27. @Controller  
  28. public class ActivitiController {  
  29.     @Autowired  
  30.     private RepositoryService repositoryService;  
  31.   
  32.   
  33.     /** 
  34.      * 查询生日列表 
  35.      *  
  36.      * @param req 
  37.      * @return 
  38.      */  
  39.     @RequestMapping(value = "/activiti.do", method = RequestMethod.GET, produces = "application/json;charset=utf-8")  
  40.     @ResponseBody  
  41.     public Object brithdayList(HttpServletRequest req) {  
  42.         Map<String, Object> map = new HashMap<String, Object>();  
  43.         map.put("name""activiti");  
  44.         return map;  
  45.     }  
  46.   
  47.   
  48.     @RequestMapping(value = "/create.do", method = RequestMethod.GET, produces = "application/json;charset=utf-8")  
  49.     public void create(  
  50.             @RequestParam("name") String name,  
  51.             @RequestParam("key") String key,  
  52.             @RequestParam(value = "description", required = false) String description,  
  53.             HttpServletRequest request, HttpServletResponse response) {  
  54.         try {  
  55.             ObjectMapper objectMapper = new ObjectMapper();  
  56.             ObjectNode modelObjectNode = objectMapper.createObjectNode();  
  57.             modelObjectNode.put(ModelDataJsonConstants.MODEL_NAME, name);  
  58.             modelObjectNode.put(ModelDataJsonConstants.MODEL_REVISION, 1);  
  59.             modelObjectNode.put(ModelDataJsonConstants.MODEL_DESCRIPTION,  
  60.                     org.apache.commons.lang3.StringUtils  
  61.                             .defaultString(description));  
  62.             Model newModel = repositoryService.newModel();  
  63.             newModel.setMetaInfo(modelObjectNode.toString());  
  64.             newModel.setName(name);  
  65.             newModel.setKey(key);  
  66.             repositoryService.saveModel(newModel);  
  67.             ObjectNode editorNode = objectMapper.createObjectNode();  
  68.             editorNode.put("id""canvas");  
  69.             editorNode.put("resourceId""canvas");  
  70.             ObjectNode stencilSetNode = objectMapper.createObjectNode();  
  71.             stencilSetNode.put("namespace",  
  72.                     "http://b3mn.org/stencilset/bpmn2.0#");  
  73.             editorNode.put("stencilset", stencilSetNode);  
  74.             repositoryService.addModelEditorSource(newModel.getId(), editorNode  
  75.                     .toString().getBytes("utf-8"));  
  76.             response.sendRedirect(request.getContextPath()  
  77.                     + "/service/editor?id=" + newModel.getId());  
  78.         } catch (Exception e) {  
  79.             e.getStackTrace();  
  80.         }  
  81.     }  
  82. }  


11、改动index.jsp为activiti.html:
[html] view plain copy
 技术分享技术分享
  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <meta charset="utf-8">  
  5. </head>  
  6. <body>  
  7. <h2>Hello World!</h2>  
  8. <a href="./activiti.do">activiti</a>  
  9.   <form action="./create.do" method="get">  
  10.     <input type="text" id="name" name="name"></input>  
  11.     <input type="text" id="key" name="key"></input>  
  12.     <input type="text" id="description" name="description"></input>  
  13.     <input type="submit" value="提交">  
  14.   </form>  
  15. </body>  
  16. </html>  



12、启动项目,使用http://localhost:8080/activitiTest1訪问项目,看到例如以下页面:

技术分享
 
13、点击提交后,跳转到流程图设计界面:

技术分享


參考文档:http://weir2009.iteye.com/blog/2119072

                    http://lpyyn.iteye.com/blog/2149219

                   http://www.mossle.com/docs/activiti/index.html

                   http://blog.csdn.net/lfsf802/article/details/46237003

                   http://www.kafeitu.me/activiti/2013/03/10/integrate-activiti-modeler.html

activiti自己定义流程之Spring整合activiti-modeler实例(一):环境搭建