設計自己的MVC框架(2)

轉自 http://www.blogjava.net/killme2008/archive/2007/02/06/98227.html


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

 

 public   void  init()  throws  ServletException  {

  
 // 初始化配置文件 
 

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

 
  
 // 實例化Dispacher 
 

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

 
  ..


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

 

 protected   void  process(HttpServletRequest request,
   HttpServletResponse response) 
 throws  ServletException, IOException  {
  ServletContext context 
 =  getServletContext();

        
 // 獲取action的path  
 
  String reqURI  =  request.getRequestURI();
  
 int  i = reqURI.lastIndexOf( " . " );
  String contextPath
 = request.getContextPath();
  String path
 = reqURI.substring(contextPath.length(),i);
  
  request.setAttribute(Constant.REQUEST_ATTR, path);
  Dispatcher dispatcher 
 =  (Dispatcher) context.getAttribute(Constant.DISPATCHER_ATTR);

  
 //  make sure we don't cache dynamic data 
 
  response.setHeader( " Cache-Control "  " no-cache " );
  response.setHeader(
 " Pragma "  " no-cache " );

  
 //  use the dispatcher to find the next page 
 
  String nextPage  =  dispatcher.getNextPage(request, context); // 調用Dispatcher的getNextPage

  
 //  forward control to the view 
 
  RequestDispatcher forwarder  =  request.getRequestDispatcher( " / " 
    
 +  nextPage);
  forwarder.forward(request, response);  
 // 轉發頁面 
 
 } 

 

 

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

 

 < servlet > 
    
 < servlet - name > StrutsletController </ servlet - name > 
    
 < servlet - class > com.strutslet.core.FrontController </ servlet - class > 
    
 <!--   
    
 < init - param > 
         
 < param - name > config </ param - name > 
         
 < param - value >/ WEB - INF / strutslet - config.xml </ param - value > 
    
 </ init - param > 
    
 --> 
       
 < load - on - startup > 0 </ load - on - startup > 
  
 </ servlet > 
 
 < servlet - mapping > 
    
 < servlet - name > StrutsletController </ servlet - name > 
    
 < url - pattern >* .action </ url - pattern > 
 
 </ servlet - mapping >

 

最後,讓我們看看整個框架圖:

 test.jpg

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章