前言
在使用了官方提供的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作為持久層
內建需要做的事
- 将activiti-app中的jpa版本更新到2.x以上,因為jpa1.x中的
方法在2.x版本中被移除了,需要将所有jpa在2.x被移除的方法都修改過來(比如findOne
),強行依賴上去會出現findOne
NoSuchMethodException
- 修改activiti6設計器中對登入權限這部分限制代碼
- 漢化實際上是修改
檔案的内容,将檔案中對應stencilset_bpmn.json
和title
翻譯過來,這個檔案不可直接用5.22的設計器替代,因為新增了很多新特性,會導緻顯示不正常description
內建步驟
-
更新JPA
首先将activiti6的源碼從gitlab上面拉到本地,進入modules/activiti-ui
編輯【activiti6】設計器的前後端內建與漢化
的pom檔案,指定jpa的版本:activiti-ui-root
<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
方法被移除了,需要一個一個的修改過來,比如:
修改完畢後将
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
下:
全局删除angularJS中的權限處理代碼,比如:
移除的字元串為:
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);
}
-
漢化處理
直接将stencilset_bpmn.json添加到resource目錄即可
完整項目位址已上傳到github:https://github.com/Siwash/activiti6-start