BaoCao Spring Web MVC

  • View
    609

  • Download
    5

Embed Size (px)

Transcript

I HC KHOA HC T NHIN KHOA CNG NGH THNG TINCQ HK1 2k9 2k10

N TM HIU Mn Cng ngh Java cho h thng phn tn

Bo co tm hiu Spring MVC FramworkVersion 3.0

Sinh vin thc hin: 0612089 Nguyn c Linh Giang 0612119 Nguyn c Hong

Spring MVC Framwork Bo co tm hiu

Phin bn: 3.0 Ngy: 15/05/2010

Mc lc1 Gii thiu v Spring MVC Framework :1.1 1.2 Spring Framework : Spring MVC Framework : Cc thnh phn quan trng nht v chc nng ca mt Spring MVC framework: Lung x l request trong Spring MVC Framework :

22 3 3 4

1.2.1 1.2.2

2

ng dng minh ho c bn (Hello world):2.1 2.2 Mi trng trin khai ng dng : Cc bc thc hin : To mt Project Spring Web MVC bng NetBeans : Phn tch cc thnh phn trong Project : Pht trin mt ng dng n gin :

55 6 6 9 11

2.2.1 2.2.2 2.2.3

3

ng dng minh ho nng cao :3.1 3.2 Pht biu bi ton Cc bc xy dng: To CSDL v lin kt kho ngoi To Project p dng Spring Framework v Hibernate Pht sinh cc mapping file v pojos tng ng cho Hibernate Tao cc lp DAO v BUS To Controller cho chc nng thm sn phm mi : To Views : Config Controller : To Validation :

2222 22 22 28 32 39 42 46 49 52

3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8

4 5

Cc u im v khuyt im ca Spring MVC Web Framework : Ti liu tham kho :

56 56

FIT-HCMUS

Trang 1

Spring MVC Framwork Bo co tm hiu

Phin bn: 3.0 Ngy: 15/05/2010

1

Gii thiu v Spring MVC Framework :

1.1 Spring Framework :Spring l mt Framework m ngun m c phin bn dng cho Java Platform v c .NET Platform. Phin bn u tin c vit bi Rod Johnson v a ra cng cun sch Expert One-on-One J2EE Design and Development c xut bn thng 10 nm 2002. Spring Framework c th dng cho tt c cc ng dng vit bng Java, nhng n thnh cng nht trn lnh vc ng dng web trn nn Java EE. Spring Framework gm cc module chnh sau :

Inversion of Control container: hiu chnh cc components ca chng trnh v qun l vng i (lifecycle) ca cc i tng Java. Aspect-oriented programming: k thut lp trnh mi cho php ng gi nhng hnh vi c lin quan n nhiu lp Data access: lm vic vi relational database management systems (h thng qun l c s d liu quan h) trn nn Java platform s dng JDBC v cng c object-relational mapping. Transaction management: thng nht cc hm APIs qun l transaction v iu phi transactions cho i tng Java. Model-View-Controller (MVC) mt framework da trn HTTP v Servlet cung cp kh nng m rng v ty bin nhiu hn. Remote Access framework: hiu chnh RPC-style trong vic import v export cc i tng java thng qua mng li h tr phng thc RMI, CORBA v HTTP bao gm SOAP. Convention-over-configuration: mt chng trnh nhanh mnh trong vic pht trin cc hng gii quyt cho ci chng trnh s dng Spring enterprise. Batch processing: mt framework tt cho vic x l mt lng ln thng tin v hm nh logging/tracing, transaction management, job processing statistics, job restart, skip, and resource management. Authentication and authorization: hiu chnh ch an tan bo mt, cung cp cc phng thc, chng trnh mc c bn cho chng trnh s dng Spring. Remote Management: hiu chnh cch hin th v qun l cc i tng java mc local hoc remote qua JMX. Messaging: hiu chnh vic x l cc thong tin trao i gia cc i tng da trn chun JMS APIs. Testing: cung cp cc lp h tr vic vit cc unit kim tra v phn tch li.

Trong ti tm hiu ny. Ch quan tm v cp n module Model-View-Controller (MVC) trong Spring Framework. Ta gi n l Spring MVC Framework.

FIT-HCMUS

Trang 2

Spring MVC Framwork Bo co tm hiu

Phin bn: 3.0 Ngy: 15/05/2010

1.2 Spring MVC Framework :Tng t nh Struts, Spring MVC l mt request-based framework. H nh ngha cc phng php chung cho tt cc cc response phi c gii quyt bng mt request-based framework. Mc ch ca h n gin l d dng hn cho developer vit cc b sung v cc ci tin ca ring h. Spring MVC gip cho vic xy dng ng dng web mt cch cht ch v linh ng. Mu thit k ModelView-Controller gip phn chia rch ri 3 cng vic business logic, presentation logic, v navigation logic. Models chu trch nhim ng gi d liu ca ng dng. Views c nhim v hin th thng tin cung cp bi i tng Model tr v cho ngi dng. Controllers chu trch nhim nhn request t ngi dng v gi cc dch v bn di x l. 1.2.1

Cc thnh phn quan trng nht v chc nng ca mt Spring MVC framework: DispatcherServlet l mt lp ng ra qun l ton b cc hnh ng ca framework (front controller) trong sut qu trnh thc thi cc lnh thng qua HTTP request. HandlerMapping: chn mt i tng s x l cc request da trn cc thuc tnh v iu kin ca cc request . HandlerAdapter: thc thi cc handler c chn. Controller: ng gia Model v View qun l cc request c gi ti v chuyn cc response chnh xc.

View: chu trch nhim tr cc respone cho client. ViewResolver: chn phng php view da trn cc logical name c sn ca View. HandlerInterceptor: ngn chn (lc) cc request t user. N c coi nh Servlet filter (ko bt buc v ko b qun l bi DispatcherServlet). LocaleResolver: x l v lu mt phn cc thng tin ca user. MultipartResolver: lm cho vic upload file d dng hn bng cch gi cc request li.

FIT-HCMUS

Trang 3

Spring MVC Framwork Bo co tm hiu1.2.2 Lung x l request trong Spring MVC Framework :

Phin bn: 3.0 Ngy: 15/05/2010

Trch t http://www.vaannila.com/spring/spring-mvc-tutorial-1.html

Vic x l request v response trong Spring MVC Framework c m t nh sau : Bc 1 : o DispatcherServlet nhn Request. o DispatcherServlet tra trong HandlerMapping v gi Controller kt hp vi Request. Bc 2 : o Controller x l Request bng cch gi nhng phng thc dch v thch hp v sau tr v mt i tng ModelAndView cho DispatcherServlet. i tng ModelAndView ny cha d liu trong i tng Model v tn ca View. Bc 3 : o DispatcherServlet gi tn ca View n cho mt ViewResolver. ViewResolver s tm View thc s cn dng. Bc 4 : o DispatcherServlet truyn i tng Model n cho View xc nh hin th kt qu. o View ly d liu trong i tng Model v hin th kt qu cho ngi dng.

-

-

-

FIT-HCMUS

Trang 4

Spring MVC Framwork Bo co tm hiu

Phin bn: 3.0 Ngy: 15/05/2010

2

ng dng minh ho c bn (Hello world):

Sau y l mt v d HelloWorld n gin p dng Spring MVC Framework.

2.1 Mi trng trin khai ng dng :IDE : NetBeans IDE 6.8

-

Web Server : Apache Tomcat 6.0.20

FIT-HCMUS

Trang 5

Spring MVC Framwork Bo co tm hiu

Phin bn: 3.0 Ngy: 15/05/2010

2.2 Cc bc thc hin :2.2.1 To mt Project Spring Web MVC bng NetBeans :

1. Click File

2. Click New Project

3. Click Java Web 4. Select Web Application

5. Click Next

FIT-HCMUS

Trang 6

Spring MVC Framwork Bo co tm hiu

Phin bn: 3.0 Ngy: 15/05/2010

6. Nhp Project Name 7. Chn ni lu tr

8. Click Next

9. Chn Server l Apache Tomcat 6.0.20

10. Click Next

FIT-HCMUS

Trang 7

Spring MVC Framwork Bo co tm hiu11. Check chn Spring Web MVC 2.5

Phin bn: 3.0 Ngy: 15/05/2010

12. Click Finish

Kt qu ta c Project SpringMVCHelloWorld vi cu trc th mc nh sau :

FIT-HCMUS

Trang 8

Spring MVC Framwork Bo co tm hiuRun ng dng ta c c kt qu nh sau :

Phin bn: 3.0 Ngy: 15/05/2010

2.2.2

Phn tch cc thnh phn trong Project :

-

Trong file web.xml, ta thy c dng thit lp ch trang ch ca ng dng l redirect.jsp nh sau cui file :

-

Trong file redirect.jsp ch lm duy nht mt vic l redirect tt c nhng request gi n ti index.htm :

-

Cng trong file web.xml ta thy dng khai bo v nh x sau :Servlet dispatcher l lp k tha t org.springframework.web.servlet.DispatcherServlet

nh x tt c requests c mu URL khp *.htm cho lp DispatcherServlet ca Spring ny

FIT-HCMUS

Trang 9

Spring MVC Framwork Bo co tm hiu

Phin bn: 3.0 Ngy: 15/05/2010

-

Class DispatcherServlet qun l nhng request n ng dng da trn nhng thit lp cu hnh c tm thy trong file dispatcher-servlet.xml Gi ta xt n file dispatcher-servlet.xml , ta ch n on code sau :Khi DispatcherServlet nhn mt request khp *.htm nh l index.htm n s tm bn trong urlMapping ny mt controller cung cp cho request .

nh x index.htm vi indexController

viewResolver nhn vo Logical view name t indexController v tm trong th mc /WEB.INF/jsp/ trang jsp c tn tng ng.

indexController l lp k tha t org.springframework.web.servlet.mvc.ParameterizableViewController y l lp c cung cp bi Spring v n n gin s tr v 1 view.

Logical view name : Dng viewResolver x l

iu ny cho php nh v nhng file trong gi file WAR ca ng dng lc runtime v response vi trang jsp tng ng.

FIT-HCMUS

Trang 10

Spring MVC Framwork Bo co tm hiu2.2.3 Pht trin mt ng dng n gin :

Phin bn: 3.0 Ngy: 15/05/2010

2.2.3.1 Tng quan :

ng dng ny s bao gm 2 trang JSP hay cn gi l 2 View trong cng ngh Spring Web MVC. View th nht cha 1 form HTML vi mt field yu cu nhp tn ca ngi dng. View th 2 l mt trang n gin ch vic hin th : Hello + [tn ngi dng va nhp]

Nhng views ny s c qun l bi mt controller, controller ny s nhn request v quyt nh view no c tr v. N cng s truyn cho view c chn nhng thng tin cn thit view hin th, nhng thng tin ny c gi l Model. Ta s t tn controller ny l HelloController. Trong nhng ng dng web phc tp, nhng x l nghip v thuc tng Business Logic khng c cha trc tip trong controller m thay vo nhng thc th khc gi l service c dng bi controller khi n cn thc hin nhng nghip v ca tng Business Logic. Trong ng dng ny, nghip v cn x l l vic tnh ton v to ra cu thng ip hello, cho nn v mc ch ny ta to mt lp HelloService.2.2.3.2 Implementing the HelloService :

1. Phi chut here

2. Chn Java Class

FIT-HCMUS

Trang 11

Spring MVC Framwork Bo co tm hiu3. Nhp Class Name : HelloService

Phin bn: 3.0 Ngy: 15/05/2010

4. Nhp Package : service

5. Click Finish

Ta c lp HelloService c to :

FIT-HCMUS

Trang 12

Spring MVC