Staplerisalibrarythat"staples"yourapplicationobjectstoURLs,makingiteasiertowritewebapplications.ThecoreideaofStapleristoautomaticallyassignURLsforyourobjects,creatinganintuitiveURLhierarchy.
Normally,mostofyourJSPandservletgoeslikethis;first,youuseparametersandextrapathinfotolocatetheobjecttoworkwith,thenusesomemoreparametersandsuchtoperformsomeoperationsagainstit(orperhapsrenderHTML).Staplertakescareofthisfirstportionbydispatchingrequeststothatobjectyou'llbeworkingwith.
Supposeyouarebuildingawebapplicationlikethesystemofjava.net,whereyougotprojects,mailinglists,documentsandfilessection,andetc.ThefollowingfigureshowshowStaplerworks.
Thelefthalfshowsyourcode.OOPletsyoumodelthoseconceptsstraight-forwardlyintoclassesthatrefertoeachother.Throughreflection,StaplerassignsURLstoyourapplicationobjects.TherootofyourapplicationobjectgetstherootURL /.Theobjectyoucanaccesswith root.getProject("stapler") wouldbeassignedtotheURL /project/stapler.Inthisway,yourobjectmodeldirectlyturnsintotheURLhierarchy,aspartiallyshowninpurplearrows.
JSPsandservletsMostofthetimesJSPsareusedtoshowanobjectinyourapplication.StaplerallowsyoutoassociateJSPstoyourapplicationclasses,asshowningreencirclesintheabovefigure.StaplerweavesthoseJSPsintotheURLhierarchy,andwhentheygetrequested,Staplersetsuptheenvironmentsuchthattheycanaccessthecorrespondingapplicationobjectveryeasily,bythe"it"variable(like${it.name}).
Similarly,oftenyouwanttorunsomeJavacodewhenacertainURLisrequested,asshowninredcirclesintheabovefigure.Forexample,when /project/stapler/delete isrequested,you'dliketodeletetheprojectandredirecttheuserto /.WithStapler,thisisaseasyasdefiningamethodonyourapplicationobjects.Staplerinvokesthemethodontherightobject.
ProblemsThatStaplerSolvesServlet(andtechnologieslikeJSP/JSFthatbuildsontopofit)canonlybindrequestURLsintoyourstaticresources,JSPs,and/orservletsinaverylimitedway.Youcaneventellthatfromthejava.netsystem.Forexample,theURLtoviewthearchiveofamailinglistis /servlets/SummarizeList?listName=announce,theURLtosearchthearchiveif /servlets/SearchList?listName=announce,andtheURLtomanageitis /servlets/MailingListEdit?list=announce.
Thishassomeproblems.
RelativeURLsareuselessOrdinaryservlet/JSPpreventsyoufromusingtheURLhierarchytoorganizeyourapplication.Inthejava.netsystem,ifyouwanttogeneratealinkfromthearchivetothesearchpage,youhavetoputthewholeURLincludingalltheparameters.You'llbewritingsomethinglike /servlets/SearchList?list=${listName}.Asyourapplicationgetsmorecomplicated,thenumberofparameterswillincrease,makingyourapplicationharderandhardertomaintain.
StaplerallowsyoutouseURLslike list/announce/summarize, list/announce/search,and list/announce/edit.Thusalinkfromthesummarypagetothesearchissimply search.
Alotofboiler-platecodeMostofthetimes,thefirstthingyourJSPsandservletsneedtodoistolocatetheobjectthatyou'llbeworkingwith.Thisistedious.Evenworse,itneedstoberepeatedinmanyplaces.
Staplertakescareofthisforyou.WhenyourJSPisinvoked,youcanaccessthetargetobjectbythevariable"it".Orwhenyouractionmethod(theequivalentoftheservlet)isinvoked,itisinvokedonthetargetobject.Thiscutsdownalotofboringboiler-platecodeinyourapplication.
RecursiveStructureServletscan'thandlerecursivedirectorystructureverywell.Intheabovefigure,Ihaverecursive Folder classthathastwoJSPseach.TheonlywaytomakethisworkinservletistoassignuniqueIDstoeachfolderanddefinethoseJSPsatthetop-level.TheactualURLswillbeverydifferentfromthenaturaltreestructure.
StaplermapsthisverynaturallytoURLs.
评论