1.在eclipse里面创建一个动态的web项目。
2.在lib目录下加入依赖库(由于我们在jsp中需要用到jstl标签,所以这里我们还必须加入jstl的依赖库)
引用
*JBPM4_HOME/jbpm.jar
*JBPM4_HOME/lib/*.jar
*jakarta-taglibs-standard-1.1.2_HOME/lib/jstl.jar
*jakarta-taglibs-standard-1.1.2_HOME/lib/standard.jar
用tomcat6页面会报错,
解决方法:
方法一:换成tomcat5.5 一点问题也没有了
方法二:将juel.jar, juel-engine.jar, juel-impl.jar这三个包复制到tomcat6下lib中,并删除原来的el-api.jar,切记要把WEB-INF\lib中的juel.jar, juel-engine.jar, juel-impl.jar删除。不然还是要冲突。
3.在项目的Java Resources目录下创建一个resources目录,并且把“JBPM4初探---HelloWorld”项目中src目录下的所有配置文件拷到resources目录下。
4.在项目的src目录一个java文件(servlet),用来编写我们发布流程并获取最新发布的流程定义
package com.test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.jbpm.api.Configuration;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessDefinitionQuery;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.RepositoryService;
public class JbpmServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private ProcessEngine processEngine = null;
private RepositoryService repositoryService = null;
/* 初始化ProcessEngine. */
public void init(ServletConfig config) throws ServletException {
processEngine = new Configuration().buildProcessEngine();
repositoryService = processEngine.getRepositoryService();
}
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
process(request, response);
}
protected void process(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String zzg = request.getParameter("zzg");
if (zzg.equals("deploy")) {
deployProcessDefinition(request, response);
} else {
removeProcessDefinitionById(request, response);
}
List list = getLatestProcessDefinition(request, response);
request.setAttribute("process", list);
request.getRequestDispatcher("/index.jsp").forward(request, response);
}
/**
* 发布流程方法
* @param request
* @param response
* @throws ServletException
* @throws IOException
*/
protected void deployProcessDefinition(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String xml = request.getParameter("xml");
repositoryService.createDeployment().addResourceFromString(
"hello.jpdl.xml", xml).deploy();
}
/* 获取所有最新流程. */
protected List<ProcessDefinition> getLatestProcessDefinition(
HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获取全部的流程定义
List<ProcessDefinition> processDefinitions = repositoryService
.createProcessDefinitionQuery().orderAsc(
ProcessDefinitionQuery.PROPERTY_NAME).list();
Map<String, ProcessDefinition> map = new LinkedHashMap<String, ProcessDefinition>();
for (ProcessDefinition pd : processDefinitions) {
String key = pd.getKey();
ProcessDefinition definition = map.get(key);
if ((definition == null)
|| (definition.getVersion() < pd.getVersion())) {
map.put(key, pd);
}
}
return new ArrayList(map.values());
}
/* 删除流程定义. */
protected void removeProcessDefinitionById(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
String id = request.getParameter("id");
//根据流程ID获取流程定义实例
//注意:jbpm4是不允许直接根据流程定义的ID来直接删除流程定义的
ProcessDefinition pd = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(id).uniqueResult();
repositoryService.deleteDeploymentCascade(pd.getDeploymentId());
}
}
}
5.在项目的webapp目录建立一个index.jsp,主要用来发布流程并显示最新发布的流程
<%@page contentType="text/html;charset=UTF-8" %>
<%@page import="org.jbpm.api.*" %>
<%@page import="java.util.*" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<html>
<head>
<title>jbpm学习</title>
</head>
<body>
<form action="jbpm?zzg=deploy" method="post">
<textarea name="xml" cols=50 rows=15></textarea>
<input type="submit" value="发布"/>
<input type="reset" value="取消"/>
</form>
<table width=430 border=1>
<tr>
<td>ID</td>
<td>Key</td>
<td>名称</td>
<td>版本</td>
<td>操作</td>
</tr>
<c:forEach var="process" items="${process}">
<tr>
<td>${process.id}</td>
<td>${process.key}</td>
<td>${process.name}</td>
<td>${process.version}</td>
<td><a href="jbpm?zzg=delete&id=${process.id }">删除</a></td>
</tr>
</c:forEach>
</table>
</body>
</html>
6.修改项目下的web.xml文件
<servlet>
<description />
<servlet-name>JbpmServlet</servlet-name>
<servlet-class>com.test.JbpmServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>JbpmServlet</servlet-name>
<url-pattern>/jbpm</url-pattern>
</servlet-mapping>
7.为项目创建一个服务器,做测试。
在文本框中输入:
<?xml version="1.0" encoding="UTF-8"?>
<process name="hello" xmlns="http://jbpm.org/4.3/jpdl">
<start g="235,38,48,48" name="start1">
<transition name="to state1" to="state1" g="-59,-17"/>
</start>
<state g="214,154,92,52" name="state1">
<transition name="to end1" to="end1" g="-47,-17"/>
</state>
<end g="240,284,48,48" name="end1"/>
</process>
如果没报错,测试成功。