事实是最近读《J2EE设计模式》讲述表达层模式的那几章,书中有一个前端控制器 command模式的workflow例子,就琢磨着可以很简单地扩展成一个MVC框架。 
						
							2.»¹ÊÇÏÈÀ´¿´ÏÂÁ½¸öÄ£ÐÍ£ºActionForwardºÍActionModel£¬Ã»Ê²Ã´¶«Î÷£¬ÊôÐÔÒÔ¼°ÏàÓ¦µÄgetter,setter·½·¨£º
- 
 - /** 
 -  * Àà˵Ã÷:תÏòÄ£ÐÍ 
 -  * @author dennis 
 -  * 
 -  * */ 
 - public class ActionForward { 
 -  private String name;      //forwardµÄname 
 -  private String viewUrl;   //forwardµÄurl 
 -  public static final ActionForward SUCCESS=new ActionForward("success"); 
 -  public static final ActionForward FAIL=new ActionForward("fail"); 
 - 
 -  public  ActionForward(String name){ 
 -   this.name=name; 
 -  } 
 - 
 -  public ActionForward(String name, String viewUrl) { 
 -   super(); 
 -   this.name = name; 
 -   this.viewUrl = viewUrl; 
 -  } 
 - 
 -  //...nameºÍviewUrlµÄgetterºÍsetter·½·¨ 
 - 
 - }    
 
 ÎÒÃÇ¿´µ½ActionForwardÔ¤ÏÈ·â×°ÁËSUCCESSºÍFAIL¶ÔÏó¡£
- 
 - public class ActionModel { 
 -  private String path; // actionµÄpath 
 - 
 -  private String className; // actionµÄclass 
 - 
 -  private Map<String, ActionForward> forwards; // actionµÄforward 
 - 
 -  public ActionModel(){} 
 - 
 -  public ActionModel(String path, String className, 
 -    Map<String, ActionForward> forwards) { 
 -   super(); 
 -   this.path = path; 
 -   this.className = className; 
 -   this.forwards = forwards; 
 -  } 
 - 
 - 
 -  //...ÏàÓ¦µÄgetterºÍsetter·½·¨      
 - 
 - } 
 
 3¡£ÖªµÀÁËÁ½¸öÄ£ÐÍÊÇʲôÑù£¬Ò²Ó¦¸Ã¿ÉÒԲµ½ÎÒÃǵÄÅäÖÃÎļþ´ó¸ÅÊÇʲôÑùµÄÁË£¬ÓëstrutsµÄÅäÖÃÎļþ¸ñʽÀàËÆ£º
- 
 - <?xml version="1.0" encoding="UTF-8"?> 
 - <actions> 
 -   <action path="/login" 
 -           class="com.strutslet.demo.LoginAction"> 
 -      <forward name="success" url="hello.jsp"/> 
 -      <forward name="fail" url="fail.jsp"/> 
 -    </action>        
 - </actions> 
 
 pathÊÇÔÚÓ¦ÓÃÖн«±»µ÷ÓõÄ·¾¶£¬classÖ¸¶¨Á˵÷ÓõÄÄĸöaction£¬forwardÔªËØÖ¸¶¨ÁËתÏò£¬±ÈÈçÎÒÃÇÕâÀïÈç¹ûÊÇsuccess¾ÍתÏòhello.jsp£¬Ê§°ÜµÄ»°×ªÏòfail.jsp£¬ÕâÀïÅäÖÃÁËdemoÓõ½µÄLoginAction¡£
4¡£Dispacher½Ó¿Ú£¬Ö÷ÒªÊÇgetNextPage·½·¨£¬´Ë·½·¨¸ºÔð»ñµÃÏÂÒ»¸öÒ³Ãæ½«µ¼ÏòÄÄÀÌṩ¸øÇ°¶Ë¿ØÖÆÆ÷ת·¢¡£
- 
 - public interface Dispatcher { 
 -  public void setServletContext(ServletContext context); 
 -  public String getNextPage(HttpServletRequest request,ServletContext context); 
 - } 
 
 5¡£5¡£ÔÏÈÊéÖÐʵÏÖÁËÒ»¸öWorkFlowµÄDispatcher£¬°´ÕÕ˳Ðòµ÷ÓÃaction£¬ÊµÏÖ¹¤×÷Á÷µ÷Ó᣶øÎÒÃÇËùÐèÒªµÄÊǸù¾ÝÇëÇóµÄpathµ÷ÓÃÏàÓ¦µÄaction£¬Ö´ÐÐactionµÄexecute·½·¨·µ»ØÒ»¸öActionForward£¬È»ºóµÃµ½ActionForwardµÄviewUrl£¬½«´ËviewUrlÌṩ¸øÇ°¶Ë¿ØÖÆÆ÷ת·¢£¬¿´¿´ËüµÄgetNextPage·½·¨£º
- 
 - public String getNextPage(HttpServletRequest request, ServletContext context) { 
 -   setServletContext(context); 
 - 
 -   Map<String, ActionModel> actions = (Map<String, ActionModel>) context 
 -     .getAttribute(Constant.ACTIONS_ATTR);   //´ÓServletContextµÃµ½ËùÓÐactionÐÅÏ¢ 
 -   String reqPath = (String) request.getAttribute(Constant.REQUEST_ATTR);//·¢ÆðÇëÇóµÄpath 
 -   ActionModel actionModel = actions.get(reqPath);  //¸ù¾ÝpathµÃµ½ÏàÓ¦µÄaction 
 -   String forward_name = ""; 
 -   ActionForward actionForward; 
 -   try { 
 -    Class c = Class.forName(actionModel.getClassName());  //ÿ¸öÇëÇó¶ÔÓ¦Ò»¸öactionʵÀý 
 - 
 -    Action action = (Action) c.newInstance(); 
 -    actionForward = action.execute(request, context);  //Ö´ÐÐactionµÄexecute·½·¨ 
 -    forward_name = actionForward.getName(); 
 -     
 -   } catch (Exception e) { 
 -    log.error("can not find action "+actionModel.getClassName()); 
 -    e.printStackTrace(); 
 -   } 
 - 
 -   actionForward = actionModel.getForwards().get(forward_name); 
 -   if (actionForward == null) { 
 -    log.error("can not find page for forward "+forward_name); 
 -    return null; 
 -   } else 
 -    return actionForward.getViewUrl();      //·µ»ØActionForwardµÄviewUrl 
 -  }