19173552 Tim Hiu Java Struts

  • Published on
    01-Dec-2015

  • View
    40

  • Download
    2

Embed Size (px)

DESCRIPTION

19173552 Tim Hiu Java Struts

Transcript

<ul><li><p>Tm hiu Java Struts</p><p>Gii thiu chung</p><p>D n StrutsStruts, mt d n m ngun m ca Apache Software Foundation, l mt bn hin thc Java pha server ca mu thit k Model-View-Controller (MVC). D n Struts c khi xng bi Craig McClanahan vo thng 5/2000, thuc d n Apache Jakarta. Hin ti, d n Struts tr thnh mt d n c lpd n Apache Struts.</p><p>D n Struts c thit k vi mc ch cung cp mt b khung m ngun m to cc ng dng Web, tch tng trnh by (presentation layer) ra khi tng giao tc (transaction layer) v tng d liu (data layer). T khi ra i, d n Struts nhn c nhiu ng h ca cc nh pht trin v nhanh chng chim u th trong cng ng m ngun m. Quyn sch ny cp phin bn 1.1 ca Struts.</p><p>Mt cuc tranh lun nh din ra trong cng ng nhng nh pht trin v mu thit k m d n Struts ging nht. Theo ti liu c cung cp t chnh nhng nh pht trin d n Struts, n c thit k theo mu MVC; nhng mt s khc th cho rng n ging mu thit k Front Controller hn (mu ny c m t bi Sun's J2EE Blueprints Program). S tht th n rt ging mu Front Controller; nhng trong quyn sch ny, chng ta tho lun theo mu MVC. Nu mun tm hiu v mu Front Controller, bn c th xem ti: http://developer.java.sun.com/developer ... EE/despat/</p><p>Mu thit k MVC nm vng Struts Framework, bn phi hiu c bn v mu thit k MVC. Mu thit k MVC, c bt ngun t Smalltalk, bao gm ba thnh phn: Model, View, v Controller.</p><p>Ba thnh phn ca mu thit k MVCModel Biu din cc i tng d liu. Thnh phn Model l nhng g ang c thao tc v trnh by cho ngi dng.</p><p>View ng vai tr l phn biu din trn mn hnh (screen representation) ca thnh phn Model. Thnh phn View l i tng trnh by trng thi hin ti ca cc i tng d liu.</p><p>Controller nh ngha cch thc giao din ngi dng tng tc li d liu u vo. Thnh phn Controller l i tng thao tc thnh phn Model (hay i tng d liu).</p><p>Di y l mt s li ch khi s dng mu thit k MVC:</p><p>Tnh tin cyTng trnh by v tng giao tc c s phn chia r rng, cho php bn thay i </p></li><li><p>cm quan ca ng dng m khng cn bin dch li m lnh ca Model hay Controller.</p><p>Tnh thch ng v ti s dng caoMu MVC cho php bn s dng nhiu loi khung nhn, tt c cng truy xut n mt m ngun pha server, t trnh duyt web (HTTP) cho n trnh duyt khng dy (WAP).</p><p>Cc ph tn trong vng i pht trin rt thpMu MVC ch cn c ngi lp trnh cp thp pht trin v bo tr cc giao din ngi dng.</p><p>Trin khai nhanh chngThi gian pht trin c th c gim ng k, bi v nhng ngi lp trnh cho thnh phn Controller (nh pht trin Java) ch tp trung vo phn giao tc, cn nhng ngi lp trnh cho thnh phn View (nh pht trin HTML v JSP) ch tp trung vo phn trnh by.</p><p>Tnh bo trVic tch phn trnh by v logic nghip v cng khin vic bo tr v sa i mt ng dng Web da trn Struts d dng hn.</p><p>Bn hin thc Struts ca mu MVC Struts Framework m hnh ha bn hin thc pha server ca mu MVC bng cch kt hp cc trang JSP, cc th JSP ty bin, v mt Java servlet. Trong phn ny, chng ta m t ngn gn cch Struts Framework nh x n mi thnh phn ca mu MVC. </p><p>Bn hin thc Struts ca mu MVC</p><p>Hnh trn m t quy trnh x l m hu ht cc yu cu ng dng Struts cn tun theo. Quy trnh ny c th c chia thnh 5 bc c bn:</p><p>1. Mt yu cu xut pht t mt View c hin th trc .</p><p>2. Yu cu ny c tip nhn bi ActionServlet (ng vai tr l mt Controller), ActionServlet tm URI c yu cu trong mt file XML (s c m t trong chng 3, Lm quen vi Struts) v xc nh tn lp Action s thc hin logic nghip v cn thit.</p></li><li><p>3. Lp Action thc hin logic ca n trn cc thnh phn Model ca ng dng.</p><p>4. Khi hon tt qu trnh x l, lp Action tr quyn iu khin cho ActionServlet, ng thi cung cp mt kha (key) biu th cc kt qu x l. ActionServlet s dng kha ny xc nh cc kt qu s c chuyn n u khi trnh by.</p><p>5. Yu cu kt thc khi ActionServlet chuyn yu cu n View c gn vi kha tr v, v View ny trnh by cc kt qu ca Action.</p><p>Bn c th nhn ra nhng bc trn l mt dng biu din c n gin ha ca mt yu cu Struts. Chng ta s cp chi tit hn v nhng bc ny, bao gm cc thnh phn khc, trong phn tip theo.</p><p>Thnh phn Model</p><p>Cc thnh phn Model ca Struts Framework, nh c gii thiu phn trc, biu din cc i tng d liu ca ng dng Struts. Chng thng biu din cc i tng nghip v hay cc h thng backend khc v c th c hin thc l nhng JavaBean n gin, nhng Enterprise JavaBean, nhng biu din i tng ca d liu c lu tr trong mt c s d liu quan h, hoc ch l nhng g cn c thao tc hay biu din trong mt ng dng Web. Chng ta s tho lun chi tit hn v thnh phn Model trong phn sau, Tch hp Jakarta Commons Database Connection Pool.Thnh phn View</p><p>Mi thnh phn View trong Struts Framework c nh x n mt trang JSP, trang ny c th cha bt k s kt hp no ca cc th ty bin Struts, JSP, v HTML. Cc trang JSP trong Struts Framework c hai chc nng chnh. Th nht, ng vai tr l tng trnh by ca mt Controller Action c thc thi trc . iu ny thng c hon thnh bng cch s dng mt tp th ty bin duyt v thu ly d liu m Controller Action chuyn n JSP ch. Loi View ny khng th hin c trng ca Struts v khng c lu tm.</p><p>Chc nng th hai, th hin c trng ca Struts, l thu thp d liu cn thit thc hin mt Controller Action c th. iu ny thng c hon thnh bng cch kt hp cc th vin th Struts v cc i tng ActionForm. Loi View ny cha mt s lp v th c-trng-Struts. on m sau l mt v d n gin v loi View ny:Code: Chn ht</p><p> User Id: Password: </p></li><li><p>Chng ta s tho lun chi tit hn v thnh phn View trong phn sau, Lm quen vi Struts, v Xy dng tng trnh by.</p><p>Thnh phn ControllerThnh phn Controller ca Struts Framework l xng sng ca tt c cc ng dng Web Struts. N c hin thc bng mt servlet c tn l org.apache.struts.action.ActionServlet. Servlet ny nhn cc yu cu HTTP v giao quyn iu khin ca mi yu cu, da vo URI ca yu cu, cho mt lp org.apache.struts.action.Action do ngi dng nh ngha. Lp Action l ni m thnh phn Model ca ng dng c thu ly v/hay thay i. Khi lp Action hon tt qu trnh x l, n tr v mt kha cho ActionServlet. Kha ny c s dng xc nh View no s trnh by cc kt qu x l. Bn c th hnh dung ActionServlet nh mt i l nhn cc yu cu cho cc dch v, v da trn cc yu cu ny, to ra cc i tng Action nhm thc hin logic nghip v cn thit hon tt cc dch v ny. </p><p>Controller l thnh phn quan trng nht ca Struts Framework. Chng ta s tho lun chi tit hn v thnh phn ny trong phn sau, Lm quen vi Struts, v Action v ActionServlet.</p><p>ng dng WebTt c cc ng dng Struts c ng gi theo nh dng ng dng Web Java. V th, trc khi tip tc, chng ta kho st s lc cc ng dng Web Java.</p><p>Java Servlet Specification 2.2 m t cc ng dng Web Java nh sau: Mt ng dng Web l tp hp cc servlet, cc trang HTML, cc lp, v cc ti nguyn khc, chng c ng gi v chy trn nhiu container t nhiu nh cung cp. Ni mt cch n gin hn, mt ng dng Web Java l mt tp hp ca mt hay nhiu thnh phn web c ng gi cng vi nhau nhm mc ch to ra mt ng dng hon chnh c thc thi tng web ca mt ng dng Enterprise. Di y l danh sch cc thnh phn c th c ng gi trong mt ng dng Web:</p><p>- Cc servlet- Cc trang JSP- Cc th vin th ty bin JSP- Cc lp tin ch v cc lp ng dng- Cc ti liu tnh, bao gm HTML, hnh nh, v JavaScript- Thng tin m t ng dng Web (siu thng tin)</p><p>Cu trc th mcTt c cc ng dng Web c t trong mt cu trc th mc chung, cu trc th mc ny l mt container cha cc thnh phn ca mt ng dng Web. Bc u tin trong vic to ra mt ng dng Web l to ra cu trc th mc ny. Bng 1.2 m t mt ng dng Web mu, c tn l stockapp, v lit k ni dung ca mi th mc. Mi th mc s c to t ca Servlet/JSP container.</p><p>Cu trc th mc ca mt ng dng Web/stockapp y l th mc gc ca ng dng Web. Tt c cc file JSP v HTML u c lu tr ti y.</p></li><li><p>/stockapp/WEB-INF y l th mc cha tt c cc ti nguyn c lin quan n ng dng m khng nm trong gc ti liu ca ng dng. y l ni lu tr b m t trin khai ng dng Web. Bn cn rng th mc WEB-INF khng phi l phn ti liu cng khai. Khng file no trong th mc ny c th c cung cp trc tip cho client.</p><p>/stockapp/WEB-INF/classes Th mc ny cha cc lp tin ch v servlet.</p><p>/stockapp/WEB-INF/lib Th mc ny cha cc file JAR (Java Archive) m ng dng Web ph thuc vo .</p><p>Nu bn s dng Tomcat lm container, th mc gc mc nh l /webapps/. Hnh sau cho thy ng dng stockapp khi n c host bi Tomcat container.</p><p>ng dng stockapp c host bi Tomcat</p><p>ng dng web cho php lu tr cc lp c bin dch trong th mc /WEB-INF/classes v /WEB-INF/lib. Khi x l, cc lp trong th mc /classes s c np trc, sau n cc file JAR trong th mc /lib. Nu c cc lp trng nhau c hai th mc th cc lp trong th mc /classes s c u tin.</p><p>B m t trin khai ng dng WebXng sng ca tt c cc ng dng Web l b m t trin khai ng dng Web (Web application deployment descriptor)mt file XML c tn l web.xml nm trong th mc // application-name/WEB-INF/. File web.xml m t tt c cc thnh phn trong ng ng dng Web. Nu chng ta s dng ng dng stockapp th file web.xml s nm </p></li><li><p>trong th mc // stockapp/WEB-INF/. Nhng thng tin c th c m t trong b m t trin khai bao gm: </p><p>- Cc thng s khi to ServletContext - Ni dung c bn a ha- Cu hnh phin lm vic- Cc nh ngha Servlet/JSP- Cc nh x Servlet/JSP- Cc tham chiu th vin th- Cc nh x kiu MIME- Danh sch file cho mng- Cc trang li- Thng tin bo mt</p><p>B m t trin khai di y nh ngha mt servlet. Chng ta s kho st chi tit hn v file web.xml trong cc phn sau.</p><p>Code: Chn ht</p><p> SimpleServlet com.stock.SimpleServlet </p><p>Tomcat JSP/Servlet containerTomcat JSP/Servlet container l mt b cha ng dng Web (Web application container) m ngun m da trn Java, c to ra thc thi cc ng dng servlet v JavaServer Page. Chng ta s s dng Tomcat cho tt c cc v d ti y.</p><p>Trc khi ci t v cu hnh Tomcat, bn phi m bo cc khon sau:Cc yu cu khi ci t TomcatJava SE Development Kit 5 http://java.sun.com/javase/downloads/index_jdk5.jsp</p><p>Apache Tomcat 5.5.8 http://archive.apache.org/dist/tomcat/tomcat-5/</p><p>Ci t v cu hnh TomcatKhi ti c Tomcat v JDK, bn tin hnh ci t theo hng dn i km. Mc ch ca chng ta l ci t Tomcat lm mt server c lp trn h iu hnh Windows Vista. Chng ta ci </p></li><li><p>t Tomcat trong th mc C:\Program Files\Apache Software Foundation\Tomcat 5.5 v JDK trong th mc C:\Program Files\Java\jdk1.5.0.</p><p>Sau khi ci t Tomcat v JDK xong, bc k tip l thit lp bin mi trng JAVA_HOME. Tht ra, bc ny khng cn thit, bn c th b qua.</p><p> thit lp bin mi trng JAVA_HOME, thc hin nh sau:1. M Control Panel.2. M hp thoi System Properties, sau chn th Advanced.3. Nhp vo nt Environment Variables, bn s thy mn hnh tng t nh hnh 1.3.4. Nhp vo nt New trong phn System variables ca hp thoi Environment Variables. 5. Thm bin c tn l JAVA_HOME v thit lp gi tr l ng dn ca bn ci t JDK. Hnh 1.4 cho thy thit lp ng vi bn ci t ca chng ta.</p><p>Tt c xong, by gi bn c th qua phn k tip kim tra bn ci t Tomcat.</p><p>Hp thoi Environment Variables</p><p>Thit lp bin mi trng JAVA_HOME ng vi bn ci t ca chng ta</p><p>Kim tra bn ci t TomcatTrc khi tip tc, chng ta kim tra cc bc va hon tt. u tin, khi ng Tomcat server bng cch vo Start | All Programs | Apache Tomcat 5.5 | Monitor Tomcat, ri nhp nt Start </p></li><li><p>trong hp thoi Apache Tomcat Properties (xem hnh di).</p><p>Hp thoi Apache Tomcat Properties</p><p>Sau khi Tomcat khi ng, m trnh duyt theo a ch:http://localhost:8080Bn s thy trang ch mc nh ca Tomcat (xem hnh di).</p><p>[img]Trang%20ch%20mc%20nh%20ca%20Tomcat[/img]</p></li><li><p>Bc k tip l kim tra bn ci t JDK. Cch tt nht thc hin iu ny l chy th mt trong cc v d do Tomcat server cung cp. chy mt v d JSP, t trang ch mc nh ca Tomcat, chn JSP Examples. Bn s thy trang tng t nh hnh di.</p><p>Trang JSP Examples</p><p>Chn v d c tn l Snoop v nhp vo lin kt Execute. Nu mi th c ci t ng, bn s thy trang tng t nh hnh di.</p><p>Kt xut ca v d Snoop</p></li><li><p>Re: Tm hiu Java Struts</p><p>tuannq 01-01-2009, 18:37 </p><p>Tng quan v kin trc Java servlet v Javaserver pageTrong phn ny, chng ta s tho lun v hai cng ngh m Struts Framework da vo: Java servlet v JavaServer Page (JSP). Trc tin, chng ta m t kin trc servlet, bao gm vng i ca servlet; mi quan h gia ServletContext v mt ng dng Web; v cch thu ly d liu form bng cc servlet.</p><p>Mt khi bn nm vng v servlet, chng ta s chuyn sang tho lun v JSP, ng vai tr l thnh phn View ca Struts Framework. y, chng ta nh ngha JSP v m t cc thnh phn ca n.</p><p>Mc ch ca chng ny l gii thiu ngn gn v cng ngh servlet v JSP. Qua chng ny, bn s hiu r v servlet v JSP, cng nh cch p dng chng vo qu trnh pht trin ng dng Web Java.</p><p>Kin trc Java servletJava servlet l mt thnh phn ng dng Web c lp nn (platform-independent) c host trong mt JSP/Servlet container. Servlet kt hp vi web-client theo m hnh yu cu/p ng (request/response), c qun l bi mt JSP/Servlet container. Hnh 2.1 m t qu trnh thc thi ca mt Java servlet.</p><p>Hnh: Qu trnh thc thi ca mt Java servlet</p><p>Hai gi cu thnh nn kin trc servlet l javax.servlet v javax.servlet.http. Gi javax.servlet cha cc lp v cc giao din tng qut, c hin thc v k tha bi tt c cc servlet. Gi javax.servlet.http cha tt c cc lp servlet th hin c trng ca giao thc HTTP. </p></li><li><p>Phn ct li ca kin trc ny l giao din javax.servlet.Servlet. y l giao din lp c s cho tt c cc servlet. Giao din Servlet nh ngha nm phng thc, trong ba phng thc quan trng nht l:- Phng thc init()khi to mt servlet- Phng thc service()nhn v p ng cc yu cu ca client- Phng thc destroy()thc hin dn dp</p><p>y l cc phng thc trong vng i ca servlet. Chng ta s m t cc phng thc ny trong phn sau. Tt c cc servlet phi hin thc giao din Servlet mt cch trc tip hoc thng qua k tha. Hnh 2.2 l mt m hnh i tng, cho bn ci nhn mc cao v b khung servlet.</p><p>Hnh: Mt m hnh i tng n gin ca b khung servlet</p><p>Lp GenericServlet v HttpServletHai lp chnh trong kin trc servlet l GenericServlet v HttpServlet. Lp HttpServlet c k tha t lp GenericServlet, GenericServlet hin thc giao din Servlet. Khi pht trin cc servlet cho mnh, rt c kh nng bn s k tha mt trong hai lp ny.</p><p>Khi k tha lp GenericServlet, bn phi hin thc phng thc service(). Phng thc GenericServlet.service() c nh ngha l mt phng thc tru tng ct buc bn tun theo b khung ny. Nguyn mu ca phng thc service() c nh ngha nh sau:Code: Chn ht</p></li><li><p>public abstract void service(ServletRequest request, ServletResponse response) throws ServletException, IOException;</p><p>Hai tham s c truyn cho phng thc service() l i tng ServletRequest v ServletResponse. i tng ServletRequest cha thng tin c gi n servlet, v i tng ServletResponse l ni t d liu m bn mun gi v client.</p><p>Ngc vi GenericServlet, khi k tha HttpServlet, bn thng khng hin thc phng thc service(); lp HttpServlet hin thc phng thc service() cho bn. Nguyn mu sau cha ch k ca phng thc HttpServlet.service():Code: Chn ht</p><p>protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException;</p><p>Khi phng thc HttpServlet.service() c gi, n xt loi phng thc trong yu cu v xc nh cc phng thc HTTP no c gi da vo gi tr ny. y l cc phng thc m bn c th chp . Nu loi phng thc l GET, n s gi phng thc doGet(). Nu loi phng thc l POST, n s gi phng thc doPost(). C nm loi phng thc i cng vi phng thc service(), nhng doGet() v doPost() l hai phng thc thng c s dng nht. Do , chng ta ch tp trung vo hai phng thc ny.</p><p>Vng i ca mt servletVng i ca mt Java servlet tun theo mt trnh t kh hp l. Giao din khai bo cc phng thc trong vng i ca servlet l javax.servlet.Servlet. Cc phng thc ny l init(), service(), v destroy(). Trnh t ny c th c m t trong mt qu trnh n gin gm ba bc:</p><p>1. Mt servlet c np v khi to bng phng thc init(). Phng thc ny c gi khi mt servlet c np trc hoc trong ln u tin yu cu servlet ny.2. Sau , servlet s phc v 0 hoc nhiu yu cu. Servlet s dng phng thc service() phc v mi yu cu.3. Sau , servlet b hy v dn dp khi ng dng Web cha servlet kt thc. Phng thc c gi khi kt thc l destroy().</p><p>Phng thc init()Phng thc init() l ni bt u vng i ca servlet. Phng thc ny c gi ngay sau khi servlet c th hin ha v ch c gi mt ln duy nht. Phng thc init() nn c s dng to v khi to cc ti nguyn s c s dng trong khi th l cc yu cu. Ch k ca phng thc init() c nh ngha nh sau:Code: Chn ht</p><p>public void init(ServletConfig config) throws ServletException;</p><p>Phng thc init()...</p></li></ul>