JBPM学习(二)--测试模块代码

此篇接上篇 JBPM学习(一)--与SSH整合

http://blog.csdn.net/lushuaiyin/article/details/8599253


SSH与JBPM整合好以后我们必须写测试代码才能确认到底整合成功与否。

在jbpm-4.4\examples\src\org\jbpm\examples路径下我们可以找到官方自带的Demo。

在不知JBPM如何使用前,最好看看Demo的代码。

我挑了这个目录下java这个文件夹里面的代码。把它拷贝到Eclipse中看看。

process.jpdl.xml是流程定义文件,这个打开后能看到图形,也可以看源码。

JBPM学习(二)--测试模块代码_第1张图片

3个bean和一个测试类。我们要把这些代码整合到SSH中去,就把每个方法在Action重新定义一遍就可以了。

搞定之后,我们再照着自己再定义一个流程。

在Eclipse中新建一个jBPM 4 Process Definition文件。

然后就可以用Eclipse的插件来画流程图了。

JBPM学习(二)--测试模块代码_第2张图片


WorkFlowTestAction

package org.jbpm.action;

import java.util.List;

import org.base.MyBaseAction;
import org.jbpm.api.ExecutionService;
import org.jbpm.api.ProcessDefinition;
import org.jbpm.api.ProcessEngine;
import org.jbpm.api.ProcessInstance;
import org.jbpm.api.RepositoryService;
import org.jbpm.api.TaskService;
import org.jbpm.api.task.Task;

public class WorkFlowTestAction  extends MyBaseAction {
	private static final long serialVersionUID = 1L;
	
	public String execute() throws Exception{
		
		return SUCCESS;
	}
	
	//发布流程
	public String setUp() throws Exception {
		RepositoryService repositoryService=getProcessEngine().getRepositoryService();
	    
		String pro="1";
		if(this.getHttpServletRequest().getParameter("pro")!=null){
			pro=(String)this.getHttpServletRequest().getParameter("pro");
			if(!pro.trim().equals("")){
				if(pro.equals("2")){
					
			    	deploymentId = repositoryService.createDeployment()
			        .addResourceFromClasspath("org/jbpm/config/process.jpdl.xml")//流程定义
			        .addResourceFromClasspath("org/jbpm/config/process.png")//流程定义图,这个可选
			        .deploy();
			    	System.out.println("发布流程2,ID:"+deploymentId);
				}else{
					
					deploymentId = repositoryService.createDeployment()
			        .addResourceFromClasspath("org/jbpm/config/myprocess.jpdl.xml")//流程定义
			        .addResourceFromClasspath("org/jbpm/config/myprocess.png")//流程定义图
			        .deploy();
					System.out.println("发布流程1,ID:"+deploymentId);
				} 
				
			}
		}
	    
	    return NONE;
	}
	
	//查询流程
	public String showAll() throws Exception {
		RepositoryService repositoryService=getProcessEngine().getRepositoryService();
		
		String proname="";
		if(this.getHttpServletRequest().getParameter("proname")!=null){
			proname=(String)this.getHttpServletRequest().getParameter("proname");
			if(proname.trim().equals("")){
				System.out.println("proname参数为空,查询所有流程");
				List<ProcessDefinition> processDefinitions = repositoryService.createProcessDefinitionQuery().list();
			    if(processDefinitions!=null){
			    	this.setValueToRequest("processDefinitions", processDefinitions);
			    }
			}else{
				System.out.println("proname参数"+proname);
				List<ProcessDefinition> processDefinitions = repositoryService
				.createProcessDefinitionQuery().processDefinitionName(proname).list();
				
			    if(processDefinitions!=null){
			    	this.setValueToRequest("processDefinitions", processDefinitions);
			    }
			}
			
		}
		
	    return SUCCESS;
	}
	
	//删除流程
	public String tearDown() throws Exception {
		RepositoryService repositoryService=getProcessEngine().getRepositoryService();
		String delId="";
		if(this.getHttpServletRequest().getParameter("delId")!=null){
			delId=(String)this.getHttpServletRequest().getParameter("delId");
			
			if(!delId.trim().equals("")){
				repositoryService.deleteDeploymentCascade(delId);
			    System.out.println("删除流程,ID:"+delId);
			}else{
				System.out.println("流程,ID为空,删除失败");
			}
		}else{
			System.out.println("流程,ID为空,删除失败");
		}
		
	    
	    return NONE;
	}
	
	
	
	public ProcessEngine processEngine;

	public ProcessEngine getProcessEngine() {
		return processEngine;
	}

	public void setProcessEngine(ProcessEngine processEngine) {
		this.processEngine = processEngine;
	}
	
	public String deploymentId;

	public String getDeploymentId() {
		return deploymentId;
	}

	public void setDeploymentId(String deploymentId) {
		this.deploymentId = deploymentId;
	}
}

struts_jbpm_test.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
	<package name="jbpmtest" extends="base-struts-default" namespace="/jbpmtest">
		<action name="setUp" class="org.jbpm.action.WorkFlowTestAction" method="setUp">
		</action>
		<action name="tearDown" class="org.jbpm.action.WorkFlowTestAction" method="tearDown">
		</action>
		<action name="showAll" class="org.jbpm.action.WorkFlowTestAction" method="showAll">
		   <result name="success">/jsp/jbpmtest/listProcess.jsp</result>
		</action>
	</package>
</struts>

listProcess.jsp

<%@ page contentType="text/html; charset=UTF-8"%>
<%@page import="java.util.*"%>
<%@page import="org.jbpm.api.ProcessDefinition"%>
<% 
String path = request.getContextPath();
%>
<% 
List list=null;
if(request.getAttribute("processDefinitions")!=null){
	list=(List)request.getAttribute("processDefinitions");
}
%>
<html>
<head>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
	<title></title>
	<script src="<%=path%>/script/jquery-1.7.1.min.js" type="text/javascript"></script>	
</head>
<body style="overflow: scroll; overflow: auto;">

<input type="hidden" name="path" id="path" value='<%=path%>' ></input>

      <table width="500px"  border="1" align="center">
        <tr>
			 <td align="center">流程名字:<input type="text"  id="proname" value='' ></input></td>
		</tr>
		<tr>
			 <td align="center"><h1><a href="javascript:void(0)" onclick="query()">查询</a></h1></td>
		</tr>
		
		<tr>
			 <td align="center"><input type="button" onclick="setUpProcess('1')"  value='发布流程1--myprocess' ></input></td>
		</tr>
		<tr>
			 <td align="center"><input type="button" onclick="setUpProcess('2')"  value='发布流程2--java' ></input></td>
		</tr>
		
	</table>
	
	 <table width="500px"  border="1" align="center">
        <tr>
			 <th align="center">流程id</th><th align="center">流程名字</th><th align="center">操作</th>
		</tr>
		<% 
		if(list!=null){
			for(int i=0;i<list.size();i++){
				ProcessDefinition process=(ProcessDefinition)list.get(i);
				if(process!=null){
					String proid="";
					String proname="";
					if(process.getDeploymentId()!=null){
						proid=process.getDeploymentId();
					}
					if(process.getName()!=null){
						proname=process.getName();
					}
		%>			
				<tr>
					 <td><%=proid%></td><td><%=proname%></td><td><input type="button" onclick="delprocess('<%=proid%>')"  value='删除' ></input></td>
				</tr>
		<%	
				}
			}
		}
		%>
		
	</table>
</body>
</html> 
<script type="text/javascript">	

function query(){
	var proname=document.getElementById("proname").value;
	var urlpath="<%=path%>/jbpmtest/showAll.action?proname="+proname+"&randomStr="+Math.random();
	window.location.href=urlpath;
	
}


function delprocess(id){
	var urlpath="<%=path%>"+"/jbpmtest/tearDown.action";
	$.ajax({
		   type:"POST",
		   url:urlpath,
		   data: "delId="+id+"&randomStr="+Math.random(),
		   success: function(msg){
			   alert("成功删除!");
		     window.location.reload();
		   }
		});
}


function setUpProcess(pro){
	
	var urlpath="<%=path%>"+"/jbpmtest/setUp.action";
	$.ajax({
		   type:"POST",
		   url:urlpath,
		   data: "pro="+pro+"&randomStr="+Math.random(),
		   success: function(msg){
			   alert("成功发布!");
		     window.location.reload();
		   }
		});
}
</script>

如下是整合后的package结构图:

JBPM学习(二)--测试模块代码_第3张图片


在启动服务器时没有报错,但是在访问jsp页面时,却报错了:

严重: Servlet.service() for servlet jsp threw exception
java.lang.LinkageError: loader constraint violation: when resolving interface method


"javax.servlet.jsp.JspApplicationContext.getExpressionFactory()Ljavax/el/ExpressionFactory;" the
class loader (instance of


org/apache/jasper/servlet/JasperLoader) of the current class, org/apache/jsp/index_jsp, and the
class loader (instance of


org/apache/catalina/loader/StandardClassLoader) for resolved class,
javax/servlet/jsp/JspApplicationContext, have different


Class objects for the type javax/el/ExpressionFactory used in the signature
at org.apache.jsp.index_jsp._jspInit(index_jsp.java:24)
at org.apache.jasper.runtime.HttpJspBase.init(HttpJspBase.java:52)


从网上找到了原因:

原因:

jbpm4 在tomcat6 下面ssh2 这个错误!
原因是项目中WEB-INF\lib中的三个jar包(juel.jar, juel-engine.jar, juel-impl.jar)和tomcat6下lib中jar
包(el-api.jar)冲突

解决方法:


方法一:换成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删除。不然还是要冲突。

------------------------------------------------------------------------------------------------------------------------------------------------------------

继续。。。。

访问http://localhost:8080/frame/jbpmtest/showAll.action

分别请求action中的查询,发布,删除方法都正常。

好,到现在算是整合成功了。

看看效果吧:

JBPM学习(二)--测试模块代码_第4张图片




你可能感兴趣的:(jbpm)