標籤:des style http color java 使用 os io
ActionServlet類是Struts架構的內建核心控制器組件,它繼承了javax.servlet.http.HttpServlet類,Struts的啟動一般從載入ActionServlet開始,因此它在MVC模型中扮演中央控制器的角色,負責接受使用者請求,返回給使用者合適的視圖組件.控制器將模型層和視圖層分開,這樣分離,可以為同一個模型開發出不同的視圖.
下面是Struts的三大主要組件:
ActionServlet組件:充當Struts架構的中央控制器;Struts應用中只存在ActionServlet的一個執行個體。
RequestProcessor組件:充當每個子應用模組的要求處理常式。
Action組件:真正來處理一項具體的業務.。
ActionServlet
Servlet容器在啟動時,或者使用者首次請求ActionServlet時載入ActionServlet類.在這兩種情況下,servlet容器都會在ActionServlet容器被載入後立即執行它的init()方法,這可以保證ActionServlet處理使用者請求時已經被初始化.
Java代碼——init()
public void init() throws ServletException {final String configPrefix = "config/";final int configPrefixLength = configPrefix.length() - 1;try {// 調用initInternal()方法,初始化Struts架構內的訊息資源,如與系統日誌相關的通知,警告,和錯誤訊息.initInternal();// 調用ininOther()方法,從web.xml檔案中載入ActionServlet的初始化參數,如config參數initOther();// 調用initServlet()方法,從web.xml檔案中載入ActionServlet的URL映射資訊.// 同時還會註冊web.xml檔案和Struts設定檔所使用的DTD檔案,// 這些DTD檔案使用者驗證web.xml和struts設定檔的文法.其中方法裡的 digester類負責解析web.xml,// 對字串servletMapping屬性進行初始化initServlet();initChain();// 把ActionServlet執行個體放到ServletContext裡getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);// 初始化一個factory,用於建立moduleConfiginitModuleConfigFactory();// 載入並解析預設struts設定檔/WEB-INF/struts-config.xml,同時建立MoudleConfig執行個體,放到ServletContext中ModuleConfig moduleConfig = initModuleConfig("", config);// 載入並初始化預設子應用模組的訊息資源;講解MessageResources對象,把它儲存在ServletContext中.initModuleMessageResources(moduleConfig);// 載入並初始化預設子應用的所有外掛程式initModulePlugIns(moduleConfig);initModuleFormBeans(moduleConfig);initModuleForwards(moduleConfig);initModuleExceptionConfigs(moduleConfig);initModuleActions(moduleConfig);// 凍結moduleConfig(在方法返回之前不能修改它,否則將拋出異常)moduleConfig.freeze();Enumeration names = getServletConfig().getInitParameterNames();while (names.hasMoreElements()) {String name = (String) names.nextElement();if (!name.startsWith(configPrefix)) {continue;}String prefix = name.substring(configPrefixLength);moduleConfig = initModuleConfig(prefix, getServletConfig().getInitParameter(name));initModuleMessageResources(moduleConfig);initModulePlugIns(moduleConfig);initModuleFormBeans(moduleConfig);initModuleForwards(moduleConfig);initModuleExceptionConfigs(moduleConfig);initModuleActions(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// initializedlog.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());}}
—————————————————————————————————————————————————
當ActionServlet接受到HTTP請求後,在doget()或doPost()方法中都會調用process()方法來處理請求.
Java代碼——process()
protected void process(HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException {// 根據request裡的資訊從servletContext裡找到相應的子模組ModuleConfig,// 和它下面的MessageResources,並放到request裡,使其他組件可以方便的供request裡取得應用配置資訊和訊息資源.ModuleUtils.getInstance().selectModule(request, getServletContext());// 取出MoudleConfig執行個體configModuleConfig 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.process()看起來不複雜,但是他調用的RequestProcessor.process()方法比較複雜(下篇部落格會介紹)。
擴充ActionServlet類
從Struts1.1開始,為減輕ActionServlet的負擔,多數功能已經移到RequestProcessor類中,所以基本不用擴充ActionServlet類。
如果需要建立自己的ActionServlet,則可以建立一個它的子類.覆蓋init()方法(或其他方法),可以寫一些自己的操作,但要先調用super.init();
定義如下的類:
package sample; public classExtendedActionServlet extends ActionServlet { public void init() throwsServletException { 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,後面的/表示轉義
總結
Web伺服器接到Request請求時,首先載入並初始化ActionServlet。ActionServlet會讀取struts-config.xml設定檔,把設定檔資訊存入ActionMapping對象中供以後使用。在Struts中,它的主要作用是用來接收使用者的請求資訊,然後根據系統配置要求將請求傳遞給相應的Action對象。ActionServlet並不是真正幹活的類,真正幹活的是RequestDispatcher和Action。