隨筆 - 63  文章 - 0  trackbacks - 0
          <2009年4月>
          2930311234
          567891011
          12131415161718
          19202122232425
          262728293012
          3456789

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

                  大家都知道,Struts控制器組件負責接受用戶請求,更通模型,以及返回給用戶合適的視圖組件.
          控制器將模型層和視圖層分開,這樣分離,可以為同一個模型開發出不同的視圖.
                  下面時Struts的三大主要組件
          ActionServlet組件:充當Struts框架的中央控制器
          RequestProcessor組件:充當每個子應用模塊的請求處理器
          Action組件:真正來處理一項具體的業務.

          一. Struts的init()方法
          Struts應用中只存在ActionServlet的一個實例,Servlet容器在啟動時,或者用戶首次請求ActionServlet時加載ActionServlet類.在這兩種情況下,servlet容器都會在ActionServlet容器被加載后立即執行它的init()方法,這可以保證ActionServlet處理用戶請求時已經被初始化.

          下面根據Init()講述Struts的初始化過程
        1. public void init() throws ServletException {   
        2.   
        3.         // Wraps the entire initialization in a try/catch to better handle   
        4.         // unexpected exceptions and errors to provide better feedback   
        5.         // to the developer   
        6.         try {   
        7. //調用initInternal()方法,初始化Struts框架內的消息資源,如與系統日志相關的通知,警告,和錯誤消息.   
        8. 1)initInternal();   
        9.   
        10. //調用ininOther()方法,從web.xml文件中加載ActionServlet的初始化參數,如config參數   
        11. 2)initOther();   
        12.   
        13. //調用initServlet()方法,從web.xml文件中加載ActionServlet的URL映射信息.同時還會注冊web.xml文件和Struts配置文件所使用的DTD文件,這些DTD文件用戶驗證web.xml和struts配置文件的語法.其中方法里的 digester類負責解析web.xml,對字符串servletMapping屬性進行初始化   
        14. 3) initServlet();   
        15.   
        16. //把ActionServlet實例放到ServletContext里   
        17. getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);   
        18.   
        19. //初始化一個factory,用于創建moduleConfig   
        20. initModuleConfigFactory();   
        21.   
        22. //,加載并解析默認struts配置文件/WEB-INF/struts-config.xml,同時創建MoudleConfig實例,放到ServletContext中   
        23. 4)ModuleConfig moduleConfig = initModuleConfig("", config);   
        24.   
        25. //加載并初始化默認子應用模塊的消息資源;講解MessageResources對象,把它存儲在ServletContext中.   
        26. 5)initModuleMessageResources(moduleConfig);   
        27.   
        28. //加載并初始化默認子應用模塊的數據源,如果在struts配置文件中沒有定義<data-sources >元素,忽略這一流程.   
        29. 6)initModuleDataSources(moduleConfig);   
        30.   
        31. //加載并初始化默認子應用的所有插件   
        32. 7)initModulePlugIns(moduleConfig);   
        33.   
        34. //凍結moduleConfig(,在方法返回之前不能修改它,否則將拋出異常)   
        35. moduleConfig.freeze();   
        36.            
        37. //如果還有其他子應用模塊,將重復4-7步   
        38.       Enumeration names = getServletConfig().getInitParameterNames();   
        39.             while (names.hasMoreElements()) {   
        40.                 String name = (String) names.nextElement();   
        41.                 if (!name.startsWith("config/")) {   
        42.                     continue;   
        43.                 }   
        44.                 String prefix = name.substring(6);   
        45.                 moduleConfig = initModuleConfig   
        46.                     (prefix, getServletConfig().getInitParameter(name));   
        47.                 initModuleMessageResources(moduleConfig);   
        48.                 initModuleDataSources(moduleConfig);   
        49.                 initModulePlugIns(moduleConfig);   
        50.                 moduleConfig.freeze();   
        51.      }   
        52.   
        53. //將各個子模塊應用(除了默認的)的前綴存到一個字符數組中,并放到servletcontext中   
        54. this.initModulePrefixes(this.getServletContext());   
        55. //釋放創建的用于讀取配置文件的digester實例,釋放內存   
        56.             this.destroyConfigDigester();   
        57.         } catch (UnavailableException ex) {   
        58.             throw ex;   
        59.         } catch (Throwable t) {   
        60.             // The follow error message is not retrieved from internal message   
        61.             // resources as they may not have been able to have been    
        62.             // initialized   
        63.             log.error("Unable to initialize Struts ActionServlet due to an "  
        64.                 + "unexpected exception or error thrown, so marking the "  
        65.                 + "servlet as unavailable.  Most likely, this is due to an "  
        66.                 + "incorrect or missing library dependency.", t);   
        67.             throw new UnavailableException(t.getMessage());   
        68.         }       
        69. }  
        70.        將各個子模塊應用(除了默認的)的前綴存到一個字符數組中,并放到servletcontext中,對于默認的子應用模塊,在appclication范圍內存放他的MoudleConfig實例的key為“org.apache.struts.action.MODULE”,其他模塊如/account,存放的key為org.apache.struts.action.MODULE/account,消息,數據源和插件等部分存在servletcontext的key和上述方法類似,不在說明.


          二.ActionServlet的process方法
          ActionServlet接受到HTTP請求后,在doget()或doPost()方法中都會調用process()方法來處理請求.

        71.  public void doGet(HttpServletRequest request,   
        72.               HttpServletResponse response)   
        73.         throws IOException, ServletException {   
        74.   
        75.         process(request, response);   
        76.   
        77. }   

        78.   public void doPost(HttpServletRequest request,   

        79.                HttpServletResponse response)   
        80.         throws IOException, ServletException {   
        81.   
        82.         process(request, response);   
        83.   
        84. }   
        85. 下面是process方法,它看上去并不復雜,但他調用的其他方法比較復雜. 
           protected void process(HttpServletRequest request, HttpServletResponse response)   

        86.         throws IOException, ServletException {   
        87.         //根據request里的信息從servletContext里找到相應的子模塊ModuleConfig,和它下面的MessageResources,并放到request里,使其他組件可以方便的供request里取得應用配置信息和消息資源.   
        88.         ModuleUtils.getInstance().selectModule(request, getServletContext());   
        89. //取出MoudleConfig實例config   
        90.         ModuleConfig config = getModuleConfig(request);   
        91.     //根據config里這個子模塊的信息,從servletcontext里,取出這個子模塊的RequestProcessor實例   
        92.         RequestProcessor processor = getProcessorForModule(config);   
        93.     //如果processor實例為空,就新建一個.同時放到servletcontext里.   
        94.         if (processor == null) {   
        95.            processor = getRequestProcessor(config);   
        96.         }   
        97. //調用RequestProcessor的process方法處理,   
        98.         processor.process(request, response);   
        99.     }  

        100. 三. 擴展ActionServlet
          從Struts1.1開始,為減輕ActionServlet的負擔,多數功能已經移到RequestProcessor類中,所以基本不用擴展ActionServlet

          如果需要創建自己的ActionServlet,則可以創建一個它的子類.覆蓋init()方法(或其他方法),可以寫一些自己的操作,但要先調用super.init();
          定義如下的類:

        101. package sample;    
        102. public class ExtendedActionServlet extends ActionServlet {    
        103.         public void init() throws ServletException {    
        104.                super.init();    
        105.                //do some operations    
        106.                ……………    
        107.         }    
        108. }   


        109. 擴展完類后,還應該在web.xml文件中如下配置:

        110. <servlet>    
        111.         <servlet-name>sample</servlet-name>    
        112.         <servlet-class>sample.ExtendedActionServlet</servlet-class>    
        113. </servlet>    
        114.     
        115. <servlet-mapping>    
        116.        <servlet-name>sample</servlet-name>    
        117.        <url-pattern>/action/*<url-pattern> 



        118. 上面的/action/*表示負責處理所有以/action為前綴的URL,后面的/表示轉義
          posted on 2009-04-05 11:10 lanxin1020 閱讀(194) 評論(0)  編輯  收藏 所屬分類: struts1
          主站蜘蛛池模板: 凉山| 丽江市| 大名县| 道孚县| 武平县| 阳西县| 志丹县| 门头沟区| 阿拉善左旗| 道孚县| 辰溪县| 丹东市| 正蓝旗| 壤塘县| 太康县| 西乌珠穆沁旗| 珠海市| 邓州市| 顺平县| 西林县| 呼图壁县| 汉源县| 五家渠市| 齐河县| 丽江市| 桃园市| 抚松县| 德兴市| 惠东县| 嘉义市| 墨玉县| 汽车| 亚东县| 秦皇岛市| 遂昌县| 霞浦县| 新乐市| 富民县| 富源县| 利辛县| 景东|