stapler HTTP请求处理引擎开源项目

我要开发同款
匿名用户2018年12月27日
42阅读
开发技术Java
所属分类其他
授权协议BSD

作品详情

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.

JSPsandservlets

MostofthetimesJSPsareusedtoshowanobjectinyourapplication.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.

ProblemsThatStaplerSolves

Servlet(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.

RelativeURLsareuseless

Ordinaryservlet/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-platecode

Mostofthetimes,thefirstthingyourJSPsandservletsneedtodoistolocatetheobjectthatyou'llbeworkingwith.Thisistedious.Evenworse,itneedstoberepeatedinmanyplaces.

Staplertakescareofthisforyou.WhenyourJSPisinvoked,youcanaccessthetargetobjectbythevariable"it".Orwhenyouractionmethod(theequivalentoftheservlet)isinvoked,itisinvokedonthetargetobject.Thiscutsdownalotofboringboiler-platecodeinyourapplication.

RecursiveStructure

Servletscan'thandlerecursivedirectorystructureverywell.Intheabovefigure,Ihaverecursive Folder classthathastwoJSPseach.TheonlywaytomakethisworkinservletistoassignuniqueIDstoeachfolderanddefinethoseJSPsatthetop-level.TheactualURLswillbeverydifferentfromthenaturaltreestructure.

StaplermapsthisverynaturallytoURLs.

声明:本文仅代表作者观点,不代表本站立场。如果侵犯到您的合法权益,请联系我们删除侵权资源!如果遇到资源链接失效,请您通过评论或工单的方式通知管理员。未经允许,不得转载,本站所有资源文章禁止商业使用运营!
下载安装【程序员客栈】APP
实时对接需求、及时收发消息、丰富的开放项目需求、随时随地查看项目状态

评论