大家都知道,Struts控制器組件負(fù)責(zé)接受用戶請求,更通模型,以及返回給用戶合適的視圖組件.
控制器將模型層和視圖層分開,這樣分離,可以為同一個模型開發(fā)出不同的視圖.
下面時Struts的三大主要組件
ActionServlet組件:充當(dāng)Struts框架的中央控制器
RequestProcessor組件:充當(dāng)每個子應(yīng)用模塊的請求處理器
Action組件:真正來處理一項具體的業(yè)務(wù).
一. Struts的init()方法
Struts應(yīng)用中只存在
ActionServlet的一個實例,Servlet容器在啟動時,或者用戶首次請求
ActionServlet時加載
ActionServlet類.在這兩種情況下,servlet容器都會在
ActionServlet容器被加載后立即執(zhí)行它的init()方法,這可以保證
ActionServlet處理用戶請求時已經(jīng)被初始化.
下面根據(jù)Init()講述Struts的初始化過程
public void init() throws ServletException {
try {
1)initInternal();
2)initOther();
3) initServlet();
getServletContext().setAttribute(Globals.ACTION_SERVLET_KEY, this);
initModuleConfigFactory();
4)ModuleConfig moduleConfig = initModuleConfig("", config);
5)initModuleMessageResources(moduleConfig);
6)initModuleDataSources(moduleConfig);
7)initModulePlugIns(moduleConfig);
moduleConfig.freeze();
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();
}
this.initModulePrefixes(this.getServletContext());
this.destroyConfigDigester();
} catch (UnavailableException ex) {
throw ex;
} catch (Throwable t) {
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());
}
}
將各個子模塊應(yīng)用(除了默認(rèn)的)的前綴存到一個字符數(shù)組中,并放到servletcontext中,對于默認(rèn)的子應(yīng)用模塊,在appclication范圍內(nèi)存放他的MoudleConfig實例的key為“org.apache.struts.action.MODULE”,其他模塊如/account,存放的key為org.apache.struts.action.MODULE/account,消息,數(shù)據(jù)源和插件等部分存在servletcontext的key和上述方法類似,不在說明.
二.ActionServlet的process方法
當(dāng)ActionServlet接受到HTTP請求后,在doget()或doPost()方法中都會調(diào)用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方法,它看上去并不復(fù)雜,但他調(diào)用的其他方法比較復(fù)雜.
protected void process(HttpServletRequest request, HttpServletResponse response)
throws IOException, ServletException {
ModuleUtils.getInstance().selectModule(request, getServletContext());
ModuleConfig config = getModuleConfig(request);
RequestProcessor processor = getProcessorForModule(config);
if (processor == null) {
processor = getRequestProcessor(config);
}
processor.process(request, response);
}
三. 擴展ActionServlet類
從Struts1.1開始,為減輕ActionServlet的負(fù)擔(dān),多數(shù)功能已經(jīng)移到RequestProcessor類中,所以基本不用擴展ActionServlet類
如果需要創(chuàng)建自己的ActionServlet,則可以創(chuàng)建一個它的子類.覆蓋init()方法(或其他方法),可以寫一些自己的操作,但要先調(diào)用super.init();
定義如下的類:
package sample;
public class ExtendedActionServlet extends ActionServlet {
public void init() throws ServletException {
super.init();
……………
}
}
擴展完類后,還應(yīng)該在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/*表示負(fù)責(zé)處理所有以/action為前綴的URL,后面的/表示轉(zhuǎn)義
posted on 2009-04-05 11:10
lanxin1020 閱讀(193)
評論(0) 編輯 收藏 所屬分類:
struts1