手把手搭建SSH+Activiti(附加源码)
Posted LuckyZhouStar
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了手把手搭建SSH+Activiti(附加源码)相关的知识,希望对你有一定的参考价值。
1.前言
本篇博客,来为大家分享一下SSH+Activiti框架的搭建。其实整个过程并不是很复杂,就是引用一些jar包和配置一些属性文件而已。没有什么神奇的东西,都是给封装好的,我们只不过来个整合就可以了。整体的目录结构如下所示
2.环境准备
1)JDK1.6或者更高版本
2)数据库为mysql
3)Activiti为5.1.3版本的jar包
4)开发环境工具为Eclipse,需要安装流程设计器插件
有网的情况下,只需要搜索插件地址,然后全部安装即可,如下图所示
没网络的情况下,直接下载插件activiti—eclipse_plugin.zip,把压缩包中的内容放入elicpse根目录的dropins文件夹下即可
3.jar包准备
Struts2相关jar包
基本jar包(11个)
从struts-2.3.7-all.zip资源包中获取apps目录下对应jar包apps\\struts2-blank\\WEB-INF\\lib\\*.jar
相关jar包(2+1个)
struts2整合Spring(需要使用)
struts2-spring-plugin-2.3.7.jar
struts2整合Ajax(通常需要使用)
struts2-json-plugin-2.3.7.jar
struts2使用注解开发(根据需求开启)
struts2-convention-plugin-2.3.7.jar
Spring3相关jar包
核心jar包(4个)
spring-beans-3.2.0.RELEASE.jarspring-context-3.2.0.RELEASE.jarspring-core-3.2.0.RELEASE.jarspring-expression-3.2.0.RELEASE.jar
日志包(2个)
com.springsource.org.apache.commons.logging-1.1.1.jarcom.springsource.org.apache.log4j-1.2.15.jar
AOP包(4个)
spring-aop-3.2.0.RELEASE.jarspring-aspects-3.2.0.RELEASE.jarcom.springsource.org.aopalliance-1.0.0.jarcom.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
JDBC包(2个)
spring-jdbc-3.2.0.RELEASE.jarspring-tx-3.2.0.RELEASE.jar
事务管理(1个)
整合ORM框架(1个)
spring-orm-3.2.0.RELEASE.jar
WEB集成(1个)
spring-web-3.2.0.RELEASE.jar
整合JUnit(1个)
spring-test-3.2.0.RELEASE.jar
Hibernate相关jar包
核心jar包(1个)
hibernate3.jar
必须的jar包(6个)
lib\\required\\目录下的所有包
•antlr-2.7.6.jar•commons-collections-3.1.jar•dom4j-1.6.1.jar•javassist-3.12.0.GA.jar•jta-1.1.jar•slf4j-api-1.6.1.jar
jpa的jar包(1个)
•hibernate-jpa-2.0-api-1.0.1.Final.jar
lslf4j整合log4j的jar包(1个) (log4j在spring中已导入)
•slf4j-log4j12-1.7.2.jar
lc3p0连接池jar包(1个)
•c3p0-0.9.1.jar
数据库驱动jar包(N个)(按需求导入)
Activiti相关jar包
1.activiti-5.13\\libs下所有包
2.activiti-5.13\\wars\\activiti-rest\\WEB-INF\\lib下mybatis-3.2.2
3.activiti-5.13\\wars\\activiti-rest\\WEB-INF\\lib下joda-time-2.1.jar
4.配置文件配置
Struts2相关配置
1.web.xml加入Struts2核心过滤器
<span style="font-family:Comic Sans MS;font-size:18px;"><!-- 配置spring启动的监听器,用来初始化Spring中的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 配置struts2启动的过滤器 -->
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping></span>
2.Struts.xml配置
在此配置中需要配置控制器以及拦截器的相关信息
<span style="font-family:Comic Sans MS;font-size:18px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd">
<struts>
<!-- 配置模式为开发模式,自动加载struts.xml和显示错误提示信息 -->
<constant name="struts.devMode" value="true" />
<!-- 设置页面主题为简单主题,去掉struts2开发提供的样式 -->
<constant name="struts.ui.theme" value="simple" />
<package name="default" namespace="/" extends="struts-default">
<!-- 拦截器配置 -->
<interceptors>
<!-- 定义了一个用于拦截器登录的拦截器 -->
<interceptor name="loginInterceptor" class="cn.itcast.ssh.utils.LoginInteceptor">
</interceptor>
<!-- 定义一个拦截器栈 -->
<interceptor-stack name="systemStack">
<interceptor-ref name="defaultStack" />
<interceptor-ref name="loginInterceptor" />
</interceptor-stack>
</interceptors>
<!-- 定义系统默认拦截器 全局 -->
<default-interceptor-ref name="systemStack" />
<!-- 全局结果视图 -->
<global-results>
<result name="login" type="redirect">
login.jsp
</result>
</global-results>
<action name="loginAction_*" class="loginAction" method="1">
<result name="success">WEB-INF/views/main.jsp</result>
<result name="top">WEB-INF/views/top.jsp</result>
<result name="left">WEB-INF/views/left.jsp</result>
<result name="welcome">WEB-INF/views/welcome.jsp</result>
</action>
<action name="leaveBillAction_*" class="leaveBillAction" method="1">
<result name="home">WEB-INF/views/leaveBill/list.jsp</result>
<result name="input">WEB-INF/views/leaveBill/input.jsp</result>
<result name="save" type="redirectAction">
<param name="actionName">leaveBillAction_home.action</param>
</result>
</action>
<action name="workflowAction_*" class="workflowAction" method="1">
<result name="list" type="redirectAction">
<param name="actionName">workflowAction_deployHome.action</param>
</result>
<result name="deployHome">WEB-INF/views/workflow/workflow.jsp</result>
<result name="task">WEB-INF/views/workflow/task.jsp</result>
<result name="image">WEB-INF/views/workflow/image.jsp</result>
<result name="listTask" type="redirectAction">
<param name="actionName">workflowAction_listTask.action</param>
</result>
<result name="viewTaskForm" type="redirectAction">
<param name="actionName">$#formKey</param>
</result>
<result name="taskForm">WEB-INF/views/workflow/taskForm.jsp</result>
<result name="viewHisComment">WEB-INF/views/workflow/taskFormHis.jsp</result>
</action>
</package>
</struts>
</span>
log4j.properties配置
用来配置控制台打印的一些warn、error等类型信息,也可以输出到外部文件中。
<span style="font-family:Comic Sans MS;font-size:18px;">log4j.rootLogger=WARN, CA
# ConsoleAppender
log4j.appender.CA=org.apache.log4j.ConsoleAppender
log4j.appender.CA.layout=org.apache.log4j.PatternLayout
log4j.appender.CA.layout.ConversionPattern= %dhh:mm:ss,SSS [%t] %-5p %c %x - %m%n
</span>
db.properties中放置的是连接数据库的一些信息
<span style="font-family:Comic Sans MS;font-size:18px;">jdbc.url=jdbc:mysql://192.168.24.82:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&failOverReadOnly=false
jdbc.driverClassName=com.mysql.jdbc.Driver
jdbc.username=root
jdbc.password=123456
</span>
activiti-context.xml中放置的是流程实例对象相关信息
<span style="font-family:Comic Sans MS;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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-3.0.xsd">
<!-- spring负责创建流程引擎的配置文件 -->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<!-- 数据源 -->
<property name="dataSource" ref="dataSource" />
<!-- 配置事务管理器,统一事务 -->
<property name="transactionManager" ref="transManager" />
<!-- 设置建表策略,如果没有表,自动创建表 -->
<property name="databaseSchemaUpdate" value="true" />
</bean>
<!-- 创建流程引擎对象 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean>
<!--
相当于下面的代码
RepositoryServicie repositoryService = processEngine.getRepositoryService();
RuntimeServicie repositoryService = processEngine.getRuntimeServicie();
TaskServicie taskServicie = processEngine.getTaskServicie();
HistoryServicie historyServicie = processEngine.getHistoryServicie();
-->
<!-- 由流程引擎对象,提供的方法,创建项目中使用的Activiti工作流的Service -->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService" />
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService" />
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="formService" factory-bean="processEngine" factory-method="getFormService" />
</beans>
</span>
而Hibernate与Spring的整合,以及Activiti与Spring的整合,都放置在applicationContext.xml
<span style="font-family:Comic Sans MS;font-size:18px;"><?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> <!-- 配置数据源 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="url" value="$jdbc.url" /> <property name="driverClassName" value="$jdbc.driverClassName" /> <property name="username" value="$jdbc.username" /> <property name="password" value="$jdbc.password" /> </bean> <!-- 配置外部数据库连接信息--> <context:property-placeholder location="classpath:db.properties"/> <!-- 创建SessionFactory,这是spring整合hibernate的核心 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <!-- 1.配置datasource --> <property name="dataSource" ref="dataSource"></property> <!-- 2.配置Hibernate属性 --> <property name="hibernateProperties"> <value> hibernate.hbm2ddl.auto=update hibernate.show_sql=true hibernate.dialect=org.hibernate.dialect.MySQL5Dialect </value> </property> <!-- 3.配置映射文件 --> <property name="mappingLocations"> <list> <value>classpath:cn/itcast/ssh/domain/*.hbm.xml</value> </list> </property> </bean> <!-- 配置事务 --> <!-- 1.配置事务管理器 --> <bean id="transManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 2.配置事务通知 --> <tx:advice id="txAdvice" transaction-manager="transManager"> <tx:attributes> <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> <tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> <tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> <tx:method name="*" read-only="true" /> </tx:attributes> </tx:advice> <!-- 3.配置切面 --> <aop:config> <aop:pointcut expression="execution(* cn.itcast.ssh.service..*.*(..))" id="aopPointcut"/> <aop:advisor advice-ref="txAdvice" pointcut-ref="aopPointcut" /> </aop:config> <!-- 配置基础的Dao,在其他的DAO中只需要继承即可 --> <bean id="baseDao" abstract="true"> <property name="sessionFactory" ref="sessionFactory"></property> </bean> <!-- 导入相关配置 --> <import resource="classpath:applicationContext-dao.xml"/> <import resource="classpath:applicationContext-service.xml"/> <import resource="classpath:applicationContext-action.xml"/> <import resource="classpath:activiti-context.xml"/> </beans> </span>
其余的applicationContext-*.xml中的配置文件配置的就是相关的依赖注入的信息
5.Dao层
而在Dao层,引入了HibernateDaoSupport模板,具体如下
<span style="font-family:Comic Sans MS;font-size:18px;">import java.util.List; import org.springframework.orm.hibernate3.support.HibernateDaoSupport; import cn.itcast.ssh.dao.IEmployeeDao; import cn.itcast.ssh.domain.Employee; public class EmployeeDaoImpl extends HibernateDaoSupport implements IEmployeeDao /**使用用户名称查询用户的集合*/ @Override public List<Employee> findEmployeeByName(String name) List<Employee> list = this.getHibernateTemplate().find("from Employee where name=?",new Object[]name); return list; </span>
其实可以再向上抽象一层,主要是看自己如何分层了。本篇就是为了展示SSH框架,顾没有划分那么细致。
6.Service层
相关的业务逻辑,需要在Service层中,而Activiti中的Dao数据引擎对象,也在Service中进行注入
<span style="font-family:Comic Sans MS;font-size:18px;">public class LeaveBillServiceImpl implements ILeaveBillService
private ILeaveBillDao leaveBillDao;
public void setLeaveBillDao(ILeaveBillDao leaveBillDao)
this.leaveBillDao = leaveBillDao;
/**查询所有的请假单列表*/
@Override
public List<LeaveBill> findLeaveBillList()
List<LeaveBill> list = leaveBillDao.findLeaveBillList();
return list;
/**保存/更新请假单*/
@Override
public void saveLeaveBill(LeaveBill leaveBill)
//获取请假申请单的ID,判断执行的是新增的操作还是修改的操作
Long id = leaveBill.getId();
//执行新增
if(id==null)
//获取页面的表单值,从Session中获取当前用户的对象,建立多对一的关联关系,直接执行保存
Employee employee = SessionContext.get();
//组织PO对象
//leaveBill.setState(0);//设置状态,0表示初始状态
leaveBill.setUser(employee);//建立多对一的关联关系
leaveBillDao.save(leaveBill);
//执行编辑
else
leaveBillDao.update(leaveBill);
/**使用主键ID,查询请假申请单的对象*/
@Override
public LeaveBill findLeaveBillByID(Long id)
LeaveBill bill = leaveBillDao.findLeaveBillByID(id);
return bill;
/**使用ID,删除请假单*/
@Override
public void deleteLeaveBillByID(Long id)
leaveBillDao.deleteLeaveBillByID(id);
</span>
7.Controller层
Controller是Struts2中的Action,在此继承了ActionSupport和实现了ModelDriven接口
<span style="font-family:Comic Sans MS;font-size:18px;">@SuppressWarnings("serial") public class LeaveBillAction extends ActionSupport implements ModelDriven<LeaveBill> private LeaveBill leaveBill = new LeaveBill(); @Override public LeaveBill getModel() return leaveBill; private ILeaveBillService leaveBillService; public void setLeaveBillService(ILeaveBillService leaveBillService) this.leaveBillService = leaveBillService; /** * 请假管理首页显示 * @return */ public String home() //1:查询请假单申请表,返回List<LeaveBill> List<LeaveBill> leaveList = leaveBillService.findLeaveBillList(); ValueContext.putValueContext("leaveList", leaveList); return "home"; /** * 添加请假申请 * @return */ public String input() //获取请假ID Long id = leaveBill.getId(); //跳转到编辑页面 if(id!=null) //1:获取请假单ID,使用请假单ID作为条件,查询请假单LeaveBill对象 LeaveBill bill = leaveBillService.findLeaveBillByID(id); //2:将LeaveBill对象放置到栈顶 ValueContext.putValueStack(bill); return "input"; /** * 保存/更新,请假申请 * * */ public String save() //执行保存 leaveBillService.saveLeaveBill(leaveBill); return "save"; /** * 删除,请假申请 * * */ public String delete() //获取请假申请单ID Long id = leaveBill.getId(); leaveBillService.deleteLeaveBillByID(id); return "save"; </span>
8.小结
以上就是整个SSH+Activiti的框架搭建,其实很简单,就是做一些配置而已。
源码下载:SSH+Activiti
以上是关于手把手搭建SSH+Activiti(附加源码)的主要内容,如果未能解决你的问题,请参考以下文章
springboot整合activiti,activiti在线编辑器,敏捷快速带源码
Activiti基础02:手把手带你来创建一个Activiti工作流