Full Spring MVC

  • Published on
    06-Apr-2015

  • View
    1.059

  • Download
    4

Embed Size (px)

Transcript

Spring MVC1. Gii thiu Spring's Web MVC framework c thit k xung quanh mt DispatcherServlet t,n gi cc request n cc handler, vi vic cho php cu hnh cc handler mapping, view resolution, locale and theme resolution nh l s h tr tt nht cho vic upload file. Hanler mc nh rt n gin Controller interface, ch a ra mt phng thc ModelAndView handleRequest(request,response). Ci ny c th c s dng cho cc controller ca ng dng, nhng s thch hn khi bao gm kin trc thc thi c th bc, s nht qun, v d AbstractController, AbstractCommandController and SimpleFormController. Cc controller ca ng dng s l cc lp con tiu biu ca chng . Ch rng chng ta c th chn mt lp c s nu chng ta khng c mt form, chng ta khng cn mt form controller. y l iu khc bit chnh so vi Strusts. Spring Web MVC cho php chng ta s dng vi i tng nh l mt lnh hoc i tng form khng cn implement mt framework-specific interface hoc base class. Spring's data binding c tnh mm do cao: v d, n i x cc kiu khng hp nh l cc validation error, iu ny c th c c lng bi ng dng, khng nh cc system error. Vi tt c nhng iu ny c ngha l chng ta khng cn sao chp cc property ca i tng bussiness, khng cn phi g cc chui vo trong form i tng ch x l yu cu khng ph hp, hoc chuyn i thnh cc chui. Thay vo n thng a ra s lin kt trc tip n i tng business ca chng ta. y l im khc bit chnh so vi Struts, Strust c xy dng xung quanh yu cu cc lp c s nh l Action v ActionForm. So snh vi WebWork , spring c thm cc vai tr khc ca i tng. N h tr th s thng bo ca mt controller, mt lnh ty chn hoc mt i tng form, v mt m hnh thng n view. M hnh s n gin bao gm lnh v i tng form nhng cng c d liu lin quan ty ; Thay vo , mt WebWork Action kt hp tt c nhng vai tr vo trong mt i tng ring r. WebWork cho pho bn s dng nhng i tng business ca form, nhng ch lm cc thuc tnh bean cho cc lp Action tng ng. Cui cng, nhng th hin Action ging nhau, s x l request ca n c s dng cho vic nh gi v xa nh v tr ca form trong View. Nh vy, d liu lin quan cng cn c m hnh nh cc thuc tnh bean ca Action. Vi nhng th nh vy, ngi ta c th cho rng c qu nhiu vai tr cho mt i tng. Gii php view ca Spring mm do v cng. Mt s thc thi ca mt Controller c th thm chi c th ghi mt view trc tip vo response ( bng cch tr v gi tr null null cho ModelAndView). Trong trng hp n gin, mt th hin ModelAndView bao gm mt tn ca view v mt m hnh nh x(Map), n cha ng tn bean v nhng i tng tng ng (going mt lnh hoc mt form, cha ng d liu lin quan). Gii php dng tn view c th c cu hnh tt, hoc thng qua tn bean, mt file properties, hoc qua thng ViewResolver implementation ca bn. Tht s, m hnh (Min MVC) c da trn Map interface, n cho php cc cng ngh view hon ton tru tng. Mt vi renderer c th c tch hp trc tip, nh l JSP, Velocity. M hnh Map c chuyn i mt cch n gin thnh mt nh dng thch hp, nh l cc thuc tnh JSP request hoc mt mu Velocity. 1.1 Pluggability of other MVC implementations C mt vi l do ti sao vi d n thch s dng nhng MVC implement khc. Nhiu nhm Nhiu nhm pht trin mun nng cp nhng hng tng hin ti gm cc k nng v cng c. Hn na, c mt lng ln ngi hiu bit v c kinh nghim v Strusts framework. Tuy nhin, nu bn quen vi kin trc Struts, n vn c th l mt la chn vng chc cho web layer; cng mt kiu p dng cho WebWork v nhng web MVC framework khc. Nu bn khng mn s dng Spring's web MVC, nhng c nh s dng nhng gii php khc m Spring ngh, bn c th tch hp s la chn web MVC framework ca bn vi Spring mt cch d dng. n gin l khi ng mt Spring root application context thng qua ContextLoaderListener ca n, v truy cp n thng qua thuc tnh ServletContext ca n (or hoc phng thc helper ca Spring tng ng) t bn trong mt Struts hoc WebWork action. Ch , khng c mt vi "plugins" c gi, v th khng c s tch hp c tnh chuyn mn no l cn thit. T im nhn ca web layer, bn s s dng mt cch n gin Spring nh l mt th vin, vi th hin root application context nh l mt entry point(li vo). Tt c nhng bean bn ng k v cc service ca Spring c th l cc u ngn tay ca bn thm ch khng c Spring MVC. Spring khng cnh tranh vi Struts or WebWork trong trng hp ny, n ch xc nh a ch nhiu khu vc ni m s trong sch ca web MVC frameworks khng lm, T s cu hnh bean n truy cp d lieu v giao tc, x l giao tc. V th bn c th lm giu ng dng ca mnh vi Spring middle tier hoc/v data access tier, thm ch nu bn ch mun dng, v d nh, transaction abstraction vi JDBC hoc Hibernate.

1.2 Features of Spring Web MVC Spring WebFlow Mc ch Spring Web Flow (SWF) l tm gii php tt nht cho vic qun l lung ca trang ng dng web ( web application page flow). SWF tch hp vi cc framework ang tn ti nh Spring MVC, Struts, v JSF, trong c mi trng. Nu bn c mt business process (hoc processes), n c t khi m mt m hnh giao tip nh l b i nghch thnh mt m hnh request mt cch r rng, SWF c th l mt gii php. SWF cho php bn bt cc lung logic ca page nh self-contained modules m n c s dng li trong cc hon cnh khc, v nh l tng cho vic xy dng cc module ca ng dng web, n hng dn ngi dng xuyn sut iu khin cc navigation, nhng navigation ny iu khin business processes. Tham kho thm v SWF ti trang Spring WebFlow site. Cc module ca Spring web cung cp mt s phong ph cc feature duy nht cho web, bao gm:

Phn chia r rng cc vai tr ca controller (roles controller), validator, command object, form object, model object, DispatcherServlet, handler mapping, view resolver, etc. Mi vai tr c th c tha mn y bi mt i tng chuyn dng.. Mnh m v s cu hnh khng phc tp ca c framework v cc lp ng dng (application classes) nh cc JavaBean, bao gm d dng tham kho (referencing) thng qua cc context, Chng hn nh t cc web controller n cc i tng business (business object) v validators. Kh nng thch ng, khng xm phm (non-intrusiveness). S dng bt c controller subclass g bn cn (plain, command, form, wizard, multi-action, hoc mt custom controller) cung cp mt kch bn thay vo thu c t mt controller n c cho mi th. Nhng business code c th c s dng li - khng cn phi sao chp. Bn c th s dng nhng business object nh l lnh (command) hoc form object thay v phn nh chng theo m rng mt base class framework c bit. Binding v validation c th chnh sa c nhng s khng ph hp kiu (type mismatches) nh l application-level validation errors, n gi nhng gi tr li, localized date v number bindingv.v Thay v ch l String form objects vi s phn tcch v chuyn i thnh business object bng tay. Handler mapping c th ty chnh v s gii quyt bng view Cc chin lc handler mapping v s gii quyt bng view nhm sp xp URL-based n gin thnh phc tp, cc chin lc cho mc ch xy dng. iu ny lm cho n mm do hn nhng web MVC framework khch This is more flexible than some web MVC frameworks. Tnh linh ng ca model transfer - model transfer thng qua tn/gi tr Map h tr cho vic d dng tch hp vi mt vi cng ngh view khc. Locale c th chnh sa v s gii quyt bng theme, h tr cho JSP vi th vin th ca Spring hoc khng, h tr cho JSTL, h tr cho Velocity khng cn thit phi c mt cy cu bc qua cng ngh ny, v.v. Mt th vin tag JSP cn mnh m c bit nh l th vin tag Spring, n cung cp s h tr cho cc tnh nng nh l data binding v themes. Cc tag t do c cho php ti a trong gii hn m nh du(markup). Tm hiu thm tag th vin tag ti Appendix D, spring.tld Mt th vin tag JSP form c gii thiu trong Spring 2.0, n lm cho vic son tho cc form trong trang JSP d dng hn. Tm hiu thm ti Appendix E, spring-form.tld Lifecycle ca lifecycle is c xc nh trong HTTP request hin ti hoc HTTP Session. y khng phi l c tnh ca Spring MVC, nhng thch WebApplicationContext container(s) hn, Spring MVC s dng. Tm hiu thm ti Section 3.4.4, The other scopes

DispatcherServlet Spring's web MVC framework ging nh nhiu web MVC frameworks, request-driven, c thit k xung quanh mt servlet trung tm, n gi cc request n cc controller v a ra nhng chc nng d dng cho s pht trin ca ng dng web. Spring DispatcherServlet bt c nh th no , lm nhiu hn l ch c vy. N hon ton c tch hp vi Spring IoC container v nh vy cho php bn s dng bt c c tnh no m Spring c.

Lung x l request ca Spring Web MVC DispatcherServlet c minh ha trong lc bn di. Ngi c c hiu bit v m hnh s nhn thy rng that the DispatcherServlet l mt s din t ca mu thit k Front Controller (y l mt mu m Spring Web MVC chia s vi nhiu web frameworks dn u khc ).

DispatcherServlet l mt Servlet tht (n k tha t lp c s HttpServlet ), v nh vy c khai bo trong web.xml ca ng dng web ca bn. Nhng request m bn mun DispatcherServlet x l s c nh x, s dng mt URL mapping trong cng file web.xml. y l chun cu hnh ca J2EE servlet; mt v d nh l mt s khai bo ca DispatcherServlet v mapping c th c tm thy bn di. example org.springframework.web.servlet.DispatcherServlet 1 example *.form Trong v d trn, tt c cc request kt thc vi .form s c x l bi 'example' DispatcherServlet. y ch l bc khi u cu hnh trong s thit lp Spring Web MVC...Nhng bean khc nhau c s dng bi Spring Web MVC framework (trn v DispatcherServlet trn ca chnh n) cu c cu hnh ngay. Nh c din t chi tit trong phn Section 3.8, The ApplicationContext, nhng th hin ApplicationContext trong Spring c th c xc nh phm vi. Trong web MVC framework, mi DispatcherServlet c WebApplicationContext ca chnh n, n k tha tt c cc bean c nh ngha trong root WebApplicationContext. Nhng th ny c k tha t cc bean c nh ngha sn, cc bean ny c th b overridden trong phm vi servlet r rng, v mt phm vi r rng ca cc bean c th c nh ngha sn local cho th hin ca servlet .

H thng ng cnh Spring Web MVC Framework s, trn s khi to ca mt DispatcherServlet, tm kim mt tn file [servlet-name]-servlet.xml trong th mc WEBINF ca ng dng web ca bn v to cc bean c nh ngha (overriding nhng s nh ngha ca mt vi bean c nh ngha cng tn trong phm vi global). Cn nhc cc DispatcherServlet servlet sau (trong file 'web.xml) ... golfing org.springframework.web.servlet.DispatcherServlet 1 golfing *.do Vi v tr trong s cu hnh servlet trn, bn s cn c mt file gi l '/WEB-INF/golfing-servlet.xml' trong ng dng ca bn; file ny s cha ng tt c cc m t ca cc component(bean) ca Spring Web MVC. V tr chnh xc ca cc file cu hnh c th b thay i qua mt tham s khi to ca servlet ( Xem bn di r hn). WebApplicationContext l mt s m rng ca plain ApplicationContext, n c mt vi tnh nng ph cn thit cho cc web. N khc vi mt ApplicationContext bnh thng, n c kh nng gii quyt cc theme (xem Section 13.7, Using themes), v rng n bi