接上例:http://blog.csdn.net/kunshan_shenbin/archive/2008/10/23/3128756.aspx
上例中采取了在spring配置文件中定义workflow的caller的做法非常的不灵活。
考虑到这个原因,决定新建一个WebBasicWorkflow类。
代码如下:
WebWorkflow.java
- package tw.idv.idealist.base;
- import com.opensymphony.workflow.Workflow;
- public interface WebWorkflow extends Workflow {
- public abstract void setCaller(String caller);
- }
WebBasicWorkflow.java
- package tw.idv.idealist.base;
- import com.opensymphony.workflow.AbstractWorkflow;
- import com.opensymphony.workflow.basic.BasicWorkflowContext;
- public class WebBasicWorkflow extends AbstractWorkflow implements WebWorkflow {
- public WebBasicWorkflow(){
- }
- public WebBasicWorkflow(String caller){
- super.context = new BasicWorkflowContext(caller);
- }
- public void setCaller(String caller) {
- /*
- if(caller == null)
- throw new IllegalArgumentException("Argument caller is Null!");
- if(super.context == null)
- super.context = new BasicWorkflowContext(caller);
- else
- throw new IllegalArgumentException((new StringBuilder()).append("the workflow ").append(toString()).append(" has already init the Context.").toString());
- */
- super.context = new BasicWorkflowContext(caller);
- }
- }
修改如下几个文件:
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:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
- http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">
- <bean id="propertyConfigurer"
- class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <property name="location">
- <value>/WEB-INF/jdbc.properties</value>
- </property>
- </bean>
- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
- <property name="driverClassName">
- <value>${jdbc.driverClassName}</value>
- </property>
- <property name="url">
- <value>${jdbc.url}</value>
- </property>
- <property name="username">
- <value>${jdbc.username}</value>
- </property>
- <property name="password">
- <value>${jdbc.password}</value>
- </property>
- <property name="maxWait">
- <value>${jdbc.maxWait}</value>
- </property>
- <property name="removeAbandoned">
- <value>${jdbc.removeAbandoned}</value>
- </property>
- <property name="removeAbandonedTimeout">
- <value>${jdbc.removeAbandonedTimeout}</value>
- </property>
- <property name="logAbandoned">
- <value>${jdbc.logAbandoned}</value>
- </property>
- </bean>
- <bean id="sessionFactory"
- class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <property name="dataSource">
- <ref bean="dataSource" />
- </property>
- <property name="mappingResources">
- <list>
- <value>com/opensymphony/workflow/spi/hibernate/HibernateCurrentStep.hbm.xml</value>
- <value>com/opensymphony/workflow/spi/hibernate/HibernateHistoryStep.hbm.xml</value>
- <value>com/opensymphony/workflow/spi/hibernate/HibernateWorkflowEntry.hbm.xml</value>
- </list>
- </property>
- <property name="hibernateProperties">
- <props>
- <prop key="hibernate.dialect">
- ${hibernate.dialect}
- </prop>
- <prop key="hibernate.show_sql">
- ${hibernate.show_sql}
- </prop>
- <prop key="hibernate.jdbc.batch_size">
- ${hibernate.jdbc.batch_size}
- </prop>
- <prop key="hibernate.generate_statistics">
- ${hibernate.generate_statistics}
- </prop>
- <prop key="hibernate.hbm2ddl.auto">
- ${hibernate.hbm2ddl.auto}
- </prop>
- </props>
- </property>
- <property name="eventListeners">
- <map>
- <entry key="merge">
- <bean class="org.springframework.orm.hibernate3.support.IdTransferringMergeEventListener" />
- </entry>
- </map>
- </property>
- </bean>
- <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <property name="sessionFactory">
- <ref local="sessionFactory" />
- </property>
- </bean>
- <bean id="workflowStore" class="com.opensymphony.workflow.spi.hibernate3.SpringHibernateWorkflowStore">
- <property name="sessionFactory"><ref local="sessionFactory"/></property>
- <property name="propertySetDelegate">
- <bean id="propertySetDelegate" class="com.opensymphony.workflow.util.PropertySetDelegateImpl"/>
- </property>
- </bean>
- <bean id="workflowTypeResolver" class="com.opensymphony.workflow.util.SpringTypeResolver"/>
- <bean id="workflowFactory" class="com.opensymphony.workflow.spi.hibernate.SpringWorkflowFactory" init-method="init">
- <property name="resource"><value>workflows.xml</value></property>
- <property name="reload"><value>false</value></property>
- </bean>
- <bean id="osworkflowConfiguration" class="com.opensymphony.workflow.config.SpringConfiguration">
- <property name="store"><ref local="workflowStore"/></property>
- <property name="factory"><ref local="workflowFactory"/></property>
- </bean>
- <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
- <property name="transactionManager"><ref local="transactionManager"/></property>
- <property name="transactionAttributes">
- <props>
- <prop key="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- <!--
- <bean id="workflow" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces">
- <value>com.opensymphony.workflow.Workflow</value>
- </property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- <value>workflowTarget</value>
- </list>
- </property>
- </bean>
- <bean id="workflowTarget" class="com.opensymphony.workflow.basic.BasicWorkflow">
- <constructor-arg><value>shenbin</value></constructor-arg>
- <property name="configuration"><ref local="osworkflowConfiguration"/></property>
- </bean>
- -->
- <bean id="workflow" class="org.springframework.aop.framework.ProxyFactoryBean">
- <property name="proxyInterfaces">
- <value>tw.idv.idealist.base.WebWorkflow</value>
- </property>
- <property name="interceptorNames">
- <list>
- <value>transactionInterceptor</value>
- <value>workflowTarget</value>
- </list>
- </property>
- </bean>
- <bean id="workflowTarget" class="tw.idv.idealist.base.WebBasicWorkflow" scope="prototype">
- <property name="configuration"><ref local="osworkflowConfiguration"/></property>
- </bean>
- </beans>
LeaveAction.java
- package tw.idv.idealist.actions;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- import tw.idv.idealist.Leave;
- import tw.idv.idealist.base.WebWorkflow;
- public class LeaveAction extends HttpServlet {
- private static final long serialVersionUID = 1376955339587565809L;
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- doPost(req, resp);
- }
- public void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
- WebWorkflow workflow = (WebWorkflow)context.getBean("workflow");
- Leave leave = new Leave();
- String employee = req.getParameter("employee");
- workflow.setCaller(employee);
- long workflowId = leave.send(workflow);
- resp.sendRedirect("allow.jsp?workflowId=" + workflowId);
- }
- }
AllowAction.java
- package tw.idv.idealist.actions;
- import java.io.IOException;
- import javax.servlet.ServletException;
- import javax.servlet.http.HttpServlet;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import org.springframework.web.context.WebApplicationContext;
- import org.springframework.web.context.support.WebApplicationContextUtils;
- import tw.idv.idealist.Leave;
- import tw.idv.idealist.base.WebWorkflow;
- public class AllowAction extends HttpServlet {
- private static final long serialVersionUID = -8935624987243166064L;
- public void doGet(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- doPost(req, resp);
- }
- public void doPost(HttpServletRequest req, HttpServletResponse resp)
- throws ServletException, IOException {
- WebApplicationContext context = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
- WebWorkflow workflow = (WebWorkflow)context.getBean("workflow");
- String workflowId = req.getParameter("workflowId");
- String actionId = req.getParameter("actionId");
- Leave leave = new Leave();
- workflow.setCaller("manager");
- leave.allow(workflow, Long.parseLong(workflowId), Integer.parseInt(actionId));
- resp.sendRedirect("completed.jsp");
- }
- }
本来的做法其实来源于下面的文中:
http://read.newbooks.com.cn/info/197933.html
他里面包换的实现所在网址:
http://groups.google.com/group/ahxu/msg/c12d0f4a8950d22d