jbpm4.4學習筆記(2)

在上一篇文章中我們已經基本上實現了一個請假流程定義的發佈和刪除操作,並且我們已經將jbpm4的流程定義集成到了web服務中。下面我們將要做如下幾件事:

1)在web應用中實現請假流程的發起;

2)針對不同用戶的登錄,我們顯示不同的代辦任務以實現用戶完成自己的task。

(再次聲明:這只是我的個人學習筆記,主要是通過看了family168網--臨遠的視頻教程,感謝原作。)

具體操作如下:

1、web應用程序的整體文件架構


圖5-1 程序的整體文件架構

這裏簡要作如下說明:

1)對於程序的src目錄下的文件主要是請假流程leave.jpdl.xml的流程定義文件和jbpm4的註冊文件;

2)在src目錄下爲了通過編譯我們導入了jbpm4.4的第三方庫文件;

3)在WebContent目錄下的文件主要是web應用中運行時使用的jsp文件,此處我們爲了實現程序視圖和邏輯處理的分離將相應的操作放在特定的jsp文件中實現;

4)在WebContent/WEB-INF/lib目錄下我們導入了jbpm4.4運行時所需要的第三方庫文件;

具體程序的實現細節以及每個jsp文件的作用我們將在後面詳細給出。

2、流程定義的改動

流程定義源文件:leave.jpdl.xml文件的源碼如下

[html] view plaincopy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2.   
  3. <process name="leave" xmlns="http://jbpm.org/4.4/jpdl">  
  4.    <start g="136,49,48,48" name="start1">  
  5.       <transition to="申請"/>  
  6.    </start>  
  7.    <!-- #{owner}的作用是實現變量存儲申請人,從而達到誰申請誰就能獲得代辦任務列表 -->  
  8.    <task assignee="#{owner}" form="request.jsp" g="112,143,92,52" name="申請">  
  9.       <transition to="經理審批"/>  
  10.    </task>  
  11.    <task assignee="manager" form="manager.jsp" g="113,247,92,52" name="經理審批">  
  12.       <transition g="-79,-22" name="批准" to="exclusive1"/>  
  13.       <transition g="47,270;48,169:-45,-22" name="駁回" to="申請"/>  
  14.    </task>  
  15.    <decision expr="#{day > 3 ? '老闆審批' : '結束'}" g="131,354,48,48" name="exclusive1">  
  16.       <transition g="273,375:-69,-22" name="老闆審批" to="老闆審批"/>  
  17.       <transition g="-50,-22" name="結束" to="end1"/>  
  18.    </decision>  
  19.    <task assignee="boss" form="boss.jsp" g="228,401,92,52" name="老闆審批">  
  20.       <transition g="275,473:" to="end1"/>  
  21.    </task>  
  22.    <end g="135,450,48,48" name="end1"/>  
  23. </process>  

這裏注意的是我們將第一個task節點的任務分配給#{owner}變量,從而實現了不同用戶的動態登錄都可以申請請假,而我們的“經理審批”task節點則分配給了manager用戶,"老闆審批"task節點則分配給了boss用戶,這些操作都可以在圖形界面的property中設置。

另外需要說明的是對於任務的完成人我們並不需要顯示的在程序中保存,jbpm會自動保存。

流程定義的圖形界面如下:


圖5-2 流程定義圖形界面

3、index.jsp的設計和實現

index.jsp是整個web應用程序的主界面,其代碼如下:

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.     <!-- 檢測用戶是否已經登錄 -->  
  4.     <%@include file="checkLogin.jsp" %>  
  5. <%@page import="java.util.*,org.jbpm.api.*,org.jbpm.api.task.*"%>  
  6. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  7. <html>  
  8. <head>  
  9. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  10. <title>Index</title>  
  11. </head>  
  12. <body>  
  13.     <%  
  14.         //獲得processEngine流程引擎  
  15.         ProcessEngine processEngine=Configuration.getProcessEngine();  
  16.       
  17.         //獲得RepositoryService對象  
  18.         //三個作用:1、流程發佈定義 2、管理流程定義 3、刪除流程定義  
  19.         RepositoryService repositoryService=processEngine.getRepositoryService();  
  20.         //獲得ExecutionService對象  
  21.         ExecutionService executionService=processEngine.getExecutionService();  
  22.         //獲得TaskService對象  
  23.         TaskService taskService=processEngine.getTaskService();  
  24.           
  25.         //獲得登錄用戶名  
  26.         String username=(String)session.getAttribute("username");  
  27.           
  28.         //獲得觸發連接並處理  
  29.         /*String action=request.getParameter("action");  
  30.         if("deploy".equals(action))  
  31.         {  
  32.             //部署流程定義  
  33.             repositoryService.createDeployment().addResourceFromClasspath("leave.jpdl.xml").deploy();  
  34.         }else if("remove".equals(action))  
  35.         {  
  36.             //刪除已部署的流程定義  
  37.             repositoryService.deleteDeploymentCascade(request.getParameter("id"));  
  38.         }*/  
  39.           
  40.         //獲得流程定義  
  41.         List<ProcessDefinition> list =repositoryService.createProcessDefinitionQuery().list();  
  42.     %>  
  43.     <!-- 部署流程定義對應連接 -->  
  44.     <a href="deploy.jsp">發佈新流程</a> | [username:<%=username%>] | <a href="login.jsp">登錄</a><br>  
  45.     <table border="1" width="100%">  
  46.     <caption>流程定義</caption>  
  47.     <thead>  
  48.         <tr>  
  49.         <td>id</td>  
  50.         <td>name</td>  
  51.         <td>version</td>  
  52.         <td>action</td>  
  53.         </tr>  
  54.     </thead>  
  55.     <%  
  56.         //for循環顯示流程部署  
  57.         for (ProcessDefinition pd: list)  
  58.         {  
  59.     %>  
  60.     <tr>  
  61.     <td><%=pd.getId() %></td>  
  62.     <td> <%=pd.getName() %></td>   
  63.     <td><%=pd.getVersion() %></td>   
  64.     <td><a href="remove.jsp?id=<%=pd.getDeploymentId()%>">remove</a>  
  65.          |   
  66.         <a href="start.jsp?id=<%=pd.getId()%>">start</a></td>  
  67.     </tr>  
  68.     <%   
  69.         }  
  70.     %>  
  71.     </table>  
  72.     <%   
  73.         //for循環顯示流程實例列表  
  74.         List <ProcessInstance> piList = executionService.createProcessInstanceQuery().list();  
  75.     %>  
  76.     <table border="1" width="100%">  
  77.         <caption>流程實例</caption>  
  78.         <thead>  
  79.             <tr>  
  80.                 <td>id</td>  
  81.                 <td>activity</td>  
  82.                 <td>state</td>  
  83.                 <td>detail</td>  
  84.             </tr>  
  85.         </thead>  
  86.         <%  
  87.             for (ProcessInstance pi:piList)  
  88.             {  
  89.         %>  
  90.             <tr>  
  91.                 <td><%=pi.getId() %></td>  
  92.                 <td><%=pi.findActiveActivityNames() %></td>  
  93.                 <td><%=pi.getState() %></td>  
  94.                 <td><a href="view.jsp?id=<%=pi.getId() %>">view</a></td>  
  95.             </tr>  
  96.         <%  
  97.             }  
  98.         %>  
  99.     </table>  
  100.     <%  
  101.         List<Task> taskList = taskService.findPersonalTasks(username);    
  102.     %>  
  103.     <table border="1" width="100%">  
  104.         <caption>代辦任務</caption>  
  105.         <thead>  
  106.             <tr>  
  107.                 <td>id</td>  
  108.                 <td>name</td>  
  109.                 <td> </td>  
  110.             </tr>  
  111.         </thead>  
  112.         <%  
  113.             for (Task task:taskList)  
  114.             {  
  115.         %>  
  116.             <tr>  
  117.                 <td><%=task.getId() %></td>  
  118.                 <td><%=task.getActivityName() %></td>  
  119.                 <!-- 獲取jpdl中所註冊的task業務處理的jsp頁面鏈接並顯示 -->  
  120.                 <td><a href="<%=task.getFormResourceName()%>?id=<%=task.getId() %>">view</a></td>  
  121.             </tr>  
  122.         <%  
  123.             }  
  124.         %>  
  125.     </table>  
  126. </body>  
  127. </html>  

index.jsp主要由5部分構成,分別加以說明:

1)獲得jbpm4流程定義、管理等操作的基本對象;

2)定義連接,調用deploy.jsp實現流程的發佈,定義連接,調用login.sjp實現用戶登錄;

3)通過List<ProcessDefinition> list =repositoryService.createProcessDefinitionQuery().list();獲得當前流程定義並以表格的形式顯示出來;

4)通過List <ProcessInstance> piList = executionService.createProcessInstanceQuery().list();獲得當前已經發起的流程定義並以表格方式顯示出來;

5)通過List<Task> taskList = taskService.findPersonalTasks(username); 獲得當前登錄用戶的待處理任務並顯示,同時顯示在leave.jpdl.xml中註冊的相應任務接點處理所對應的就是jsp頁面鏈接。例如:

對於:“經理審批” 節點

<task assignee="manager" form="manager.jsp" g="113,247,92,52" name="經理審批">

我們通過

<td><a href="<%=task.getFormResourceName()%>?id=<%=task.getId() %>">view</a></td>

來顯示manager.jsp頁面的鏈接。

另注:我認爲jbpm自動保存任務的完成者也是通過此處完成的。

4、相關jsp源碼

login.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     <fieldset>  
  11.     <legend>登錄</legend>  
  12.         <form action="doLogin.jsp" method="post">  
  13.             用戶名:<input type="text" name="username" value=""/><br/>  
  14.             <input type="submit">  
  15.         </form>  
  16.     </fieldset>  
  17. </body>  
  18. </html>  

doLogin.jsp

[html] view plaincopy
  1. <%   
  2. session.setAttribute("username", request.getParameter("username"));  
  3. response.sendRedirect("index.jsp");  
  4. %>  

checkLogin.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10.     <% if (session.getAttribute("username") == null) {  
  11.         response.sendRedirect("login.jsp");  
  12.     }%>  
  13. </body>  
  14. </html>  

注:以上三個jsp頁面主要實現用戶登錄的相應功能。

deploy.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@ page import="java.util.*,org.jbpm.api.*,java.util.zip.*" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11.     <%  
  12.     ProcessEngine processEngine=Configuration.getProcessEngine();  
  13.     RepositoryService repositoryService=processEngine.getRepositoryService();  
  14.     repositoryService.createDeployment().addResourceFromClasspath("leave.jpdl.xml").deploy();  
  15.     //ZipInputStream zis = new ZipInputStream(this.getClass()  
  16.     //      .getResourceAsStream("/leave.zip"));  
  17.     //repositoryService.createDeployment()  
  18.     //      .addResourcesFromZipInputStream(zis).deploy();  
  19.     response.sendRedirect("index.jsp");  
  20.     %>  
  21. </body>  
  22. </html>  

注:此處實現了流程定義的發佈

request.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. <fieldset>  
  11.     <legend>申請</legend>  
  12.     <form action="submit.jsp" method="post">  
  13.       <input type="hidden" name="taskId" value="${param.id}">  
  14.       申請人:<input type="text" name="owner" value="${sessionScope['username']}"/><br/>  
  15.   請假時間:<input type="text" name="day" value=""/><br/>  
  16.     請假原因:<textarea name="reason"></textarea><br/>  
  17.     <input type="submit"/>  
  18.     </form>  
  19. </fieldset>  
  20. </body>  
  21. </html>  

submit.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.     <%@ page import="java.util.*,org.jbpm.api.*" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11.     <%  
  12.     ProcessEngine processEngine = Configuration.getProcessEngine();  
  13.     TaskService taskService = processEngine.getTaskService();  
  14.   
  15.     String taskId = request.getParameter("taskId");  
  16.     String owner = request.getParameter("owner");  
  17.     int day = Integer.parseInt(request.getParameter("day"));  
  18.     String reason = request.getParameter("reason");  
  19.   
  20.     //將用戶請求的時間和原因作爲流程變量傳遞流程  
  21.     Map map = new HashMap();  
  22.     map.put("day", day);  
  23.     map.put("reason", reason);  
  24.     taskService.completeTask(taskId, map);  
  25.     response.sendRedirect("index.jsp");  
  26.     %>  
  27. </body>  
  28. </html>  

注:這兩個jsp頁面實現了普通用戶申請請假的功能。值得說明的是在submit.jsp中的如下兩句代碼:

[html] view plaincopy
  1. Map map = new HashMap();  
  2.     map.put("day", day);  
  3.     map.put("reason", reason);  
  4.     taskService.completeTask(taskId, map);  

將用戶申請的請假天數和請假原因作爲流程變量保存在數據庫中,同時實現了對"申請"節點task任務的完成。

manager.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@page import="org.jbpm.api.*,org.jbpm.api.task.*" %>      
  4. <%  
  5.     ProcessEngine processEngine = Configuration.getProcessEngine();  
  6.     TaskService taskService = processEngine.getTaskService();  
  7.     String taskId = request.getParameter("id");  
  8.     Task task = taskService.getTask(taskId);  
  9. %>      
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  11. <html>  
  12. <head>  
  13. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  14. <title>Insert title here</title>  
  15. </head>  
  16. <body>  
  17.     <fieldset>  
  18.         <legend>經理審批</legend>  
  19.         <form action="submit_manager.jsp">  
  20.             <input type="hidden" name="taskId" value="${param.id}">  
  21.              申請人:<%=taskService.getVariable(taskId, "owner") %><br/>  
  22.              請假時間:<%=taskService.getVariable(taskId, "day") %><br/>  
  23.              請假原因:<%=taskService.getVariable(taskId, "reason") %><br/>  
  24.             <input name="result" type="submit" value="批准"/><input name="result" type="submit" value="駁回"/>  
  25.         </form>  
  26.     </fieldset>  
  27. </body>  
  28. </html>  
submist_manager.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3.     <%@ page import="java.util.*,org.jbpm.api.*" %>  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  5. <html>  
  6. <head>  
  7. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  8. <title>Insert title here</title>  
  9. </head>  
  10. <body>  
  11.     <%  
  12.         ProcessEngine processEngine = Configuration.getProcessEngine();  
  13.         TaskService taskService = processEngine.getTaskService();  
  14.         String taskId = request.getParameter("taskId");  
  15.         String result = request.getParameter("result");  
  16.         result = new String(result.getBytes("ISO-8859-1"), "UTF-8");  
  17.         taskService.completeTask(taskId, result);  
  18.         response.sendRedirect("index.jsp");  
  19.     %>  
  20. </body>  
  21. </html>  

注:這兩個頁面實現了“經理審批”節點的task,此處要說明的對於流程圖中批准和駁回的實現,我們主要是使用taskService.completeTask(tsaskId,result);函數來實現,此處的result並不是流程變量而是我們所要選擇的流程路徑。對於TaskService.completeTask函數我這裏做如下不規範說明:
該函數包含三個參數,一個必備參數:taskId,即流程ID;兩個可選參數:流程變量參數Map類型和後繼路徑選擇參數String類型(應該與流程定義中的路徑transaction名相同)。

boss.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <%@page import="org.jbpm.api.*,org.jbpm.api.task.*" %>      
  4. <%  
  5.     ProcessEngine processEngine = Configuration.getProcessEngine();  
  6.     TaskService taskService = processEngine.getTaskService();  
  7.     String taskId = request.getParameter("id");  
  8.     Task task = taskService.getTask(taskId);  
  9. %>      
  10. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  11. <html>  
  12. <head>  
  13. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  14. <title>Insert title here</title>  
  15. </head>  
  16. <body>  
  17.     <fieldset>  
  18.         <legend>老闆審批</legend>  
  19.         <form action="submit_boss.jsp">  
  20.             <input type="hidden" name="taskId" value="${param.id}">  
  21.              申請人:<%=taskService.getVariable(taskId, "owner") %><br/>  
  22.              請假時間:<%=taskService.getVariable(taskId, "day") %><br/>  
  23.              請假原因:<%=taskService.getVariable(taskId, "reason") %><br/>  
  24.             <input name="result" type="submit" value="提交審批內容"/>  
  25.         </form>  
  26.     </fieldset>  
  27. </body>  
  28. </html>  

submit_boss.jsp

[html] view plaincopy
  1. <%@ page language="java" contentType="text/html; charset=UTF-8"  
  2.     pageEncoding="UTF-8"%>  
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  
  4. <html>  
  5. <head>  
  6. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">  
  7. <title>Insert title here</title>  
  8. </head>  
  9. <body>  
  10. <%@page import="java.util.*,org.jbpm.api.*"%>  
  11. <%  
  12.     ProcessEngine processEngine = Configuration.getProcessEngine();  
  13.     TaskService taskService = processEngine.getTaskService();  
  14.   
  15.     String taskId = request.getParameter("taskId");  
  16.     taskService.completeTask(taskId);  
  17.     response.sendRedirect("index.jsp");  
  18. %>  
  19. </body>  
  20. </html>  

至此爲止請假流程web應用的最基本的功能已經實現。

5、程序運行結果

發佈了5 篇原創文章 · 獲贊 0 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章