作为一个项目,我们不可能只使用JBPM,肯定需要跟其他框架相结合使用,而SSH2是我们常见的框架,那么SSH2与JBPM也是比较常见的组合了,今天就来介绍下他们是如何协和工作的。
1.版本:
Struts2.3.14+hibernate3.6.8+spring2.5.6+JBPM4.4
下载地址:
Struts2.3.14:http://struts.apache.org/download.cgi#struts2312
Hibernate3.6.8:http://sourceforge.net/projects/hibernate/files/hibernate3/
Spring2.5.6:http://www.springsource.org/download/community
Jbpm4.4:https://sourceforge.net/projects/jbpm/files/jBPM%204/jbpm-4.4/jbpm-4.4.zip/download
2.引入jar包:三个的jar包必须放到lib下面
tips:有些包如mysql-connector-java.jar这些驱动jar包可能需要手动下载或者去jbpm的lib下面找。
3.配置文件
Web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <display-name>ssh2Jbpm</display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置spring配置文件位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:config/applicationContext.xml</param-value> </context-param> <!-- 配置CharacterEncoding,设置字符集 --> <filter> <filter-name>characterEncodingFilter</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>characterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置struts2 --> <filter> <filter-name>struts2</filter-name> <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> <init-param> <param-name>config</param-name> <param-value>struts-default.xml,struts-plugin.xml,/config/struts.xml</param-value> </init-param> </filter> <filter-mapping> <filter-name>struts2</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 配置spring --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> </web-app>
Spring配置文件applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd"> <!-- 启用spring注解支持 --> <context:annotation-config/> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- 读取hibernate配置文件的位置,连接数据源 --> <property name="configLocation" value="classpath:config/hibernate.cfg.xml"></property> <!-- 扫描实体位置,注入实体 --> <property name="packagesToScan"> <list> <value>com.dan.entity</value> </list> </property> </bean> <!-- 第一种方法配置事务 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"/> </bean> <tx:advice id="txadvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="add*" propagation="REQUIRED" rollback-for="Exception"/> <tx:method name="del*" propagation="REQUIRED" no-rollback-for="MyException"/> <tx:method name="update*" propagation="REQUIRED"/> <tx:method name="*" propagation="REQUIRED" read-only="true"/> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="daoMethods" expression="execution(* dao.*.*(..))"/> <aop:advisor advice-ref="txadvice" pointcut-ref="daoMethods"/> </aop:config> <!-- 实体Bean --> <bean id="userDao" class="com.dan.dao.UserDao"> <property name="sessionFactory" ref="sessionFactory" /> </bean> <bean id="userService" class="com.dan.service.UserService"></bean> <!-- spring管理struts2的Action --> <bean id="userAction" class="com.dan.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> <property name="processEngine" ref="processEngine"></property> </bean> </beans>
Hibernate配置文件hibernate.cfg.xml
<?xml version='1.0' encoding='UTF-8'?> <!--表明解析本XML文件的DTD文档位置,DTD是Document Type Definition 的缩写,即文档类型的定义,XML解析器使用DTD文档来检查XML文件的合法性。hibernate.sourceforge.net/hibernate-configuration-3.0dtd可以在Hibernate3.1.3软件包中的src\org\hibernate目录中找到此文件--> <!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd"> <hibernate-configuration> <session-factory> <property name="hibernate.connection.driver_class">com.mysql.jdbc.Driver </property> <property name="hibernate.connection.url">jdbc:mysql://127.0.0.1:3306/jbpm?useUnicode=true&characterEncoding=UTF-8</property> <property name="hibernate.connection.username">root </property> <property name="hibernate.connection.password">root </property> <property name="hibernate.show_sql">true </property> <property name="hibernate.hbm2ddl.auto">update</property> <property name="hibernate.dialect">org.hibernate.dialect.MySQLDialect </property> </session-factory> </hibernate-configuration>
Struts配置文件struts.xml
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd"> <struts> <!-- 将Action的创建交给spring来管理 --> <constant name="struts.objectFactory" value="spring" /> <!-- 更改struts2请求Action的后缀名,默认为action。若想去掉后缀,设为","即可 --> <constant name="struts.action.extension" value=","></constant> <package name="struts" namespace="/" extends="struts-default"> <action name="user" class="userAction"> <result name="showAddUser">/addUser.jsp</result> <result name="success">/success.jsp</result> <result name="error" type="redirect">/error.jsp</result> </action> </package> </struts>
Tips:
在Spring的配置文件中注入Bean时要注意,如果需要用到哪个bean,就需要引用。
4.编写类
这个类的编写大家都很熟悉,这里我就只展现一下我的主要方法,想要运行的可以下载源码。
Dao层:
接口就不展示了,看下添加用户的方法:
@Override public void addUser(User user) { Session session = sessionFactory.openSession(); session.getTransaction().begin(); session.save(user); session.getTransaction().commit(); session.close(); }
Service层:
@Resource(name="userDao") private IUserDao userDao; @Override public void addUser(User user) { userDao.addUser(user); }
Action层:
private IUserService userService;//有个get,set方法 private User user;//有个get,set方法 //显示添加页面 public String showAddUser(){ return "showAddUser"; } //添加用户 public String addUser() throws Exception{ userService.addUser(user); return "success"; }
页面:addUser.jsp
<form action="user!addUser" method="post"> <input type="text" id="name" name="user.name"/> <input type="text" id="age" name="user.age"/> <input type="submit" value="添加"/> </form>
Tips:
使用get,set方法来使用我们spring中引用的对象,如果没有引用的可以使用@Resource来获取.
页面要使用post提交,不然实体不能实例化。
研究过JBPM的人都知道,JBPM在操作流程时,一切都是基于ProcessEngine对象,所以整合的思路很简单,只要我们能让Spring创建出ProcessEngine,整合就算是迈出一大步。
引入jar包:
由于已经有了ssh的jar包基础,这里我们只需要再添加jbpm.jar和mail.jar包即可。
2.添加jbpm.cfg.xml文件
需要将jbpm.cfg.xml改成使用spring管理(即将jbpm.tx.hibernate.cfg.xml改成jbpm.tx.spring.cfg.xml)
<?xml version="1.0" encoding="UTF-8"?> <jbpm-configuration> <import resource="jbpm.default.cfg.xml" /> <import resource="jbpm.businesscalendar.cfg.xml" /> <import resource="jbpm.tx.spring.cfg.xml" /> <import resource="jbpm.jpdl.cfg.xml" /> <import resource="jbpm.bpmn.cfg.xml" /> <import resource="jbpm.identity.cfg.xml" /> </jbpm-configuration>
3.修改applicationContext.xml
在applicationContext.xml中添加如下代码
<!-- 在sessionFactory中添加jbpm的相关映射文件--> <!-- 配置sessionFactory --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean"> <!-- 读取hibernate配置文件的位置,连接数据源 --> <property name="configLocation" value="classpath:config/hibernate.cfg.xml"></property> <!-- 扫描实体位置,注入实体 --> <property name="packagesToScan"> <list> <value>com.dan.entity</value> </list> </property> <!-- 读取jbpm相关映射文件 --> <property name = "mappingLocations" > <list > <value > classpath:jbpm.execution.hbm.xml </value > <value > classpath:jbpm.history.hbm.xml </value > <value > classpath:jbpm.identity.hbm.xml </value > <value > classpath:jbpm.repository.hbm.xml </value > <value > classpath:jbpm.task.hbm.xml </value > </list > </property > </bean> <!-- 集成jbpm --> <bean id="springHelper" class="org.jbpm.pvm.internal.processengine.SpringHelper"> <property name="jbpmCfg" value="classpath:config.jbpm.cfg.xml" /> </bean> <!-- 通过指定factory的bean,然后调用其方法进行对象的创建 --> <bean id="processEngine" factory-bean="springHelper" factory-method="createProcessEngine"/> <!--并在Action中引用processEngine--> <!-- spring管理struts2的Action --> <bean id="userAction" class="com.dan.action.UserAction" scope="prototype"> <property name="userService" ref="userService"></property> <property name="processEngine" ref="processEngine"></property> </bean>
4.在Action中添加方法进行测试
private ProcessEngine processEngine;//有get,set方法 //测试JBPM public String testJbpm() throws Exception{ System.out.println("整合成功"); System.out.println("processEngine =========== " + processEngine); return "success"; }
下载地址:http://download.csdn.net/detail/zhaodandan19910306/5366477
以上只是对于SSH2与JBPM的环境的简单搭建,具体的开发,比如将jdpl图形化设计展示到我们的页面上然后让用户设定流程等等还需要我们继续深入的研究。