閱讀686 返回首頁    go 阿裏雲 go 技術社區[雲棲]


設計自己的MVC框架

取這樣一個標題太大,吸引眼球嘛@_@。

    事實是最近讀《J2EE設計模式》講述表達層模式的那幾章,書中有一個前端控製器+command模式的workflow例子,就琢磨著可以很簡單地擴展成一個MVC框架。花了一個下午改寫了下,對書中所述的理解更為深入。我想這也許對於學習和理解設計模式,以及初次接觸struts等MVC框架的人可能有點幫助。因為整個模型類似於struts,我把它取名叫strutslet^_^

(一)完整的類圖如下:

      10fd68bb171.jpg

1。前端控製器(FrontController):前端控製器提供了一個統一的位置來封裝公共請求處理,它的任務相當簡單,執行公共的任務,然後把請求轉交給相應的控製器。在strutslet中,前端控製器主要作用也在於此,它初始化並解析配置文件,接受每個請求,並簡單地把請求委托給調度器(Dispatcher),由調度器執行相應的動作(Action)。調度器把action返回的url返回給FrontController, FrontController負責轉發。

2。Action接口:command模式很好的例子,它是一個命令接口,每一個實現了此接口的action都封裝了某一個請求:新增一條數據記錄並更新model,或者把某個文件寫入磁盤。命令解耦了發送者和接受者之間聯係。發送者調用一個操作,接受者接受請求執行相應的動作,因為使用Command模式解耦,發送者無需知道接受者任何接口。

3。Dispatcher:調度器,負責流程的轉發,負責調用action去執行業務邏輯。由調度器選擇頁麵和action,它去除了應用行為和前端控製器間的耦合。調度器服務於前端控製器,它把model的更新委托給action,又提供頁麵選擇給FrontController

4。ActionForward:封裝了轉向操作所需要信息的一個模型,包括name和轉向url

5。ActionModel:解析配置文件後,將每一個Action封裝成一個ActionModel對象,所有ActionModel構成一個map,並存儲在ServletContext中,供整個框架使用。

(二)源代碼分析:

 1。Action接口,隻有一個execute方法,任何一個action都隻要實現此接口,並實現相應的業務邏輯,最後返回一個ActionForward,提供給Dispacher調用。

 

None.gif package  com.strutslet.core;
None.gif
None.gif import  javax.servlet.ServletContext;
None.gif import  javax.servlet.http.HttpServletRequest;
None.gif
None.gif import  com.strutslet.model.ActionForward;
None.gif
ExpandedBlockStart.gif /** 
InBlock.gif * command接口
InBlock.gif * 
 @author  dennis
InBlock.gif *
ExpandedBlockEnd.gif 
 */ 

ExpandedBlockStart.gif public   interface  Action  {
InBlock.gif  public  ActionForward execute(HttpServletRequest request,ServletContext context); 
ExpandedBlockEnd.gif

None.gif 
None.gif

 

比如,我們要實現一個登陸係統,LoginAction驗證用戶名和密碼,如果正確,返回success頁麵,如果登陸失敗,返回fail頁麵:

None.gif package  com.strutslet.demo;
None.gif
None.gif import  javax.servlet.ServletContext;
None.gif import  javax.servlet.http.HttpServletRequest;
None.gif
None.gif import  com.strutslet.core.Action;
None.gif import  com.strutslet.model.ActionForward;
None.gif
ExpandedBlockStart.gif public   class  LoginAction  implements  Action  {
InBlock.gif
InBlock.gif  private  String name = "" ;
InBlock.gif  public  ActionForward execute(HttpServletRequest request,
ExpandedSubBlockStart.gif   ServletContext context)  {
InBlock.gif  String userName = request.getParameter( " userName " );
InBlock.gif  String password = request.getParameter( " password " );
ExpandedSubBlockStart.gif         if (userName.equals( " dennis " ) && password.equals( " 123 " )) {
InBlock.gif      request.setAttribute( " name " , name);
InBlock.gif       return  ActionForward.SUCCESS;   // 登陸成功,返回success 
ExpandedSubBlockEnd.gif 
        } 
else 
InBlock.gif          return  ActionForward.FAIL;     // 否則,返回fail 
ExpandedSubBlockEnd.gif 
 } 

InBlock.gif 
ExpandedBlockEnd.gif

None.gif 
None.gif


2。還是先來看下兩個模型:ActionForward和ActionModel,沒什麼東西,屬性以及相應的getter,setter方法:

None.gif package  com.strutslet.model;
None.gif
ExpandedBlockStart.gif /** 
InBlock.gif * 類說明:轉向模型
InBlock.gif * 
 @author  dennis
InBlock.gif *
ExpandedBlockEnd.gif * 
 */ 

ExpandedBlockStart.gif public   class  ActionForward  {
InBlock.gif  private  String name;       // forward的name 
InBlock.gif 
  private  String viewUrl;    // forward的url 
InBlock.gif 
  public   static   final  ActionForward SUCCESS = new  ActionForward( " success " );
InBlock.gif  public   static   final  ActionForward FAIL = new  ActionForward( " fail " );
InBlock.gif 
ExpandedSubBlockStart.gif  public   ActionForward(String name) {
InBlock.gif   this .name = name;
ExpandedSubBlockEnd.gif } 

InBlock.gif 
ExpandedSubBlockStart.gif   public  ActionForward(String name, String viewUrl)  {
InBlock.gif   super ();
InBlock.gif   this .name  =  name;
InBlock.gif   this .viewUrl  =  viewUrl;
ExpandedSubBlockEnd.gif } 

InBlock.gif 
InBlock.gif  // dot.gifname和viewUrl的getter和setter方法 
InBlock.gif 

ExpandedBlockEnd.gif
   
None.gif
None.gif我們看到ActionForward預先封裝了SUCCESS和FAIL對象。
None.gif
None.gif // ActionModel.java 
None.gif 

None.gif package  com.strutslet.model;
None.gif
None.gif import  java.util.Map;
None.gif
ExpandedBlockStart.gif /** 
InBlock.gif * 類說明:
InBlock.gif * 
 @author  dennis
InBlock.gif *
ExpandedBlockEnd.gif 
 */ 

None.gif 
ExpandedBlockStart.gif public   class  ActionModel  {
InBlock.gif  private  String path;  //  action的path 
InBlock.gif 

InBlock.gif  private  String className;  //  action的class 
InBlock.gif 

InBlock.gif  private  Map < String, ActionForward >  forwards;  //  action的forward 
InBlock.gif 

ExpandedSubBlockStart.gif   public  ActionModel() {} 
InBlock.gif 
InBlock.gif  public  ActionModel(String path, String className,
ExpandedSubBlockStart.gif   Map < String, ActionForward >  forwards)  {
InBlock.gif   super ();
InBlock.gif   this .path  =  path;
InBlock.gif   this .className  =  className;
InBlock.gif   this .forwards  =  forwards;
ExpandedSubBlockEnd.gif } 

InBlock.gif 
InBlock.gif 
InBlock.gif  // dot.gif相應的getter和setter方法      
InBlock.gif 

ExpandedBlockEnd.gif

None.gif 
None.gif


3。知道了兩個模型是什麼樣,也應該可以猜到我們的配置文件大概是什麼樣的了,與struts的配置文件格式類似:

 

None.gif <? xml version = " 1.0 "  encoding = " UTF-8 " ?> 
None.gif < actions > 
None.gif   < action path = " /login " 
None.gif           class = " com.strutslet.demo.LoginAction " > 
None.gif      < forward name = " success "  url = " hello.jsp " /> 
None.gif      < forward name = " fail "  url = " fail.jsp " /> 
None.gif    </ action >        
None.gif </ actions >

 

path是在應用中將被調用的路徑,class指定了調用的哪個action,forward元素指定了轉向,比如我們這裏如果是success就轉向hello.jsp,失敗的話轉向fail.jsp,這裏配置了demo用到的LoginAction。

4。Dispacher接口,主要是getNextPage方法,此方法負責獲得下一個頁麵將導向哪裏,提供給前端控製器轉發。

None.gif package  com.strutslet.core;
None.gif
None.gif
None.gif import  javax.servlet.ServletContext;
None.gif import  javax.servlet.http.HttpServletRequest;
None.gif
ExpandedBlockStart.gif /** 
InBlock.gif * service to worker模式,提供給FrontController使用
InBlock.gif * 負責流程轉發
InBlock.gif * 
 @author  dennis
InBlock.gif *
ExpandedBlockEnd.gif 
 */ 

ExpandedBlockStart.gif public   interface  Dispatcher  {
InBlock.gif  public   void  setServletContext(ServletContext context);
InBlock.gif  public  String getNextPage(HttpServletRequest request,ServletContext context);
ExpandedBlockEnd.gif

None.gif 
None.gif


5。原先書中實現了一個WorkFlow的Dispatcher,按照順序調用action,實現工作流調用。而我們所需要的是根據請求的path 調用相應的action,執行action的execute方法返回一個ActionForward,然後得到ActionForward的 viewUrl,將此viewUrl提供給前端控製器轉發,看看它的getNextPage方法:

ExpandedBlockStart.gif public  String getNextPage(HttpServletRequest request, ServletContext context)  {
InBlock.gif  setServletContext(context);
InBlock.gif
InBlock.gif  Map < String, ActionModel >  actions  =  (Map < String, ActionModel > ) context
InBlock.gif    .getAttribute(Constant.ACTIONS_ATTR);    // 從ServletContext得到所有action信息 
InBlock.gif 
  String reqPath  =  (String) request.getAttribute(Constant.REQUEST_ATTR); // 發起請求的path 
InBlock.gif 
  ActionModel actionModel  =  actions.get(reqPath);   // 根據path得到相應的action 
InBlock.gif 
  String forward_name  =   "" ;
InBlock.gif  ActionForward actionForward;
ExpandedSubBlockStart.gif   try   {
InBlock.gif   Class c  =  Class.forName(actionModel.getClassName());   // 每個請求對應一個action實例 
InBlock.gif 

InBlock.gif   Action action  =  (Action) c.newInstance();
InBlock.gif   actionForward  =  action.execute(request, context);   // 執行action的execute方法 
InBlock.gif 
   forward_name  =  actionForward.getName();
InBlock.gif   
ExpandedSubBlockStart.gif  } 
  catch  (Exception e)  {
InBlock.gif   log.error( " can not find action  " + actionModel.getClassName());
InBlock.gif   e.printStackTrace();
ExpandedSubBlockEnd.gif  } 

InBlock.gif 
InBlock.gif  actionForward  =  actionModel.getForwards().get(forward_name);
ExpandedSubBlockStart.gif   if  (actionForward  ==   null )  {
InBlock.gif   log.error( " can not find page for forward  " + forward_name);
InBlock.gif    return   null ;
ExpandedSubBlockEnd.gif  } 
  else 
InBlock.gif    return  actionForward.getViewUrl();       // 返回ActionForward的viewUrl 
ExpandedBlockEnd.gif 
 } 

None.gif 
None.gif


6。前端控製器(FrontController),它的任務我們已經很清楚,初始化配置文件;存儲所有action到 ServletContext供整個框架使用;得到發起請求的path,提供給Dispachter查找相應的action;調用Dispatcher,執行getNextPage方法得到下一個頁麵的url並轉發:

ExpandedBlockStart.gif public   void  init()  throws  ServletException  {
InBlock.gif
InBlock.gif   // 初始化配置文件 
InBlock.gif 

InBlock.gif  ServletContext context = getServletContext();
InBlock.gif  String config_file  = getServletConfig().getInitParameter( " config " );
InBlock.gif  String dispatcher_name = getServletConfig().getInitParameter( " dispatcher " );
InBlock.gif   if  (config_file  ==   null   ||  config_file.equals( "" ))
InBlock.gif   config_file  =   " /WEB-INF/strutslet-config.xml " ;  // 默認是/WEB-INF/下麵的strutslet-config 
InBlock.gif 
   if (dispatcher_name == null || dispatcher_name.equals( "" ))
InBlock.gif   dispatcher_name = Constant.DEFAULT_DISPATCHER;
InBlock.gif    
ExpandedSubBlockStart.gif   try   {
InBlock.gif   Map < String, ActionModel >  resources  =  ConfigUtil.newInstance()   // 工具類解析配置文件 
InBlock.gif 
     .parse(config_file, context);
InBlock.gif   context.setAttribute(Constant.ACTIONS_ATTR, resources);   // 存儲在ServletContext中 
InBlock.gif 
   log.info( " 初始化strutslet配置文件成功 " );
ExpandedSubBlockStart.gif  } 
  catch  (Exception e)  {
InBlock.gif   log.error( " 初始化strutslet配置文件失敗 " );
InBlock.gif   e.printStackTrace();
ExpandedSubBlockEnd.gif  } 

InBlock.gif 
InBlock.gif   // 實例化Dispacher 
InBlock.gif 

ExpandedSubBlockStart.gif    try {
InBlock.gif   Class c  =  Class.forName(dispatcher_name);
InBlock.gif      Dispatcher dispatcher  =  (Dispatcher) c.newInstance();
InBlock.gif      context.setAttribute(Constant.DISPATCHER_ATTR, dispatcher);  // 放在ServletContext 
InBlock.gif 
      log.info( " 初始化Dispatcher成功 " );
ExpandedSubBlockStart.gif  } 
catch (Exception e)  {
InBlock.gif    log.error( " 初始化Dispatcher失敗 " );
InBlock.gif      e.printStackTrace();
ExpandedSubBlockEnd.gif  } 

InBlock.gif 
InBlock.gif  dot.gif..
InBlock.gif
InBlock.gif


doGet()和doPost方法我們都讓它調用process方法:

None.gif protected   void  process(HttpServletRequest request,
ExpandedBlockStart.gif   HttpServletResponse response)  throws  ServletException, IOException  {
InBlock.gif  ServletContext context  =  getServletContext();
InBlock.gif
InBlock.gif         // 獲取action的path  
InBlock.gif 
  String reqURI  =  request.getRequestURI();
InBlock.gif   int  i = reqURI.lastIndexOf( " . " );
InBlock.gif  String contextPath = request.getContextPath();
InBlock.gif  String path = reqURI.substring(contextPath.length(),i);
InBlock.gif  
InBlock.gif  request.setAttribute(Constant.REQUEST_ATTR, path);
InBlock.gif  Dispatcher dispatcher  =  (Dispatcher) context.getAttribute(Constant.DISPATCHER_ATTR);
InBlock.gif
InBlock.gif   //  make sure we don't cache dynamic data 
InBlock.gif 
  response.setHeader( " Cache-Control " ,  " no-cache " );
InBlock.gif  response.setHeader( " Pragma " ,  " no-cache " );
InBlock.gif
InBlock.gif   //  use the dispatcher to find the next page 
InBlock.gif 
  String nextPage  =  dispatcher.getNextPage(request, context); // 調用Dispatcher的getNextPage
InBlock.gif
InBlock.gif  
 //  forward control to the view 
InBlock.gif 
  RequestDispatcher forwarder  =  request.getRequestDispatcher( " / " 
InBlock.gif     +  nextPage);
InBlock.gif  forwarder.forward(request, response);   // 轉發頁麵 
ExpandedBlockEnd.gif 
 } 

None.gif 
None.gif


7。最後,web.xml的配置就非常簡單了,配置前端控製器,提供啟動參數(配置文件所在位置,為空就查找/WEB-INF/下麵的strutslet-config.xml文件),我們把所有以action結尾的請求都交給FrontController處理:

 

None.gif < servlet > 
None.gif     < servlet - name > StrutsletController </ servlet - name > 
None.gif     < servlet - class > com.strutslet.core.FrontController </ servlet - class > 
None.gif     <!--   
None.gif     < init - param > 
None.gif          < param - name > config </ param - name > 
None.gif          < param - value >/ WEB - INF / strutslet - config.xml </ param - value > 
None.gif     </ init - param > 
None.gif     --> 
None.gif        < load - on - startup > 0 </ load - on - startup > 
None.gif   </ servlet > 
None.gif  < servlet - mapping > 
None.gif     < servlet - name > StrutsletController </ servlet - name > 
None.gif     < url - pattern >* .action </ url - pattern > 
None.gif  </ servlet - mapping >
文章轉自莊周夢蝶  ,原文發布5.16

最後更新:2017-05-17 00:48:09

  上一篇:go  縈繞於指尖的節奏:今天,我們來聊聊機械鍵盤
  下一篇:go  隨鼓點躍動的身心:那些悠揚易推的耳機