TODO:
繼續(xù)測(cè)試和改進(jìn),不滿(mǎn)意的地方:@ToSession標(biāo)簽
更新日志:
2006-06-18?增加和改進(jìn) @ToList 標(biāo)簽,用來(lái)修飾一個(gè)參數(shù)為java.util.List<T>的setter方法?
2006-05-23 增加 VelocityView接口,方便velocity視圖調(diào)用.
2006-05-23 增加 WebParam接口,封裝掉request和response以及ServletContext
2006-05-22 增加 5.1 初始化 velocity
2006-05-21 增加 4.1 - 4.2 HttpSession操作
更新note:
1.HttpServletRequest的getParameterNames()?方法返回的Enumeration相對(duì)于頁(yè)面參數(shù)的物理順序剛好是相反的,所以實(shí)現(xiàn)@ToList標(biāo)簽時(shí)候需要先用一個(gè)Stack來(lái)對(duì)所有參數(shù)進(jìn)行反轉(zhuǎn).不知tomcat在這個(gè)api方法上是怎么實(shí)現(xiàn)的,為什么就倒過(guò)來(lái)。c語(yǔ)言函數(shù)可以根據(jù)不同的call類(lèi)型來(lái)指定不同的壓棧的順序,莫非這個(gè)慣例也被用到servlet里面來(lái)了,挺有意思的東西,看來(lái)需要留意一下。
問(wèn)題和解決方式:
1.關(guān)于@ToSession標(biāo)簽.如果是登陸的話(huà),那么就有個(gè)判斷條件決定某個(gè)login token是否應(yīng)該被加到session中,這時(shí)候可以在@ToSession標(biāo)記的getter方法中加入邏輯判斷,來(lái)決定返回一個(gè)token實(shí)體或者null(這樣做有點(diǎn)古怪..).
2.路徑的問(wèn)題.比如一個(gè)http://host/logic/bbs/admin/ListUsers.wff 摸版的相關(guān)資源目錄(比如,圖片,css,js等)就必須存在webproject/bbs/admin響應(yīng)的目錄下,有點(diǎn)像struts的多模塊.
開(kāi)始:
Ruby on Rails有個(gè)設(shè)計(jì)思想是用編碼規(guī)定代替繁瑣的配置文件。jvm平臺(tái)已經(jīng)有一些類(lèi)似ror的實(shí)現(xiàn),比如
grails(http://docs.codehaus.org/display/GRAILS/2006/03/29/Groovy+on+Rails+(Grails)+0.1+Released)
雖然由于java自身的局限,它很難做出像ruby或者groovy那樣動(dòng)態(tài)語(yǔ)言那樣隨心所欲的動(dòng)作,但是利用它的運(yùn)行時(shí)反射、動(dòng)態(tài)代理等特性來(lái)盡可能實(shí)現(xiàn)“用編碼規(guī)定代替繁瑣的配置文件”這一思想。
下面轉(zhuǎn)入正題。
ServletAPI對(duì)HTTP協(xié)議進(jìn)行了封裝,通過(guò)配置Web.xml來(lái)把不同的請(qǐng)求轉(zhuǎn)發(fā)給不同的servlet來(lái)處理。Web框架則用一個(gè)ActionServlet,根據(jù)自己的對(duì)Action的定義來(lái)轉(zhuǎn)發(fā)請(qǐng)求。
拋開(kāi)那些繁瑣的配置文件,設(shè)想一下這樣一種方法:
1.Web動(dòng)作的處理和響應(yīng)
假設(shè)這樣一個(gè)POST請(qǐng)求:
? <form action="logic/group/NewTopic.wff" method="post">
Web動(dòng)作實(shí)現(xiàn)Bean:
?? org.qqsns.web.logic.group.NewTopic??
注意后面的logic/group/NewTopic和logic.group.NewTopic, 動(dòng)作類(lèi)和Web動(dòng)作是通過(guò)請(qǐng)求路徑和包名相互關(guān)聯(lián)。
這樣,對(duì)Web動(dòng)作的響應(yīng)就依賴(lài)于編譯期的代碼的組織結(jié)構(gòu)而不是執(zhí)行期的配置文件。這樣的好處是避免了維護(hù)繁瑣的配置文件,特別是在沒(méi)有IDE支持的情況下。
org.qqsns.web.logic.group.NewTopic類(lèi)是一個(gè)實(shí)現(xiàn)net.wff.servlet.WebAction接口的POJO,下面是NewTopic中execute的方法片段:??
?
?//Only method must be implemented for interface net.wff.servlet.WebAction
?public String execute(WebParam param,?VelocityView view)
?throws ServletException, IOException{
??...
??//return "redirect /success.html";? //請(qǐng)求重定向
??return "/success.jsp";????????????? //請(qǐng)求轉(zhuǎn)發(fā)
}
execute方法的返回值手動(dòng)指定了一個(gè)轉(zhuǎn)發(fā)或重定向的路徑。
2.輸入驗(yàn)證
普通的Web框架都帶數(shù)據(jù)輸入驗(yàn)證功能,一般復(fù)雜程度和功能強(qiáng)大與否成正比。
這里簡(jiǎn)單地要求從setter方法里拋出一個(gè)包含驗(yàn)證信息的異常,以此來(lái)實(shí)現(xiàn)輸入異常處理。
??????
普通setter方法
public void setName(String name){?
??this.name = name;
}
添加輸入驗(yàn)證后的setter方法
public void setName(String name) throws InputException{???
?if(name.length()<3)
??? throw new InputException("Topic name must has a length greater than 3");?
??this.name = name;
}
在WaterFallServlet如何中處理驗(yàn)證信息:
??? WebAction wa =
??? (WebAction)Class.forName(classPath).newInstance();
????????? //procces forwarding
????????? try {
????ActionHelper.setProperties(request,wa);
???} catch (InputException e) {
????//return to input view
????//header:referer
????String rtn = request.getHeader("referer");
????//clear old errors
????if(rtn.indexOf("?")!=1){
?????rtn = rtn.substring(0,rtn.indexOf("?"));
????}
????rtn=rtn+"?error="+URLEncoder.encode(e.getMessage(),"UTF-8");
????response.sendRedirect(rtn);
????return;
???}
這樣驗(yàn)證信息通過(guò)請(qǐng)求參數(shù)傳回到輸入頁(yè)面.
3.數(shù)據(jù)綁定
假設(shè)有這樣的html輸入:
??? <input type="text" name="name"/>
??? <input type="text" name="number"/>
??? <input type="text" name="price"/>
???
?下面是NewTopic中execute的方法全部:??
?
?public String execute(WebParam param, VelocityView view)
?throws ServletException, IOException{
??System.out.println(getName());
??System.out.println(getNumber());
??System.out.println(getPrice());
??System.out.println(getLength());
??return "/success.html";
}
???
自動(dòng)從request注入parameter,這也是Struts DynamicActionForm的好處之一。
不過(guò)這里實(shí)現(xiàn)更類(lèi)似多了類(lèi)型轉(zhuǎn)換的<jsp:setProperty name="bean" property="*"/>
因?yàn)镹ame的類(lèi)型是String,Number的類(lèi)型是Integer,Price的類(lèi)型是float,length的類(lèi)型是double.至于其他復(fù)雜的類(lèi)型,也許jsf的轉(zhuǎn)換器是個(gè)更好的主意。
這樣就初步解決了數(shù)據(jù)的輸入綁定和驗(yàn)證。余下的就是業(yè)務(wù)邏輯的問(wèn)題。
4.如何操作Session中的信息
?
?獲取session中的信息:
? @FromSession(
???? sessionKey=User.key
???? )
?public void setCurrentUser(String currentUser) {
??this.currentUser = currentUser;
?}
?信息保存到session中:?
? @ToSession(
????? sessionKey = User.key??
??? )
?public String getName() {
??if(login())return name;
??else return null;
?}
?這2個(gè)方法定義在WebAction的實(shí)現(xiàn)類(lèi)中。
?框架在execute()之前執(zhí)行@FromSession動(dòng)作,在execute()之后執(zhí)行@ToSession動(dòng)作。
?這里使用了annotation,所以要求必須是jdk5.0以上版本.
?
?5.1 Velocity
? waterfall啟動(dòng)時(shí)候自動(dòng)在WEB-INF目錄下尋找并初始化Velocity.properties文件
?操作:
?public String execute(WebParam param,?VelocityView view)
?throws ServletException, IOException{
??view.addElement("msg","Hello");
? view.go("index.vm");
??return null;
}