接上例: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) {
-
- super.context = new BasicWorkflowContext(caller);
- }
- }
修改如下几个文件:
applicationContext.xml
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/browse_thread/thread/2bf91654d871d7e4/5adfec0e7e83c351?#5adfec0e7e83c351
http://groups.google.com/group/ahxu/msg/c12d0f4a8950d22d