BaoCao Spring Web MVC

  • Published on
    25-Jul-2015

  • View
    609

  • Download
    5

Embed Size (px)

Transcript

<p>I HC KHOA HC T NHIN KHOA CNG NGH THNG TINCQ HK1 2k9 2k10</p> <p> N TM HIU Mn Cng ngh Java cho h thng phn tn</p> <p>Bo co tm hiu Spring MVC FramworkVersion 3.0</p> <p>Sinh vin thc hin: 0612089 Nguyn c Linh Giang 0612119 Nguyn c Hong</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>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 :</p> <p>22 3 3 4</p> <p>1.2.1 1.2.2</p> <p>2</p> <p>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 :</p> <p>55 6 6 9 11</p> <p>2.2.1 2.2.2 2.2.3</p> <p>3</p> <p>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 :</p> <p>2222 22 22 28 32 39 42 46 49 52</p> <p>3.2.1 3.2.2 3.2.3 3.2.4 3.2.5 3.2.6 3.2.7 3.2.8</p> <p>4 5</p> <p>Cc u im v khuyt im ca Spring MVC Web Framework : Ti liu tham kho :</p> <p>56 56</p> <p>FIT-HCMUS</p> <p>Trang 1</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>1</p> <p>Gii thiu v Spring MVC Framework :</p> <p>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 : </p> <p>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.</p> <p>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.</p> <p>FIT-HCMUS</p> <p>Trang 2</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>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</p> <p>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.</p> <p>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.</p> <p>FIT-HCMUS</p> <p>Trang 3</p> <p>Spring MVC Framwork Bo co tm hiu1.2.2 Lung x l request trong Spring MVC Framework :</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>Trch t http://www.vaannila.com/spring/spring-mvc-tutorial-1.html</p> <p>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.</p> <p>-</p> <p>-</p> <p>-</p> <p>FIT-HCMUS</p> <p>Trang 4</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>2</p> <p>ng dng minh ho c bn (Hello world):</p> <p>Sau y l mt v d HelloWorld n gin p dng Spring MVC Framework.</p> <p>2.1 Mi trng trin khai ng dng :IDE : NetBeans IDE 6.8</p> <p>-</p> <p>Web Server : Apache Tomcat 6.0.20</p> <p>FIT-HCMUS</p> <p>Trang 5</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>2.2 Cc bc thc hin :2.2.1 To mt Project Spring Web MVC bng NetBeans :</p> <p>1. Click File</p> <p>2. Click New Project</p> <p>3. Click Java Web 4. Select Web Application</p> <p>5. Click Next</p> <p>FIT-HCMUS</p> <p>Trang 6</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>6. Nhp Project Name 7. Chn ni lu tr</p> <p>8. Click Next</p> <p>9. Chn Server l Apache Tomcat 6.0.20</p> <p>10. Click Next</p> <p>FIT-HCMUS</p> <p>Trang 7</p> <p>Spring MVC Framwork Bo co tm hiu11. Check chn Spring Web MVC 2.5</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>12. Click Finish</p> <p>Kt qu ta c Project SpringMVCHelloWorld vi cu trc th mc nh sau :</p> <p>FIT-HCMUS</p> <p>Trang 8</p> <p>Spring MVC Framwork Bo co tm hiuRun ng dng ta c c kt qu nh sau :</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>2.2.2</p> <p>Phn tch cc thnh phn trong Project :</p> <p>-</p> <p>Trong file web.xml, ta thy c dng thit lp ch trang ch ca ng dng l redirect.jsp nh sau cui file :</p> <p>-</p> <p>Trong file redirect.jsp ch lm duy nht mt vic l redirect tt c nhng request gi n ti index.htm :</p> <p>-</p> <p>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</p> <p>nh x tt c requests c mu URL khp *.htm cho lp DispatcherServlet ca Spring ny</p> <p>FIT-HCMUS</p> <p>Trang 9</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>-</p> <p>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 .</p> <p>nh x index.htm vi indexController</p> <p>viewResolver nhn vo Logical view name t indexController v tm trong th mc /WEB.INF/jsp/ trang jsp c tn tng ng.</p> <p>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.</p> <p>Logical view name : Dng viewResolver x l</p> <p> iu ny cho php nh v nhng file trong gi file WAR ca ng dng lc runtime v response vi trang jsp tng ng.</p> <p>FIT-HCMUS</p> <p>Trang 10</p> <p>Spring MVC Framwork Bo co tm hiu2.2.3 Pht trin mt ng dng n gin :</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>2.2.3.1 Tng quan :</p> <p>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]</p> <p>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 :</p> <p>1. Phi chut here</p> <p>2. Chn Java Class</p> <p>FIT-HCMUS</p> <p>Trang 11</p> <p>Spring MVC Framwork Bo co tm hiu3. Nhp Class Name : HelloService</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>4. Nhp Package : service</p> <p>5. Click Finish</p> <p>Ta c lp HelloService c to :</p> <p>FIT-HCMUS</p> <p>Trang 12</p> <p>Spring MVC Framwork Bo co tm hiuThm vo lp ny phng thc sayHello nh sau :</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>2.2.3.3 Implementing the Controller and Model y ta c th dng mt SimpleFormController qun l d liu ngi dng v xc nh view no c tr v.1. Click File 2. Click New File</p> <p>4. Click Simple Form Controller 3. Click Spring Framework</p> <p>5. Click Next</p> <p>FIT-HCMUS</p> <p>Trang 13</p> <p>Spring MVC Framwork Bo co tm hiu6. Nhp Class Name : HelloController</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>7. Nhp Package : controller</p> <p>8. Click Finish</p> <p>Kt qu ta to c lp HelloController :</p> <p>Ta edit li constructor ca lp HelloController nh sau :</p> <p>FIT-HCMUS</p> <p>Trang 14</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>Mt instance ca lp ny s c to vi mi request. t tn view c dng hin th thng ip Hello khi submit thnh cng t tn view c dng hin th form nhp tn</p> <p>nh ngha tn ca command trong model</p> <p>By gi ta cn to class Name nh mt bean n gin gi thng tin ca mi request. Tng t nh cch to lp trn. Phi chut vo project chn New =&gt; Java Class To mt lp tn l Name trong package pojo.</p> <p>Trong lp Name ny ta to mt bin private value lu tn ngi dng nhp vo. Sau l cc phng thc get set. Nh sau :</p> <p>Quay tr li class HelloController, ta xo phng thc doSubmitAction() i v b comment phng thc onSubmit() bn di. Phng thc onSubmit ny cho php ta t to ModelAndView. Import org.springframework.web.servlet.ModelAndView. Lp ModelAndView c tr v bi mt controller v c x l bi mt DispatcherServlet. View c th c ly da vo chui View Name m s c x l bi mt i tng ViewResolver, hoc i tng View c th c ch nh trc tip. Thm mt bin mc lp kiu HelloService cho HelloController. Import service.HelloService v thm nhng dng sau vo lp HelloController :</p> <p>-</p> <p>FIT-HCMUS</p> <p>Trang 15</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>-</p> <p>Edit li phng thc onSubmit() nh sau :</p> <p>Vo file applicationContext.xml ng k HelloService. ang k ta thm dng code sau :</p> <p>FIT-HCMUS</p> <p>Trang 16</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>Sau thc hin ng k HelloController trong file dispatcher-servlet.xml :</p> <p>V ng k mapping controller ca chung ta vi url mong mun c x l :</p> <p>FIT-HCMUS</p> <p>Trang 17</p> <p>Spring MVC Framwork Bo co tm hiu2.2.3.4 Implementing the Views : Ta cn to 2 lp JSP : -</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>Ci th nht gi l nameView.jsp hin th trang welcome v cho php ngi dng nhp tn. Ci th hai gi l helloView.jsp hin th li cho cng vi tn ca ngi dng va nhp.2. Click JSP 1. Phi chut here</p> <p>3. Nhp File Name : helloView</p> <p>4. Click Finish</p> <p>FIT-HCMUS</p> <p>Trang 18</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>Edit li file helloView.jsp nh sau :</p> <p>Tng t ta to mt trang JSP khc tn nameView.jsp tng t nh trn.</p> <p>FIT-HCMUS</p> <p>Trang 19</p> <p>Spring MVC Framwork Bo co tm hiuThay i ni dung file ny nh sau :</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>Th vin cha nhng tags hu dng khi implement views</p> <p>Cho php ch nh mt bean</p> <p>Cho php kt buc vi mt thuc tnh ca mt bean</p> <p>Tag spring:nestedPath ch nh mt bean v tag spring:bind kt buc tham s vi mt thuc tnh. ng dn n bean ny tr thnh name.value. Nh ci t t trc, tn command ca HelloController l name. V vy ng dn ny tham chiu n thuc tnh value ca bean tn l name trong phm vi trang. Cui cng m file redirect.jsp v i index.htm thnh hello.htm.</p> <p>Sau Run ng dng :</p> <p>FIT-HCMUS</p> <p>Trang 20</p> <p>Spring MVC Framwork Bo co tm hiuClick OK :</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>FIT-HCMUS</p> <p>Trang 21</p> <p>Spring MVC Framwork Bo co tm hiu</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>3</p> <p>ng dng minh ho nng cao :</p> <p>Sau khi tri qua ng dng HelloWorld v hiu khi qut v cc bc cn thit cu hnh cho ng dng s dng Spring MVC Framework. Sau y l mt v d nng cao hn v gn vi thc t hn. ng dng s c y kin trc ca mt phn mm nn c. D liu ca ng dng c lu tr trong c s d liu.</p> <p>3.1 Pht biu bi tonXy dng chc nng thm thm sn phm mi cho mt ng dng web thng mi in t. lu tr d liu cho chc nng ny ta cn 2 bng DanhMuc v SanPham vi quan h kho ngoi nh hnh di</p> <p>ng dng c xy dng theo kin trc 3 tng dng Spring MVC tng giao din (presentation) v Hibernate tng truy xut d liu.</p> <p>3.2 Cc bc xy dng:3.2.1 To CSDL v lin kt kho ngoi</p> <p>3.2.1.1 Mi trng to lp : Trong ng dng ny ta s dng H qun tr CSDL MySQL cung cp bi XAMPP Control Panel :</p> <p>C th download ti url sau : http://www.apachefriends.org/en/xampp-windows.html</p> <p>FIT-HCMUS</p> <p>Trang 22</p> <p>Spring MVC Framwork Bo co tm hiu3.2.1.2 Cc bc thc hin Sau khi ci t xong XAMPP v Navicat by gi ta bt u cng vic chnh -</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>Khi ng XAMPP Control Panel : Start &gt;&gt; All Programs &gt;&gt; XAMPP for Windows &gt;&gt; XAMPP Control Panel</p> <p>-</p> <p>Ca s sau hin th :</p> <p>1. Click Start</p> <p>-</p> <p>Kt qu nh sau c ngha l H qun tr MySQL c khi ng v sn sng s dng. Sau y ta s m trang cu hnh MySQL to CSDL.</p> <p>2. Click Apache</p> <p>3. Click Apache</p> <p>FIT-HCMUS</p> <p>Trang 23</p> <p>Spring MVC Framwork Bo co tm hiuTrang sau c hin th :4. Nhp tn CSDL mun to</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>6. Chn li l utf8_unicode_ci</p> <p>5. Chn li l utf8_unicode_ci</p> <p>Nh sau :7. Click Create</p> <p>-</p> <p>Kt qu ta to thnh cng CSDL mi vi tn l mobiledb :</p> <p>FIT-HCMUS</p> <p>Trang 24</p> <p>Spring MVC Framwork Bo co tm hiu-</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>Gi ta to 2 bng DanhMuc (MaDM, TenDM) v SanPham(MaSP, TenSP, SoLuong, DonGia, MaDM) :8. Click Go to create</p> <p>K tip ta in cc thng tin thuc tnh ca bng nh sau :</p> <p>9. Fill it</p> <p>10. Chn l InnoDB mi c th thc hin to kho ngoi 11. Click Save</p> <p>Kt qu nh sau nu to thnh cng :</p> <p>FIT-HCMUS</p> <p>Trang 25</p> <p>Spring MVC Framwork Bo co tm hiuTng t cho bng SanPham :</p> <p>Phin bn: 3.0 Ngy: 15/05/2010</p> <p>Gi ta c 2 bng ta cn lin kt kho ngoi t thuc tnh SanPham.MaDM tham chiu n DanhM...</p>