首页 > 代码库 > jbpm 工作流(二)
jbpm 工作流(二)
1 概述
本文主要介绍如何将JBPM+Struts+Spring+Hibernate整合在一块。并通过一个简单实例来说明。此实例为一个申请审批的简单流程,并将申请人和审批人记录到数据库中,流程为start----申请----审批----end,中间无退回,无分支,只是看一下jbpm的流程设计,本文将jbpm和struts/spring/hibernate结合在一块做为一个完整的项目来叙述。
通过processdefinition.xml的可视图,可了解一下流程图:
2 环境
tomcat6.0+jdk1.5+eclips3.2
jbpm3.1.4
spring2.0
hibernate3
数据库:postgreSql8.1
jbpm提供了在eclipse下的一个可视化插件,通过它我们可以方便的定义工作流,介绍一下eclipse如何添加该插件?
打开jbpm-starters-kit-3.1.4\jbpm-designer\jbpm-gpd-feature目录,我们可以看到下面有一个eclipse文件夹,这里我们通过link的方式为eclipse添加该插件。
在{eclipse_home}/link文件夹下新建jbpm-designer.link文件,用记事本打开该文件加入如下一行:
path=D:\\jbpm-starters-kit-3.1.4\\jbpm-designer\\jbpm-gpd-feature
后面的路径即为该插件的存放路径,根据个人情况修改。注意,这里的分隔符使用双斜杠\\或者用单斜杠/。
3 项目总体结构
src部分
WebRoot部分:
4 要引入的jar包
很多jar包是从jbpm-starters-kit-3.1.4\jbpm-starters-kit-3.1.4\jbpm\build
和jbpm-starters-kit-3.1.4\jbpm-starters-kit-3.1.4\jbpm\lib中得到的
其中spring-modules-jbpm31.jar、postgresql-8.1-405.jdbc2.jar要下载
以下是myeclips需要引入的包
5 web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<context-param>
<param-name>webAppRootKey</param-name>
<param-value>/jbpm/</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<context-param>
<param-name>log4jRefreshInterval</param-name>
<param-value>60000</param-value>
</context-param>
<filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.binghe.pub.SetCharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>GBK</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<init-param>
<param-name>debug</param-name>
<param-value>3</param-value>
</init-param>
<init-param>
<param-name>detail</param-name>
<param-value>3</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>imageServlet</servlet-name>
<servlet-class>com.binghe.pub.ImageServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<servlet-mapping>
<servlet-name>imageServlet</servlet-name>
<url-pattern>/servlet/imageServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/apply.jsp</welcome-file>
</welcome-file-list>
</web-app>
6 applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<bean id="dataSource"class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName">
<value>org.postgresql.Driver</value>
</property>
<property name="url">
<value>jdbc:postgresql://localhost:5432/jbpmtest</value>
</property>
<property name="username">
<value>admin</value>
</property>
<property name="password">
<value>admin</value>
</property>
</bean>
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="mappingLocations">
<value>classpath*:/org/jbpm/**/*.hbm.xml</value>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.PostgreSQLDialect
</prop>
<prop key="jdbc.fetch_size">50</prop>
<prop key="jdbc.batch_size">30</prop>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.use_outer_join">true</prop>
<prop key="hibernate.cglib.use_reflection_optimizer">
true
</prop>
</props>
</property>
</bean>
<!-- Transaction Manager -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory">
<ref local="sessionFactory" />
</property>
</bean>
<!-- Transaction Interceptor -->
<bean id="transactionInterceptor"
class="org.springframework.transaction.interceptor.TransactionInterceptor"
dependency-check="none">
<property name="transactionManager">
<ref bean="transactionManager" />
</property>
<property name="transactionAttributes">
<props>
<prop key="save*">PROPAGATION_REQUIRED</prop>
<prop key="update*">PROPAGATION_REQUIRED</prop>
<prop key="attachDirty*">PROPAGATION_REQUIRED</prop>
<prop key="delete*">PROPAGATION_REQUIRED</prop>
<!-- default, readOnly -->
<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
</props>
</property>
</bean>
<!-- reading jBPM process definitions -->
<bean id="websaleWorkflow1"
class="org.springmodules.workflow.jbpm31.definition.ProcessDefinitionFactoryBean">
<property name="definitionLocation"
value="http://www.mamicode.com/classpath:/com/jpdl/simple/processdefinition.xml" />
</bean>
<!-- jBPM configuration-->
<bean id="jbpmConfiguration"
class="org.springmodules.workflow.jbpm31.LocalJbpmConfigurationFactoryBean">
<property name="sessionFactory" ref="sessionFactory" />
<property name="configuration" value="http://www.mamicode.com/classpath:/jbpm.cfg.xml" />
<property name="processDefinitions">
<list>
<ref local="websaleWorkflow1" />
</list>
</property>
<property name="createSchema" value="http://www.mamicode.com/true" />
</bean>
<!-- jBPM template -->
<bean id="jbpmTemplate"
class="org.springmodules.workflow.jbpm31.JbpmTemplate">
<constructor-arg index="0" ref="jbpmConfiguration" />
<constructor-arg index="1" ref="websaleWorkflow1" />
</bean>
<bean id="JbpmDAO" class="com.binghe.hibernate.dao.JbpmDAO">
<property name="sessionFactory">
<ref bean="sessionFactory" />
</property>
<property name="jbpmConfiguration">
<ref bean="jbpmConfiguration" />
</property>
</bean>
<bean name="/jbpm" class="com.binghe.struts.action.JbpmAction"
singleton="false">
<property name="jbpmDAO">
<ref bean="JbpmDAO" />
</property>
</bean>
</beans>
7 struts-config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources />
<form-beans>
<form-bean name="jbpmForm" type="com.binghe.struts.form.JbpmForm" />
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="error" path="/info/error.jsp" />
</global-forwards>
<action-mappings>
<action
attribute="jbpmForm"
input="/approve.jsp"
name="jbpmForm"
parameter="method"
path="/jbpm"
scope="request"
type="org.springframework.web.struts.DelegatingActionProxy"
validate="false">
<forward name="approve" path="/approve.jsp" />
<forward name="end" path="/end.jsp" />
</action>
</action-mappings>
<message-resources parameter="ApplicationResources" />
<plug-in
className="org.springframework.web.struts.ContextLoaderPlugIn">
<set-property property="contextConfigLocation"
value="http://www.mamicode.com/WEB-INF/classes/applicationContext.xml" />
</plug-in>
</struts-config>
8 processdefinition.xml
<?xml version="1.0" encoding="GBK"?>
<process-definition xmlns="urn:jbpm.org:jpdl-3.1" name="helloworld">
<start-state name="start">
<transition name="to_apply" to="申请"></transition>
</start-state>
<!-- 申请 -->
<task-node name="申请" signal="last-wait" create-tasks="false">
<task name="申请"></task>
<!-- 流程转向 -->
<transition name="to_approve" to="审批"></transition>
</task-node>
<!-- 审批 -->
<task-node name="审批" signal="last-wait" create-tasks="false">
<task name="审批"></task>
<transition name="to_end" to="end1"></transition>
</task-node>
<end-state name="end1"></end-state>
</process-definition>
9 hibernate.cfg.xml
<?xml version=‘1.0‘ encoding=‘utf-8‘?>
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- jdbc connection properties -->
<propertyname="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<propertyname="hibernate.connection.driver_class">org.postgresql.Driver</property>
<propertyname="hibernate.connection.url">jdbc:postgresql://localhost:5432/jbpmtest</property>
<property name="hibernate.connection.username">admin</property>
<property name="hibernate.connection.password">admin</property>
<!--<property name="hibernate.cache.provider_class">org.hibernate.cache.HashtableCacheProvider</property>-->
<!-- ############################################ -->
<!-- # mapping files with external dependencies # -->
<!-- ############################################ -->
<!-- following mapping file has a dependendy on -->
<!-- ‘bsh-{version}.jar‘. -->
<!-- uncomment this if you don‘t have bsh on your -->
<!-- classpath. you won‘t be able to use the -->
<!-- script element in process definition files -->
<mapping resource="org/jbpm/graph/action/Script.hbm.xml"/>
<!-- following mapping files have a dependendy on -->
<!-- ‘jbpm-identity-{version}.jar‘, mapping files -->
<!-- of the pluggable jbpm identity component. -->
<!-- comment out the following 3 lines if you don‘t-->
<!-- want to use the default jBPM identity mgmgt -->
<!-- component -->
<mapping resource="org/jbpm/identity/User.hbm.xml"/>
<mapping resource="org/jbpm/identity/Group.hbm.xml"/>
<mapping resource="org/jbpm/identity/Membership.hbm.xml"/>
<!-- ###################### -->
<!-- # jbpm mapping files # -->
<!-- ###################### -->
<!-- hql queries and type defs -->
<mapping resource="org/jbpm/db/hibernate.queries.hbm.xml" />
<!-- graph.def mapping files -->
<mapping resource="org/jbpm/graph/def/ProcessDefinition.hbm.xml"/>
<mapping resource="org/jbpm/graph/def/Node.hbm.xml"/>
<mapping resource="org/jbpm/graph/def/Transition.hbm.xml"/>
<mapping resource="org/jbpm/graph/def/Event.hbm.xml"/>
<mapping resource="org/jbpm/graph/def/Action.hbm.xml"/>
<mapping resource="org/jbpm/graph/def/SuperState.hbm.xml"/>
<mapping resource="org/jbpm/graph/def/ExceptionHandler.hbm.xml"/>
<mapping resource="org/jbpm/instantiation/Delegation.hbm.xml"/>
<!-- graph.node mapping files -->
<mapping resource="org/jbpm/graph/node/StartState.hbm.xml"/>
<mapping resource="org/jbpm/graph/node/EndState.hbm.xml"/>
<mapping resource="org/jbpm/graph/node/ProcessState.hbm.xml"/>
<mapping resource="org/jbpm/graph/node/Decision.hbm.xml"/>
<mapping resource="org/jbpm/graph/node/Fork.hbm.xml"/>
<mapping resource="org/jbpm/graph/node/Join.hbm.xml"/>
<mapping resource="org/jbpm/graph/node/State.hbm.xml"/>
<mapping resource="org/jbpm/graph/node/TaskNode.hbm.xml"/>
<!-- context.def mapping files -->
<mapping resource="org/jbpm/context/def/ContextDefinition.hbm.xml"/>
<mapping resource="org/jbpm/context/def/VariableAccess.hbm.xml"/>
<!-- taskmgmt.def mapping files -->
<mapping resource="org/jbpm/taskmgmt/def/TaskMgmtDefinition.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/def/Swimlane.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/def/Task.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/def/TaskController.hbm.xml"/>
<!-- module.def mapping files -->
<mapping resource="org/jbpm/module/def/ModuleDefinition.hbm.xml"/>
<!-- bytes mapping files -->
<mapping resource="org/jbpm/bytes/ByteArray.hbm.xml"/>
<!-- file.def mapping files -->
<mapping resource="org/jbpm/file/def/FileDefinition.hbm.xml"/>
<!-- scheduler.def mapping files -->
<mapping resource="org/jbpm/scheduler/def/CreateTimerAction.hbm.xml"/>
<mapping resource="org/jbpm/scheduler/def/CancelTimerAction.hbm.xml"/>
<!-- graph.exe mapping files -->
<mapping resource="org/jbpm/graph/exe/Comment.hbm.xml"/>
<mapping resource="org/jbpm/graph/exe/ProcessInstance.hbm.xml"/>
<mapping resource="org/jbpm/graph/exe/Token.hbm.xml"/>
<mapping resource="org/jbpm/graph/exe/RuntimeAction.hbm.xml"/>
<!-- module.exe mapping files -->
<mapping resource="org/jbpm/module/exe/ModuleInstance.hbm.xml"/>
<!-- context.exe mapping files -->
<mapping resource="org/jbpm/context/exe/ContextInstance.hbm.xml"/>
<mapping resource="org/jbpm/context/exe/TokenVariableMap.hbm.xml"/>
<mapping resource="org/jbpm/context/exe/VariableInstance.hbm.xml"/>
<mappingresource="org/jbpm/context/exe/variableinstance/ByteArrayInstance.hbm.xml"/>
<mappingresource="org/jbpm/context/exe/variableinstance/DateInstance.hbm.xml"/>
<mappingresource="org/jbpm/context/exe/variableinstance/DoubleInstance.hbm.xml"/>
<mappingresource="org/jbpm/context/exe/variableinstance/HibernateLongInstance.hbm.xml"/>
<mappingresource="org/jbpm/context/exe/variableinstance/HibernateStringInstance.hbm.xml"/>
<mappingresource="org/jbpm/context/exe/variableinstance/LongInstance.hbm.xml"/>
<mappingresource="org/jbpm/context/exe/variableinstance/NullInstance.hbm.xml"/>
<mappingresource="org/jbpm/context/exe/variableinstance/StringInstance.hbm.xml"/>
<!-- msg.db mapping files -->
<mapping resource="org/jbpm/msg/Message.hbm.xml"/>
<mapping resource="org/jbpm/msg/db/TextMessage.hbm.xml"/>
<mapping resource="org/jbpm/command/ExecuteActionCommand.hbm.xml"/>
<mapping resource="org/jbpm/command/ExecuteNodeCommand.hbm.xml"/>
<mapping resource="org/jbpm/command/SignalCommand.hbm.xml"/>
<mapping resource="org/jbpm/command/TaskInstanceEndCommand.hbm.xml"/>
<!-- taskmgmt.exe mapping files -->
<mapping resource="org/jbpm/taskmgmt/exe/TaskMgmtInstance.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/exe/TaskInstance.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/exe/PooledActor.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/exe/SwimlaneInstance.hbm.xml"/>
<!-- scheduler.exe mapping files -->
<mapping resource="org/jbpm/scheduler/exe/Timer.hbm.xml"/>
<!-- logging mapping files -->
<mapping resource="org/jbpm/logging/log/ProcessLog.hbm.xml"/>
<mapping resource="org/jbpm/logging/log/MessageLog.hbm.xml"/>
<mapping resource="org/jbpm/logging/log/CompositeLog.hbm.xml"/>
<mapping resource="org/jbpm/graph/log/ActionLog.hbm.xml"/>
<mapping resource="org/jbpm/graph/log/NodeLog.hbm.xml"/>
<mapping resource="org/jbpm/graph/log/ProcessInstanceCreateLog.hbm.xml"/>
<mapping resource="org/jbpm/graph/log/ProcessInstanceEndLog.hbm.xml"/>
<mapping resource="org/jbpm/graph/log/ProcessStateLog.hbm.xml"/>
<mapping resource="org/jbpm/graph/log/SignalLog.hbm.xml"/>
<mapping resource="org/jbpm/graph/log/TokenCreateLog.hbm.xml"/>
<mapping resource="org/jbpm/graph/log/TokenEndLog.hbm.xml"/>
<mapping resource="org/jbpm/graph/log/TransitionLog.hbm.xml"/>
<mapping resource="org/jbpm/context/log/VariableLog.hbm.xml"/>
<mapping resource="org/jbpm/context/log/VariableCreateLog.hbm.xml"/>
<mapping resource="org/jbpm/context/log/VariableDeleteLog.hbm.xml"/>
<mapping resource="org/jbpm/context/log/VariableUpdateLog.hbm.xml"/>
<mappingresource="org/jbpm/context/log/variableinstance/ByteArrayUpdateLog.hbm.xml"/>
<mappingresource="org/jbpm/context/log/variableinstance/DateUpdateLog.hbm.xml"/>
<mappingresource="org/jbpm/context/log/variableinstance/DoubleUpdateLog.hbm.xml"/>
<mappingresource="org/jbpm/context/log/variableinstance/HibernateLongUpdateLog.hbm.xml"/>
<mappingresource="org/jbpm/context/log/variableinstance/HibernateStringUpdateLog.hbm.xml"/>
<mappingresource="org/jbpm/context/log/variableinstance/LongUpdateLog.hbm.xml"/>
<mappingresource="org/jbpm/context/log/variableinstance/StringUpdateLog.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/log/TaskLog.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/log/TaskCreateLog.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/log/TaskAssignLog.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/log/TaskEndLog.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/log/SwimlaneLog.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/log/SwimlaneCreateLog.hbm.xml"/>
<mapping resource="org/jbpm/taskmgmt/log/SwimlaneAssignLog.hbm.xml"/>
</session-factory>
</hibernate-configuration>
10 代码说明
10.1代码包结构
在src/com/目录下面,有包
com.binghe.hibernate.dao 存放dao类,调用jbpm的方法操作数据库
com.binghe.pub 存放公共的信息,如字符集乱码的处理等等,不用的话可以在web.xml中不加载,本文不赘述。
com.binghe.struts.action 存放sturts中的action,与jsp页面交互
com.binghe.struts.form 存放sturts中的form,存放jsp页面中的提交数据
com.binghe.jpdl 存放jbpm的流程文件
Jsp部分:
apply.jsp 申请jsp,输入申请人,web.xml中已将此页面为welcome页面
approve.jsp 审批jsp,输入审批人
end.jsp 结束jsp,显示申请人和审批人
10.2完整代码
10.2.1 JbpmDAO
package com.binghe.hibernate.dao;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jbpm.JbpmConfiguration;
import org.jbpm.JbpmContext;
import org.jbpm.db.GraphSession;
import org.jbpm.graph.def.ProcessDefinition;
import org.jbpm.graph.exe.ProcessInstance;
import org.jbpm.graph.exe.Token;
import org.jbpm.graph.node.TaskNode;
import org.jbpm.taskmgmt.def.Task;
import org.jbpm.taskmgmt.exe.TaskMgmtInstance;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
public class JbpmDAO extends HibernateDaoSupport {
private static final Log log = LogFactory.getLog(JbpmDAO.class);
JbpmConfiguration jbpmConfiguration = null;
protected void initDao() {
// do nothing
}
/**
* start节点
* signal()之后,节点流向start---->申请
*
*/
public Long apply(String applyName) {
JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();
Long id=null;
if(jbpmContext==null){
jbpmContext = jbpmConfiguration.createJbpmContext();
}
try{
GraphSession graphSession = jbpmContext.getGraphSession();
//通过名称查询出所需要的流程,也可以通过id查询
ProcessDefinition processDefinition = graphSession
.findLatestProcessDefinition("helloworld");
//创建一个新流程
ProcessInstance processInstance = newProcessInstance(processDefinition);
id = processInstance.getId();
//请假申请结束,转到下一个流程结点
processInstance.getRootToken().signal();
//创建任务,并指定用户,数据库中记录是那个用户提出的申请
TaskNode taskNode = (TaskNode)processInstance.getRootToken().getNode();
Task task = (Task)taskNode.getTasks().iterator().next();
Token token = processInstance.getRootToken();
TaskMgmtInstance taskInstance =processInstance.getTaskMgmtInstance();
taskInstance.createTaskInstance(task, token).setActorId(applyName);
jbpmContext.save(processInstance);
}
finally{
jbpmContext.close();
}
return id;
}
/**
* 审批节点
*signal()之后,节点流向:审批------>end
*/
public void approve(String approveName,Long id) {
JbpmContext jbpmContext = jbpmConfiguration.getCurrentJbpmContext();
if(jbpmContext==null){
jbpmContext = jbpmConfiguration.createJbpmContext();
}
try{
//通过当前流程实例的id查询出流程
ProcessInstance processInstance =jbpmContext.loadProcessInstance(id);
processInstance.getRootToken().signal();
//创建任务,并指定用户,数据库中记录是那个用户做的审批
TaskNode taskNode = (TaskNode)processInstance.getRootToken().getNode();
Task task = (Task)taskNode.getTasks().iterator().next();
Token token = processInstance.getRootToken();
TaskMgmtInstance taskInstance = processInstance.getTaskMgmtInstance();
taskInstance.createTaskInstance(task, token).setActorId(approveName);
jbpmContext.save(processInstance);
}
finally{
jbpmContext.close();
}
}
public static JbpmDAO getFromApplicationContext(ApplicationContext ctx) {
return (JbpmDAO) ctx.getBean("JbpmDAO");
}
public JbpmConfiguration getJbpmConfiguration() {
return jbpmConfiguration;
}
public void setJbpmConfiguration(JbpmConfiguration jbpmConfiguration) {
this.jbpmConfiguration = jbpmConfiguration;
}
}
10.2.2 JbpmAction
/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package com.binghe.struts.action;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;
import com.binghe.hibernate.dao.JbpmDAO;
import com.binghe.struts.form.JbpmForm;
public class JbpmAction extends DispatchAction {
private JbpmDAO jbpmDAO = null;
/**
* 申请,
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward apply(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
JbpmForm jbpmForm = (JbpmForm) form;
try {
String applyName = jbpmForm.getApplyName();
Long id = this.jbpmDAO.apply(applyName);
jbpmForm.setId(id);
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("approve");
}
/**
* 审批
*
* @param mapping
* @param form
* @param request
* @param response
* @return
*/
public ActionForward approve(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
JbpmForm jbpmForm = (JbpmForm) form;
try {
// 审批人
String approveName =jbpmForm.getApproveName();
Long id = jbpmForm.getId();
this.jbpmDAO.approve(approveName, id);
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("end");
}
public ActionForward end(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
JbpmForm jbpmForm = (JbpmForm) form;
try {
//申请人
jbpmForm.setApplyName(jbpmForm.getApplyName());
// 审批人
jbpmForm.setApproveName(jbpmForm.getApproveName());
} catch (Exception e) {
e.printStackTrace();
}
return mapping.findForward("end");
}
public JbpmDAO getJbpmDAO() {
return jbpmDAO;
}
public void setJbpmDAO(JbpmDAO jbpmDAO) {
this.jbpmDAO = jbpmDAO;
}
}
10.2.3 JbpmForm
package com.binghe.struts.form;
import org.apache.struts.action.ActionForm;
public class JbpmForm extends ActionForm {
private Long id;
private String applyName;
private String approveName;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getApplyName() {
return applyName;
}
public void setApplyName(String applyName) {
this.applyName = applyName;
}
public String getApproveName() {
return approveName;
}
public void setApproveName(String approveName) {
this.approveName = approveName;
}
}
10.2.4 apply.jsp
<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
<title>jbpm</title>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
<html:form action="/jbpm.do?method=apply" method="post" styleId="form1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="50" colspan="1" align="center" bgcolor="#F5F5f5">申请人:</td>
<td height="50" colspan="1" align="center" bgcolor="#F5F5f5">
<html:text property="applyName" name="jbpmForm"/>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="50" colspan="2" align="center" bgcolor="#F5F5f5">
<input type="submit" name="Submit" id="btn" value="http://www.mamicode.com/ 申请 "></td>
</tr>
</table>
</html:form>
</body>
</html:html>
10.2.5 approve.jsp
<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>jbpm</title>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
<html:form action="/jbpm.do?method=approve" method="post" styleId="form1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="50" colspan="1" align="center" bgcolor="#F5F5f5">申请人:</td>
<td height="50" colspan="1" align="center" bgcolor="#F5F5f5">
<html:text property="applyName" name="jbpmForm" readonly="true"/>
</td>
</tr>
<tr>
<td height="50" colspan="1" align="center" bgcolor="#F5F5f5">审批人:</td>
<td height="50" colspan="1" align="center"bgcolor="#F5F5f5">
<html:text property="approveName" name="jbpmForm"/>
</td>
</tr>
<tr>
<td>
<html:hidden property="id" name="jbpmForm"/>
</td>
</tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="50" colspan="2" align="center" bgcolor="#F5F5f5"><input type="submit" name="Submit" id="btn" value="http://www.mamicode.com/ 审批 "></td>
</tr>
</table>
</html:form>
</body>
</html:html>
10.2.6 end.jsp
<%@ page language="java" contentType="text/html; charset=GBK" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<html:html>
<head>
</head>
<body leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" >
<html:form action="/jbpm.do?method=end" method="post" styleId="form1">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="50" colspan="1" align="center" bgcolor="#F5F5f5">申请人:</td>
<td height="50" colspan="1" align="center" bgcolor="#F5F5f5">
<bean:write property="applyName" name="jbpmForm"/>
</td>
</tr>
<tr>
<td height="50" colspan="1" align="center" bgcolor="#F5F5f5">审批人:</td>
<td height="50" colspan="1" align="center"bgcolor="#F5F5f5">
<bean:write property="approveName" name="jbpmForm"/>
</td>
</tr>
</table>
</html:form>
</body>
</html:html>
11 系统操作页面
输入http://localhost:8080/jbpm/
录入:娄伟征,点击【申请】
录入:刘经理,点击【审批】
12 数据库记录
jbpm_node表
jbpm_processdefinition表
jbpm_processinstance表
jbpm_task表
jbpm_taskinstance表
jbpm_token表
jbpm_transition表