Servlet的演變:在常規(guī)的 JSP,Servlet,JavaBean三層結(jié)構(gòu)中,JSP實(shí)現(xiàn)View的功能,Servlet實(shí)現(xiàn)Controller的功能,JavaBean實(shí)現(xiàn)Model的實(shí)現(xiàn)。
在Struts中,將常規(guī)情況下的Servlet拆分與ActionServlet、FormBean、ActionBean三個(gè)部分。ActionServlet配合Struts-config.xml,專職完成頁面導(dǎo)航,而不再負(fù)責(zé)具體的數(shù)據(jù)獲取與相應(yīng)邏輯,這兩部分功能由FormBean和ActionBean來完成。
Struts的核心是Controller,即ActionServlet,而ActionServlet的核心就是Struts-config.xml,Struts-config.xml集中了所有頁面的導(dǎo)航定義。對于大型的WEB項(xiàng)目,通過此配置文件即可迅速把握其脈絡(luò),這不管是對于前期的開發(fā),還是后期的維護(hù)或升級都是大有裨益的。掌握Struts-config.xml是掌握Struts的關(guān)鍵所在。
<struts-config>
<data-sources />
<form-beans >
<form-bean name="systemForm" type="com.gdglc.survey.form.SystemForm" />
<form-bean name="FindMemberForm" type="org.apache.struts.validator.DynaValidatorForm">
<form-property name="findName" type="java.lang.String"/>
</form-bean>
</form-beans>
<global-exceptions />
<global-forwards>
<forward name="showMsg" path="/msg.jsp" />
</global-forwards>
<action-mappings>
<action
path="/list"
type="com.gdglc.survey.action.ListAction">
<forward name="list" path="/list.jsp"/>
</action>
<action
attribute="systemForm"
input="/admin/System_Base_List.jsp"
name="systemForm"
path="/systemBaseModify"
type="com.gdglc.survey.action.SystemAction"
scope="request"
validate="false">
</action>
</action-mappings>
<message-resources parameter="com.gdglc.application" />
<plug-in className="org.apache.struts.validator.ValidatorPlugIn">
<set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/>
</plug-in>
</struts-config>
每一個(gè)FormBean 都必須繼承ActionForm類,F(xiàn)ormBean是對頁面請求的封裝。即把HTTP request 封裝在一個(gè)對象中,需要說明的一點(diǎn)就是多個(gè)HTTP request可以共用一個(gè)FormBean,便于維護(hù)和重用。
public
final class RegUserForm extends
ActionForm{
private String logname;
private String password;
private String email;
public RegUserForm(){
logname = null;
password = null;
email = null;
}
......
public void reset(ActionMapping mapping, HttpServletRequest request)
{
logname = null;
password = null;
email = null;
}
public ActionErrors validate(ActionMapping mapping,HttpServletRequest request) {
ActionErrors errors=new ActionErrors();
if(this.password==null)
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("password is null!!"));
......
return errors;
}
}
FormBean的產(chǎn)生是為了提供數(shù)據(jù)給ActionBean,在ActionBean中可以取得FormBean中封裝的數(shù)據(jù),經(jīng)相應(yīng)的邏輯處理后,調(diào)用業(yè)務(wù)方法完成相應(yīng)業(yè)務(wù)要求。
public
final class RegUserAction extends
Action{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) {
......
if(true)
return mapping.findForwad("list"); else {
ActionErrors errors=new ActionErrors();
errors.add(ActionErrors.GLOBAL_ERROR,new ActionError("...."));
this.saveErrors(request,errors);
return new ActionForward(mapping.getInput()); }
}
}
Struts優(yōu)缺點(diǎn)
優(yōu)點(diǎn):Struts跟Tomcat、Turbine等諸多Apache項(xiàng)目一樣,是開源軟件,這是它的一大優(yōu)點(diǎn)。使開發(fā)者能更深入的了解其內(nèi)部實(shí)現(xiàn)機(jī)制。
除此之外,
Struts的優(yōu)點(diǎn)主要集中體現(xiàn)在兩個(gè)方面:Taglib和頁面導(dǎo)航。Taglib是Struts的標(biāo)記庫,靈活動(dòng)用,能大大提高開發(fā)效率。另外,就目前國內(nèi)的JSP開發(fā)者而言,除了使用JSP自帶的常用標(biāo)記外,很少開發(fā)自己的標(biāo)記,或許Struts是一個(gè)很好的起點(diǎn)。
關(guān)于頁面導(dǎo)航,我認(rèn)為那將是今后的一個(gè)發(fā)展方向,事實(shí)上,這樣做,使系統(tǒng)的脈絡(luò)更加清晰。通過一個(gè)配置文件,即可把握整個(gè)系統(tǒng)各部分之間的聯(lián)系,這對于后期的維護(hù)有著莫大的好處。尤其是當(dāng)另一批開發(fā)者接手這個(gè)項(xiàng)目時(shí),這種優(yōu)勢體現(xiàn)得更加明顯。
缺點(diǎn):Taglib是Struts的一大優(yōu)勢,但對于初學(xué)者而言,卻需要一個(gè)持續(xù)學(xué)習(xí)的過程,甚至還會(huì)打亂你網(wǎng)頁編寫的習(xí)慣,但是,當(dāng)你習(xí)慣了它時(shí),你會(huì)覺得它真的很棒。
Struts將MVC的Controller一分為三,在獲得結(jié)構(gòu)更加清晰的同時(shí),也增加了系統(tǒng)的復(fù)雜度。
Struts從產(chǎn)生到現(xiàn)在還不到半年,但已逐步越來越多運(yùn)用于商業(yè)軟件。雖然它現(xiàn)在還有不少缺點(diǎn),但它是一種非常優(yōu)秀的J2EE MVC實(shí)現(xiàn)方式,如果你的系統(tǒng)準(zhǔn)備采用J2EE MVC架構(gòu),那么,不妨考慮一下Struts。
Struts實(shí)施經(jīng)驗(yàn):
1)、基于Struts架構(gòu)的項(xiàng)目開發(fā),首先需要有一個(gè)很好的整體規(guī)劃,整個(gè)系統(tǒng)中包括哪幾個(gè)模塊,每個(gè)模塊各需要多少FormBean和ActionBean等,而且最好有專人負(fù)責(zé)Struts-config.xml的管理。開發(fā)基于Struts的項(xiàng)目的難點(diǎn)在于配置管理,尤其是對Struts-config.xml的管理。
2)、如果你的項(xiàng)目非常緊,并且項(xiàng)目組中又沒有富有經(jīng)驗(yàn)的Struts開發(fā)人員,建議不要冒然采用Struts。Struts的掌握需要一個(gè)過程,對于一個(gè)熟練的JSP程序員,自學(xué)大概需要半個(gè)月左右的時(shí)間。如果結(jié)合titls,則需要更長的時(shí)間。
3)、如果你在網(wǎng)頁中大量運(yùn)用taglib,那么你的美工將做出部分犧牲。當(dāng)你結(jié)合Tiles,功能增強(qiáng)的同時(shí),這種犧牲尤為明顯。當(dāng)然,你對功能和美觀的取舍由你自己決定。
4)、Taglib是一個(gè)好東西,但靈活運(yùn)用它卻需要一個(gè)過程,如果你不想在Taglib上花太多的時(shí)間,那么只需理解與FORM有關(guān)的幾個(gè)標(biāo)記,其它的標(biāo)記就放著吧,以后再看,先去研究ActionServlet和Struts-config.xml,你會(huì)覺得很有成就感。
5)、Struts是否只適合于大型項(xiàng)目呢?No!Struts適合于各種大小的項(xiàng)目,當(dāng)然,對于大型項(xiàng)目,它所體現(xiàn)出來的優(yōu)勢更加明顯。
http://www.chinaitlab.com/www/news/article_show.asp?id=33092
posted on 2005-12-15 13:33
kelven 閱讀(838)
評論(0) 編輯 收藏 所屬分類:
Struts