Spring Mvc Netbean

  • Published on
    08-Apr-2016

  • View
    513

  • Download
    5

Embed Size (px)

Transcript

  • I HC KHOA HC T NHIN KHOA CNG NGH THNG TIN

    CQ HK1 2k9 2k10

    N TM HIU

    Mn Cng ngh Java cho h thng phn tn

    Bo co tm hiu Spring MVC Framwork

    Version 3.0

    Sinh vin thc hin:

    0612089 Nguyn c Linh Giang

    0612119 Nguyn c Hong

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 1

    Mc lc

    1 Gii thiu v Spring MVC Framework : 2

    1.1 Spring Framework : 2

    1.2 Spring MVC Framework : 3

    1.2.1 Cc thnh phn quan trng nht v chc nng ca mt Spring MVC framework: 3

    1.2.2 Lung x l request trong Spring MVC Framework : 4

    2 ng dng minh ho c bn (Hello world): 5

    2.1 Mi trng trin khai ng dng : 5

    2.2 Cc bc thc hin : 6

    2.2.1 To mt Project Spring Web MVC bng NetBeans : 6

    2.2.2 Phn tch cc thnh phn trong Project : 9

    2.2.3 Pht trin mt ng dng n gin : 11

    3 ng dng minh ho nng cao : 22

    3.1 Pht biu bi ton 22

    3.2 Cc bc xy dng: 22

    3.2.1 To CSDL v lin kt kho ngoi 22

    3.2.2 To Project p dng Spring Framework v Hibernate 28

    3.2.3 Pht sinh cc mapping file v pojos tng ng cho Hibernate 32

    3.2.4 Tao cc lp DAO v BUS 39

    3.2.5 To Controller cho chc nng thm sn phm mi : 42

    3.2.6 To Views : 46

    3.2.7 Config Controller : 49

    3.2.8 To Validation : 52

    4 Cc u im v khuyt im ca Spring MVC Web Framework : 56

    5 Ti liu tham kho : 56

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 2

    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.

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 3

    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 Model-

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

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 4

    1.2.2 Lung x l request trong Spring MVC Framework :

    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.

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 5

    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

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 6

    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

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 7

    6. Nhp Project Name

    7. Chn ni lu tr

    8. Click Next

    10. Click Next

    9. Chn Server l

    Apache Tomcat 6.0.20

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 8

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

    11. Check chn

    Spring Web MVC 2.5

    12. Click Finish

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 9

    Run ng dng ta c c kt qu nh sau :

    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

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 10

    - 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 :

    iu ny cho php nh v nhng file trong gi file WAR ca ng dng lc runtime v

    response vi trang jsp tng ng.

    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

    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

    viewResolver nhn vo Logical view name t indexController v

    tm trong th mc /WEB.INF/jsp/ trang jsp c tn tng ng.

  • Spring MVC Framwork Phin bn: 3.0

    Bo co tm hiu Ngy: 15/05/2010

    FIT-HCMUS Trang 11

    2.2.3 Pht trin mt ng dng n gin :

    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