Struts配置文件節點說明:
通過源代碼瞭解struts運行路徑:
1:解析請求路徑得到path
Path=request.getServletPath();
2:根據path得到一個ActionMapping
封裝一個action節點的所有信息,包括子節點信息,如:forwoard
3:得到formBean
a.在scope指向的範圍內尋找,名字以attribute爲準:
ActionForm actionForm = null;
HttpSesseion session = null ;
if(“request”.equals(scope)){
actionForm = (ActionForm)request.getAttribute(“attribute”);
}else{
Session = request.getSession();
actionForm = (ActionForm)session.getAttribute(“attribute”);
}
return actionForm;
b.如果找到返回,沒有找到創建,爲了效率把formBean放到scope所指向的範圍內;
if(“request”.equeals(mapping.getScope())){
request.setAttribute(mapping.getAttribute(),actionForm);
}else{
session = request.getSession();
session.setAttribute(mapping.getAttribute(),actionForm);
}
c.調用formBean的reset方法(重置),因爲此formBean可能是從request或session範圍中取出的,還保留上次業務邏輯的數據
d.根據請求參數給formBean賦值
a)Enumeration name =request.getParametersName();得到客戶端請求參數名的集合;
b)循環每個名稱並求parmeterValue= request.getParameterValues(“name”);
c)BeanUtils.populate(bean,properties);給formBean賦值。
4.得到ActionBean
Action action = null ;
Synchronized(action){
action = (Action)action.get(className);
if(action != null){
if(log.isTraceEnabled()){
log.trace(“returnint existint action instance”);
}
rturn action;
}
ation.setServlet(this.servlet);
actions.put(className,action);
}
5.調用actionBean的exectue方法,返回ActionForward對象
6.請求轉發或者重定向到指定的新頁面
formBean驗證用戶名和密碼不爲空(與js在客戶端驗證有別):重寫validate方法:
實體Bean作爲formBean的屬性
1.用struts時,當formBean中有多個字段,爲了減小冗餘 將用到類:BeanUtils,這樣就可以將一系列的set方法 刪掉了 如:
Struts-DispatchAction(爲了解決action數目過多的問題)
使用步驟:
1:
擴展RequestProcessor(類似於過濾器)
1. 繼承至RequestProcessor類,重寫prcessPreprocess方法(方法返回true則繼續運行(創 建actionMapping,formBean等…))
1. 配置一個controller節點(下面的配置(控制器、資源文件、插件)位置不能調換)