<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    隨筆 - 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的初始化過程
  • public void init() throws ServletException {   
  •   
  •         // Wraps the entire initialization in a try/catch to better handle   
  •         // unexpected exceptions and errors to provide better feedback   
  •         // to the developer   
  •         try {   
  • //調用initInternal()方法,初始化Struts框架內的消息資源,如與系統日志相關的通知,警告,和錯誤消息.   
  • 1)initInternal();   
  •   
  • //調用ininOther()方法,從web.xml文件中加載ActionServlet的初始化參數,如config參數   
  • 2)initOther();   
  •   
  • //調用initServlet()方法,從web.xml文件中加載ActionServlet的URL映射信息.同時還會注冊web.xml文件和Struts配置文件所使用的DTD文件,這些DTD文件用戶驗證web.xml和struts配置文件的語法.其中方法里的 digester類負責解析web.xml,對字符串servletMapping屬性進行初始化   
  • 3) initServlet();   
  •   
  • //把ActionServlet實例放到ServletContext里   
  • getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);   
  •   
  • //初始化一個factory,用于創建moduleConfig   
  • initModuleConfigFactory();   
  •   
  • //,加載并解析默認struts配置文件/WEB-INF/struts-config.xml,同時創建MoudleConfig實例,放到ServletContext中   
  • 4)ModuleConfig moduleConfig = initModuleConfig("", config);   
  •   
  • //加載并初始化默認子應用模塊的消息資源;講解MessageResources對象,把它存儲在ServletContext中.   
  • 5)initModuleMessageResources(moduleConfig);   
  •   
  • //加載并初始化默認子應用模塊的數據源,如果在struts配置文件中沒有定義<data-sources >元素,忽略這一流程.   
  • 6)initModuleDataSources(moduleConfig);   
  •   
  • //加載并初始化默認子應用的所有插件   
  • 7)initModulePlugIns(moduleConfig);   
  •   
  • //凍結moduleConfig(,在方法返回之前不能修改它,否則將拋出異常)   
  • moduleConfig.freeze();   
  •            
  • //如果還有其他子應用模塊,將重復4-7步   
  •       Enumeration names = getServletConfig().getInitParameterNames();   
  •             while (names.hasMoreElements()) {   
  •                 String name = (String) names.nextElement();   
  •                 if (!name.startsWith("config/")) {   
  •                     continue;   
  •                 }   
  •                 String prefix = name.substring(6);   
  •                 moduleConfig = initModuleConfig   
  •                     (prefix, getServletConfig().getInitParameter(name));   
  •                 initModuleMessageResources(moduleConfig);   
  •                 initModuleDataSources(moduleConfig);   
  •                 initModulePlugIns(moduleConfig);   
  •                 moduleConfig.freeze();   
  •      }   
  •   
  • //將各個子模塊應用(除了默認的)的前綴存到一個字符數組中,并放到servletcontext中   
  • this.initModulePrefixes(this.getServletContext());   
  • //釋放創建的用于讀取配置文件的digester實例,釋放內存   
  •             this.destroyConfigDigester();   
  •         } catch (UnavailableException ex) {   
  •             throw ex;   
  •         } catch (Throwable t) {   
  •             // The follow error message is not retrieved from internal message   
  •             // resources as they may not have been able to have been    
  •             // initialized   
  •             log.error("Unable to initialize Struts ActionServlet due to an "  
  •                 + "unexpected exception or error thrown, so marking the "  
  •                 + "servlet as unavailable.  Most likely, this is due to an "  
  •                 + "incorrect or missing library dependency.", t);   
  •             throw new UnavailableException(t.getMessage());   
  •         }       
  • }  
  •        將各個子模塊應用(除了默認的)的前綴存到一個字符數組中,并放到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()方法來處理請求.

  •  public void doGet(HttpServletRequest request,   
  •               HttpServletResponse response)   
  •         throws IOException, ServletException {   
  •   
  •         process(request, response);   
  •   
  • }   

  •   public void doPost(HttpServletRequest request,   

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

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

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

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

  • package sample;    
  • public class ExtendedActionServlet extends ActionServlet {    
  •         public void init() throws ServletException {    
  •                super.init();    
  •                //do some operations    
  •                ……………    
  •         }    
  • }   


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

  • <servlet>    
  •         <servlet-name>sample</servlet-name>    
  •         <servlet-class>sample.ExtendedActionServlet</servlet-class>    
  • </servlet>    
  •     
  • <servlet-mapping>    
  •        <servlet-name>sample</servlet-name>    
  •        <url-pattern>/action/*<url-pattern> 



  • 上面的/action/*表示負責處理所有以/action為前綴的URL,后面的/表示轉義
    posted on 2009-04-05 11:10 lanxin1020 閱讀(193) 評論(0)  編輯  收藏 所屬分類: struts1
    主站蜘蛛池模板: 在线v片免费观看视频| 风间由美在线亚洲一区| 精品久久久久久国产免费了| 国产成人3p视频免费观看| 国产成人精品日本亚洲专| www视频在线观看免费| 亚洲女人影院想要爱| 99爱在线精品免费观看| 亚洲人成人77777网站不卡| 成人片黄网站A毛片免费| 亚洲精品无码专区| 国产又黄又爽又猛的免费视频播放| 国产精品亚洲专区无码WEB| 国产精品成人免费综合| 夜夜爽妓女8888视频免费观看| 国产午夜亚洲精品理论片不卡| 成人免费ā片在线观看| 亚洲视频在线播放| 18勿入网站免费永久| 亚洲色偷偷偷综合网| 亚洲AV网站在线观看| 成人性做爰aaa片免费看| 久久亚洲国产成人精品性色| 无码专区永久免费AV网站| 国产亚洲一卡2卡3卡4卡新区| 国产亚洲精品高清在线| 毛片无码免费无码播放| 亚洲欧美日韩中文字幕一区二区三区 | 亚洲精品资源在线| 大学生高清一级毛片免费| 在线播放国产不卡免费视频| 亚洲AV日韩精品久久久久久| 久久久久免费看黄A片APP| 在线看亚洲十八禁网站| 国产亚洲av片在线观看16女人| 亚洲人成免费网站| 久青草国产免费观看| 亚洲国语在线视频手机在线| 亚洲成a人一区二区三区| 最近中文字幕无免费| 羞羞漫画在线成人漫画阅读免费|