bai19 struts basic
TRANSCRIPT
-
8/12/2019 Bai19 Struts Basic
1/20
9/10/201
LP TRNH WEB HNG JAVABi 19: C bn v Struts
Ging vin: ThS. Trnh Tun tB mn CNPM
Email: [email protected]/[email protected]
B mn Cng ngh Phn mmVin CNTT & TT
Trng i hc Bch Khoa H Ni
1DatTT-DSE-SOICT-HUST
Ni dung
1. Struts l g, ti sao s dng Struts? 2. Kin trc ca Struts
ControllerModel
View
3. web.xml
4. Internationalization
5. Input Validation
6. X l li 7. Chn View tr v
2DatTT-DSE-SOICT-HUST
1.1. Struts l g?
3DatTT-DSE-SOICT-HUST
Jakarta Struts
Struts l mt Web application frameworkmngun m, l mt phn ca Apache Jakartaprojecthttp://jakarta.apache.org/struts/
4DatTT-DSE-SOICT-HUST
1.1. Struts l g?
Model-View-Controller (MVC) framework
c s dng xy dng cc ng dng Web sdng Servlets v JSPs ng dng Struts l ng dng Web, chy c trn bt k
Sevlet container no, bao gm c tt c cc J2EEcompliant App servers
Tun theo cc mu thit k Singleton, composition view, delegate
D hc & s dng
C km cc th vin th JSP custom tag
5DatTT-DSE-SOICT-HUST
Struts cho php cc LTV ...
Pht trin ng dng web JSP/Servlet theom hnh MVC
S dng cc i tng c sn ca frameworktrong file cu hnh XML
S dng cc mu thit k xy dng sn caframework
S dng cc c tnh nng cao nh inputvalidation, internationalization
6DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
2/20
9/10/201
1.2. Ti sao s dng Struts
7DatTT-DSE-SOICT-HUST
1.2. Ti sao s dng Struts?
Struts m nhim nhng phn phc tp nht khixy dng ng dng MVC
D hc v s dng Nhiu tnh nng Nhiu cng c h tr ca bn th 3 (third party
tools)
Linh ng v d m rng (Flexible & extensible) Cng ng s dng ng o n nh v vn ang lin tc pht trin M ngun m
8DatTT-DSE-SOICT-HUST
1.2. Ti sao s dng Struts?
Tch hp tt vi Java EE (J2EE) H tr taglib tt X l li thng nht, theo c 2 c ch
programmatic v declarative
Tch hp vi Tiles framework (Struts nngcao)
9DatTT-DSE-SOICT-HUST
2. Kin trc ca Struts
10DatTT-DSE-SOICT-HUST
Model-View-Control (model 2)
Request1
(Controller)Servlet
(View)JSPResponse
5
3
4
BROWSER
Servlet Container (EIS)
Redirect2
(Model)Java Bean
MVC Design Pattern
11DatTT-DSE-SOICT-HUST
Struts hot ng nh th no
12DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
3/20
9/10/201
Struts: Kin trc MVC
Controller trung tm iu hnh lung ng dng, yquyn cho cc b x l thch hp gi lAction
Action Handlers c th s dng cc modelcomponents Model ng gi trng thi hoc logic nghip v iu khin c chuyn tip (forward) cho
Controller vi View ph hp Vic forward c th c xc nh bng vic tra cu t
tp cc mappings trong file cu hnh, cho php tng linh ng ga tng View v Model
13DatTT-DSE-SOICT-HUST
Struts: Kin trc MVC
3 thnh phn chnh trong StrutsServlet controller (Controller)
Java Server Pages hoc bt k cng ngh hinth no khc (View)
Application Business Logic dng bt k ph hpvi ng dng (Model)
Struts tp trung ch yu vo phn Controller Struts c s c lp gia Model & View Struts c th s dng bt k cng ngh Model &View no
14DatTT-DSE-SOICT-HUST
Struts: Kin trc MVC
File cu hnh cha cc action mappingsnh x URL vi cc Action Controller s dng cc mapping ny tr v
HTTP requests cho Action no ca ng dng Xc nh forwarding/navigation
Mapping phi m t c ng dn ca yu cu: request path
Action x l request nhn c
15DatTT-DSE-SOICT-HUST
2.1. Controller
16DatTT-DSE-SOICT-HUST
Controller c nhim v g?
Ging nh 1 switch trong kin trc MVC Tt c cc request phi i qua controller Chu trch nhim iu khin lung cho cc
x l request (action mapping) c file cu hnh xc nh lung iu khin
17DatTT-DSE-SOICT-HUST
Cc thnh phn Controller
18DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
4/20
9/10/201
Controller trong Struts Framework
Struts framework cung cp servlet:org.apache.struts.action.ActionServlet
Phi cu hnh Servlet mapping cho servlet nytrong file web.xml
Cc cu hnh lin quan ti Struts khc cto lp trong file struts-config.xmlAction Mapping nh ngha cc nh x gia URI
ca request gi n vi 1 lp Action c th
19DatTT-DSE-SOICT-HUST
Nhim v ca LTV
Vit lp Action (k tha lp Action) cho mirequest c th nhn c Phi override phng thc execute() (phng
thc perform() trong Struts 1.0)
Vit action mapping trong file cu hnhstruts-config.xml
Cp nht file web.xml, thc hin mappingcho ActionServletweb.xml
20DatTT-DSE-SOICT-HUST
Cc thnh phn Controller trong StrutsFramework
ActionServlet (Struts cung cp) RequestProcessor (Struts 1.1) (Struts cung cp)
Cho mi module
Action (LTV vit) LTV k tha lp Action do Struts cung cp
Action Mapping (To bi LTV) LTV to cc action mapping trong file struts-config.xml Struts framework s to cc i tng ActionMapping v
truyn chng cho i tng Action
21DatTT-DSE-SOICT-HUST
2.1.1. ActionServlet(Framework cung cp)
22DatTT-DSE-SOICT-HUST
Nhim v ca ActionServlet?
Thc hin vai tr ca Controller X l cc user requests Xc nh xem user cn g, cn c o request
nhn c Ly d liu t model (nu cn thit), v Ly ra view ph hp tr v cho ngi dng
y nhim hu ht nhng cng vic ny cholp Action
23DatTT-DSE-SOICT-HUST
Nhim v ca ActionServlet? (2)
Chu trch nhim khi to v dn dp tinguyn loads cc cu hnh ng dng trong web.xml Duyt ln lt cc goes through an enumeration
of all init-param elements, looking for thoseelements who's name starts with config/ formodules
truy cp vo module foo, s dng URL nh:http://localhost:8080/myApp/foo/someAction.do
24DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
5/20
9/10/201
iu khin lung bi Controller
25DatTT-DSE-SOICT-HUST
Lung Struts (Struts 1.0)Http://myhost/authorize.do
Server configured to pass *.do extensions to
org.apache.struts.action.ActionServletvia a
web.xml configuration file
ActionServlet object inspects the URI and tries to match itagainst anActionMapping located in the struts-config.xml file
Instance of appropriateAction class is found and its execute()is called.
Action object handles the request and returns a next view.View is identified byActionForward objerct.
26DatTT-DSE-SOICT-HUST
2.1.2. RequestProcessor(Framework cung cp)
27DatTT-DSE-SOICT-HUST
Nhim v ca RequestProcessor?
processPath Xc nh ng dn (path) c request. Sau s c
s dng ly v mt ActionMapping.
processLocale Chn mt locale (nu cha chn locale no) v t n
vo trong request.
processContent Thit lp content type mc nh (ty chn cch encoding)
cho tt c cc response nu c yu cu
28DatTT-DSE-SOICT-HUST
What Does RequestProcessor Do?
processMapping Xc nh ActionMapping ng vi ng dn yu cu.
processRoles Nu mapping c kt hp vi mt role, cn m bo
user c role tng ng . Nu khng, to ra mt errorv dng x l request.
processActionForm To mt thc th ActionForm (nu cn thit) kt hp vi
mapping trn (nu c), vi tm vc (scope) ph hp
29DatTT-DSE-SOICT-HUST
What Does RequestProcessor Do?
processPopulate Gn ActionForm vi request, nu c
processValidate Thc hin validate (nu c yu cu) trnActionForm gn vi request ny (nu c).
processForward Nu mapping biu din mt forward, forward
n ng dn c ch nh trong mapping.
30DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
6/20
9/10/201
What Does RequestProcessor Do?
processInclude Nu mapping biu din mt include, include kt
qu ca vic gi n ng dn (path) trongrequest ny
processActionCreate
To mt thc th ca lp c c t trongActionMapping ang xt (nu cn).
processActionPerform L lc m phng thc perform() hoc execute()
s c gi31DatTT-DSE-SOICT-HUST
What Does RequestProcessor Do?
processForwardConfig Cui cng, phng thc x l ca
RequestProcessor ly v ActionForward tr v tlp Action, v s dng n chn ra resourcetip theo (nu c).
Thng ActionForward s dn n cc trangtrnh din (presentation page), sinh ra responsetr v cho client.
32DatTT-DSE-SOICT-HUST
2.1.3. Action Mapping(LTV vit)
33DatTT-DSE-SOICT-HUST
Action Mapping trong file cu hnh Struts
Struts controller ActionServlet cn bit 1 sthng tin map mi request URI gi nvi lp Action ph hp
Cc yu cu ny c ng gi trong mtJava interface l ActionMapping
Struts framework s to i tngActionMapping cho mi phn t cu hnh
trong file struts-config.xml
34DatTT-DSE-SOICT-HUST
Struts Config File (struts-config.xml)
struts-config.xml cha 3 phn t quan trng
m t cc actions: cha cc nh ngha FormBean
bao gm tn v kiu (classname) cha cc nh ngha action
S dng mt phn t cho mi action cnh ngha
cha c nh ngha globalforward
35DatTT-DSE-SOICT-HUST
Struts Config File (struts-config.xml)
Phn t ny cha cc nh ngha form bean. S dng phn t cho mi form
bean, bao gm 2 thuc tnh quan trng sau:name: the name of the request request (and session
level attribute that this form bean will be stored as)
type: tn lp y ca form bean
36DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
7/20
9/10/201
struts-config.xml:
1.
2.
5.
6. 7.
8.
10.
37DatTT-DSE-SOICT-HUST
Struts Config File (struts-config.xml)
Phn t ny cha cc nh ngha action. Mi
action cn nh ngha ng vi mt phn t
38DatTT-DSE-SOICT-HUST
Struts Config File (struts-config.xml)
Mi phn t cn nh ngha cc thuc
tnh sau: path: ng dn ti action (tng i so vi context
ca ng dng) (URI ca request) type: Tn y ca lp Action name: Tn ca phn t s dng vi
action ny
input: Tn ca trang hin th khi c li validate inputform scope: Tm vc ca form bean c to validate: C thc hin validate input form hay khng 39DatTT-DSE-SOICT-HUST
struts-config.xml:
1.2.
3. 9. 10. 11.
12.
13.
40DatTT-DSE-SOICT-HUST
struts-config.xml:
1.
2.
9.
10.
41DatTT-DSE-SOICT-HUST
Action Mapping Config File
Forwards l cc thc th ca lpActionForward
c tr v t phng thc execute() ca lpAction
Thc hin map cc logical names vi ccresources c th (thng l cc trang JSPs)
Phn t c cc thuc tnh sau name: logical name ca forward
c s dng trong phng thc execute() ca lp Action forward n ti nguyn tip theo
path: resource s c forward nredirect: redirect (true) hay forward (false) 42DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
8/20
9/10/201
Global Forwarding
1.2. 3. 6. 7. 9. 13. 14.
43DatTT-DSE-SOICT-HUST
Local Forwarding
1.2.8. 11. 14.
44DatTT-DSE-SOICT-HUST
2.1.4. ActionForm(LTV phi cung cp, nu khng sdng DynaActionForm)
45DatTT-DSE-SOICT-HUST
ActionForm Bean (Form bean)
Vit bi LTV nh ngha mt ActionForm bean (bn cht l 1 lp Java
k tha lp ActionForm) cho input form nh ngha trong file struts-config.xml
Tn thuc tnh ca lp
Ch cha cc phng thc getter & setter cho ccthuc tnh (khng c x l nghip v)
Cung cp c ch validation chun tc
46DatTT-DSE-SOICT-HUST
ActionForm Bean & Controller
Vi miActionForm bean c nh ngha trong file
servlet-config.xml, Controller (ActionServlet) s Kim tra session scope cho 1 thc th ca ActionForm
bean Nu khng tn ti, controller s to mi mt thc th
Gi phng thc setter tng ng ca ActionForm beancho tt c cc tham s request c tn trng vi tn cathuc tnh ca bean
Truyn i tng ActionForm bean lm tham s caphng thc execute() ca lp Action
47DatTT-DSE-SOICT-HUST
Vit mt ActionForm Bean?
Ch cn vit phng thc getter v setter cho
mi thuc tnh ca mt input form Khng c bt k on code x l nghip v no
Thm 1 phng thc validate chun tc:validate() Controller s gi n phng thc ny
nh ngha mt thuc tnh (cng vi phngthc getXxx v setXxx) cho mi trng xuthin trong input form
48DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
9/20
9/10/201
Vd:submit.jsp
1.2.
3.
4.
5.
6.Submit example
7.
8.Example Submit Page
9.
10.
11.Last Name:
12.Address:
13.Sex: Male
14. Female
15.Married:
16.Age:
17. 0-19
18. 20-49
19. 50-
20.
21.
22.49DatTT-DSE-SOICT-HUST
Model: ActionForm
1.package submit;
2.import javax.servlet.http.HttpServletRequest;3.import org.apache.struts.action.*;
4.public final class SubmitForm extends ActionForm {
5. /* Last Name */6. private String lastName = "Hansen"; // default value7. public String getLastName() {8. return (this.lastName);9. }10. public void setLastName(String lastName) {11. this.lastName = lastName;12. }
13. /* Address */14. private String address = null;15. public String getAddress() {16. return (this.address);17. }18. public void setAddress(String address) {19. this.address = address;20. }21.... 50DatTT-DSE-SOICT-HUST
struts-config.xml:
1.
2.
5.
6. 7.
8.
10.
51DatTT-DSE-SOICT-HUST
struts-config.xml:
1.2.
3. 9. 10. 11.
12.
13.
52DatTT-DSE-SOICT-HUST
DynaActionForm trong Struts 1.1
Thay v to mt lp con cho ActionForm vi
cc phng thc get/set cho mi thuc tnhca bean, LTV c th lit k cc thuc tnhny, cng vi kiu, gi tr mc nh trong filecu hnh Struts NN s dng DanaActionForm khi c th
(Struts nng cao)
53DatTT-DSE-SOICT-HUST
2.1.5. Action(LTV t vit)
54DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
10/20
9/10/201
1
Action Class Diagram
55DatTT-DSE-SOICT-HUST
Lp Action
Tp trung vo lung iu khin X l client request bng cch gi cch i tng khc
(BusinessLogic beans) trong phng thc execute() can Tr v mt i tng ActionForward nh danh mt
destination resource m iu khin s c forward nDestination resource c th l
JSP
Tile definition
Velocity template
Action khc
56DatTT-DSE-SOICT-HUST
Lp Action l g?
Lp Java thc hin cc cng vic trong ng dng X l request Thc hin cc x l logic nghip v
C th n gin hoc phc tp Lp action n gin t thc hin x l logic nghip v Lp action phc tp y quyn cc x l logic nghip v
cho cc thnh phn Model Chc nng ca lp Action ging nh mu thit k Facade
57DatTT-DSE-SOICT-HUST
V d Action: Logon
ng dng cn Xc thc ngi dng Thit lp User Session X l li
Cn vit code lp LogonAction
58DatTT-DSE-SOICT-HUST
Nhim v ca LTV: Action Class
K tha org.jakarta.struts.action.ActionOverride
Phng thc execute() (trong Struts 1.1) Phng thc perform() (trong Struts 1.0)
59DatTT-DSE-SOICT-HUST
Phng thc execute(..) ca lp Action(Ch trong Struts 1.1)
c gi bi controller
publicActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception;
60DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
11/20
9/10/201
Phng thc perform(..) ca lp Action(Ch trong Struts 1.0)
Deprecated
publicActionForward perform(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException;
61DatTT-DSE-SOICT-HUST
Phng thc execute() ca lp Action
Thc hin cc x l cn thit vi request Cp nht cc i tng pha server (Scope
variables) c s dng to trang tr vcho ngi dng
Tr v i tng ActionForward ph hp
62DatTT-DSE-SOICT-HUST
V d: lp Action1.package submit;
2.import javax.servlet.http.*;
3.import org.apache.struts.action.*;
4.public final class SubmitAction extends Action {
5. public ActionForward execute(ActionMapping mapping,
6. ActionForm form,
7. HttpServletRequest request,
8. HttpServletResponse response) {
9. SubmitForm f = (SubmitForm) form; // get the form bean
10. // and take the last name value
11. String lastName = f.getLastName();
12. // Translate the name to upper case
13. //and save it in the request object
14. request.setAttribute("lastName", lastName.toUpperCase());15.
16. // Forward control to the specified success target
17. return (mapping.findForward("success"));
18. }
19.}63DatTT-DSE-SOICT-HUST
Hng dn thit k lp Action
Controller Servlet to 1 thc th duy nhtcho lp Action, v s dng n cho tt c ccrequest Lp Action phi hot ng ch multi-
threaded safe
Ch s dng cc bin local (as opposed toinstanace variables)
Ch nn to cc lp Action n gin S dng cc thnh phn Model cho cc x l
logic nghip v phc tp64DatTT-DSE-SOICT-HUST
V d 2: Lp Action1.package submit;
2.import javax.servlet.http.*;
3.import org.apache.struts.action.*;
4.public final class SubmitAction extends Action {5. public ActionForward execute(ActionMapping mapping,
6. ActionForm form,
7. HttpServletRequest request,
8. HttpServletResponse response) {
9. SubmitForm f = (SubmitForm) form; // get the form bean
10. // and take the last name value
11. String lastName = f.getLastName();
12. if (lastName.startsWith(Passion)){
13. // Translate the name to upper case
14. //and save it in the request object
15. request.setAttribute("lastName", lastName.toUpperCase());
16. // Forward control to the specified success target
17. return (mapping.findForward("success"));
18. }
19. else{
20. return (mapping.findForward("failure"));
21. }
22. }
23.} 65DatTT-DSE-SOICT-HUST
struts-config.xml: ActionMapping
1. 2.
3. 9. 10. 11.
12.
13.66DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
12/20
-
8/12/2019 Bai19 Struts Basic
13/20
9/10/201
1
Cc thnh phn Model trong StrutsFramework
ActionForm Bean Lu ActionForm Bean l mt Model component,
mc d n ch biu din d liu nhp bi ngidng
SystemState Bean
Ch l thut ng mc khi nim: Struts khngcung cp cc API lp trnh
BusinessLogic Bean Ch l thut ng mc khi nim: Struts khng
cung cp cc API lp trnh73DatTT-DSE-SOICT-HUST
Struts Model Components
System State Bean & Businesslogic Bean
74DatTT-DSE-SOICT-HUST
System State Bean
Struts khng nh ngha lp chun cho loi beanny
Xc nh trng thi hin ti C th c biu din bi tp mt hoc nhiu cc lp
JavaBeans, cc lp c cc thuc tnh xc nh trng thihin ti
V d: h thng gi hng-shopping cart: Cha mt bean tng trng cho gi hng c qun l
c lp cho mi ngi mua hng Bao gm tp cc items m ngi mua hng chn mua
75DatTT-DSE-SOICT-HUST
System State Bean
Vi h thng quy m nh, ho vi cc thng tintrng thi (state information) khng cn lu tr ludi: Tp cc system state beans c th cha ton b tri thc
h thng c c
Vi ng dng quy m ln Cc System state beans c th biu din thng tin c
lu tr lu di trong cc CSDL ngoi V d: i tng CustomerBean tng ng vi 1 row no
trong bng CUSTOMERS C th s dng EJB
76DatTT-DSE-SOICT-HUST
BusinessLogic Bean
Struts khng nh ngha lp chun cho loibean ny C th l mt JavaBean thng thng C th l EJB c trng thi hoc phi trng thi
ng gi cc functional logic (lm thay itrng thi h thng) thnh cc phng thc H thng ln: phng thc nm lun trong cc
beans biu din trng thi h thng H thng nh: phng thc nm trong cc lpAction
77DatTT-DSE-SOICT-HUST
BuesinessLogic Bean
L tng nht l phi c thit k sao cho
chng khng bit mnh c thc thi trnmi trng Web Khng nn tham chiu n bt k i tng no
trong ng dng Web Tng tnh ti s dng
78DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
14/20
9/10/201
1
2.3. Cc thnh phn View
(View Components)
LTV phi cung cp
79DatTT-DSE-SOICT-HUST
View components
80DatTT-DSE-SOICT-HUST
View Components
CcJSP files cho tng ng dng Tp cc JSP custom tag libraries Cc Resource files cho tnh nng
internationalization
Cho php nhanh chng to form cho cc ngdng
Phi hp hot ng vi controller Servlet
81DatTT-DSE-SOICT-HUST
View
i tngActionForward thng bo viServlet controller trang JSP no s c trv (dispatched)
Cc trang JSP s dngActionForm ly dliu output t Model hin th
Struts c tp cc th vin th (tag libraries) D pht trin cc ni dung ng n gin trao i gia HTML designer v LTV
82DatTT-DSE-SOICT-HUST
Forms v FormBean Interactions
Nu user gy li (vd: nhp liu), ng dng
cn cho php h sa i li gi tr cVi JSP:
Vi Struts, ch cn;
83DatTT-DSE-SOICT-HUST
V d: submit.jsp1.2.3.4.
5.6.Submit example7.
8.Example Submit Page
9.
10.11.Last Name:
12.Address:
13.Sex: Male14. Female
15.Married:
16.Age: 17. 0-1918. 20-4919. 50-2 0. < /h tml:se le ct>
21. 22.84DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
15/20
9/10/201
1
V d: submit.jsp
1.2.Hello
3.
4. young
5.
6.
7 . o ld
8.
9.
10.
11.
12.
85DatTT-DSE-SOICT-HUST
3. web.xml
86DatTT-DSE-SOICT-HUST
Web App Deployment Descriptor(web.xml)
ng dng Struts l mt ng dng Web Tun th theo lut chung Phi c file deployment descriptor - web.xml
web.xml cha: Cu hnh mapping cho ActionServlet nh ngha Resource file trong Ch nh file servlet-config.xml trong nh ngha cc th vin th Struts
web.xml c lu tr trong WEB-INF/web.xml
87DatTT-DSE-SOICT-HUST
V d: web.xml1.
4.5. Advanced J2EE Programming Class Sample App6.7. 8. 9. action10. 11. org.apache.struts.action.ActionServlet12. 13. 14. application15. ApplicationResources
16. 17. 18. config19. /WEB-INF/struts-config.xml20. 21. 88DatTT-DSE-SOICT-HUST
V d: web.xml1. 2.
3. action
4. *.do5.
6.
7.
8.
9. /WEB-INF/struts-bean.tld
10. /WEB-INF/struts-bean.tld
11.
12.
13. /WEB-INF/struts-html.tld
14. /WEB-INF/struts-html.tld
15.
16.
17. /WEB-INF/struts-logic.tld
18. /WEB-INF/struts-logic.tld
19.
20. 89DatTT-DSE-SOICT-HUST
4. Internationalization
90DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
16/20
9/10/201
1
Internationalization
M rng cajava.util.ResourceBundleorg.apache.struts.util.MessageResources
Ch gii hn h tr hin th. Nhp liu tytheo thit b client
Cu hnh resource bundles trong web.xmlfile
91DatTT-DSE-SOICT-HUST
Internationalization: Nhim v ca LTV
To resource file cho ngn ng mc nh To cc resource files cho mi ngn ng
mun h tr nh ngha tn ca cc resource nh cc
tham s khi toTrong trang JSP
S dng hin th cc thngbo li theo locale
92DatTT-DSE-SOICT-HUST
Resource files
MyApplication.properties Cha thng ip vit bng ngn ng mc nh Mi thng ip c dng (vd Ting Anh):
prompt.hello=Hello
MyApplication_xx.properties
Cha cng thng ip tng ng, nhng cc ngn ng khc, c m ngn ng theo chun
ISO l "xx"prompt.hello=Bonjour
93DatTT-DSE-SOICT-HUST
V d: ApplicationResource.properties
1.errors.header=Validation Error(s)2.errors.footer=
3.error.lastName=Enter your last name
4.error.address=Enter your address
5.error.sex=Enter your sex
6.error.age=Enter your age
94DatTT-DSE-SOICT-HUST
V d: web.xml
1.
2. action3.
4. org.apache.struts.action.ActionServlet
5.
6.
7. application
8.
9. com.mycompany.mypackage.MyApplication
10.
11.
12.
13.
95DatTT-DSE-SOICT-HUST
5. (Input) Validation
96DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
17/20
9/10/201
1
Validation: Nhim v ca LTV (Struts 1.0)
Ch nh s thc hin validate input bng thuc tnhvalidate ca phn t (con ca phn t
) trong file servlet-config.xml validate=true
Ch nh trang JSP cn c hin th khi validationkhng thnh cng input=/errorpage.jsp
Override phng thc validate() trong lpActionForm Ty chn
97DatTT-DSE-SOICT-HUST
validate() method
c gi bi controller servlet Sau khi ly xong cc thuc tnh ca bean
Nhng trc khi phng thc execute() ca lp Actiontng ng c gi
Ty chn Phng thc mc nh tr v null
C php:public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request);
98DatTT-DSE-SOICT-HUST
V d: phng thc validate()
Trong ng dng Login Cn m bo nhp ng thi c username vpassword
Cn m bo password c t nht l 6 k t
99DatTT-DSE-SOICT-HUST
Phng thc validate()
Sau khi thc hin validate Nu khng xy ra li validate no, tr v null Nu xy ra li validate, tr v 1 thc th ca lpActionErrors
Mi ActionError cha kha thng ip li (error message key)trong MessageResources bundle ca ng dng
Controller servlet lu tr mng ActionErrors nh 1 thuc tnhrequest s dng cho cc th
Controller sau forwards iu khin t li input form (xc nhtrong thuc tnh input ho Actionmapping tng ng)
100DatTT-DSE-SOICT-HUST
Lp ActionError
L c ch c s dng tr v cc li khi
thc hin validate input ng gi li cc li
Cc message key c s dng tm kim ramessage text ph hp t cc resource file
ActionErrors l mt tp cc ActionError
101DatTT-DSE-SOICT-HUST
struts-config.xml: Validation
1. 2.
3. 9. 10. 11.
12.
13.102DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
18/20
9/10/201
1
ActionForm1.public final class SubmitForm extends ActionForm {2.3....4. public ActionErrors validate(ActionMapping mapping,5. HttpServletRequest request) {
6. ...7.8. // Check for mandatory data9. ActionErrors errors = new ActionErrors();10. if (lastName == null || lastName.equals("")) {11. errors.add("Last Name", new ActionError("error.lastName"));12. }13. if (address == null || address.equals("")) {14. errors.add("Address", new ActionError("error.address"));15. }16. if (sex == null || sex.equals("")) {17. errors.add("Sex", new ActionError("error.sex"));18. }19. if (age == null || age.equals("")) {20. errors.add("Age", new ActionError("error.age"));21. }22. return errors;23 . }24...25.}
103DatTT-DSE-SOICT-HUST
V d: submit.jsp1.
2.
3.
4.
5.
6.Submit example
7.
8.Example Submit Page
9.
10.
11.Last Name:
12.Address:
13.Sex: Male
14. Female
15.Married:
16.Age:
17. 0-19
18. 20-49
19. 50-
2 0. < /html :s elec t>
21.
22. 104DatTT-DSE-SOICT-HUST
Validator Framework trong Struts 1.1
Validation logic c th c m t trong filecu hnh struts-config.xml Thay v phi vit code phng thc Java:
validate()
(Struts nng cao)
105DatTT-DSE-SOICT-HUST
6. X l li (Error Handling)
106DatTT-DSE-SOICT-HUST
CustomExceptionHandler (1.1)
T nh ngha lp ExceptionHandler, c
gi n khi phng thc execute() ca 1Action nm ra 1 Exception L lp con ca
org.apache.struts.action.ExceptionHandler
Phng thc execute() phi x l Exception vtr v 1 i tng ActionForward thng bovi Struts s forward n u tip
107DatTT-DSE-SOICT-HUST
ExceptionHandler t nh ngha (1.1)
Cu hnh b x l ngoi l trong struts-config.xml
com.yourcorp.ExceptionHandler.execute() sc gi khi c bt k mt IOException noc nm ra bi mt Action
108DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
19/20
9/10/201
1
CustomExceptionHandler (1.1)
C th c tc dng global hoc cho tngaction
109DatTT-DSE-SOICT-HUST
V d: nm ra mt Exception1.package hansen.playground;
2.import javax.servlet.http.*;
3.import org.apache.struts.action.*;
4.public final class SubmitAction extends Action {
5. public ActionForward execute(ActionMapping mapping,
6. ActionForm form,
7. HttpServletRequest request,8. HttpServletResponse response) {
9. SubmitForm f = (SubmitForm) form; // get the form bean
10. // and take the last name value
11. String lastName = f.getLastName();
12. if (lastName.startsWith(Passion)){
13. // Translate the name to upper case
14. //and save it in the request object
15. request.setAttribute("lastName", lastName.toUpperCase());
16. // Forward control to the specified success target
17. return (mapping.findForward("success"));
18. }
19. else{
20. throw new WrongLastNameExcetion(lastName);
21. }
22. }
23.} 110DatTT-DSE-SOICT-HUST
ExceptionHandler t nh ngha (1.1)
Cc ExceptionHandler khc nhau cho cc likhc nhau
111DatTT-DSE-SOICT-HUST
7. View Selection
112DatTT-DSE-SOICT-HUST
View Selection: Nhim v ca LTV
Trong file struts-config ,Vi mi outcome (gi tr ca thuc tnh name ca
th ), ch nh trang JSP s cforward ti nh th con , con ca th hoc th
Trong phng thc execute() ca lp Action, Tr v i tng ActionForward, ng vi
outcome no.
113DatTT-DSE-SOICT-HUST
struts-config.xml: ActionMapping
1. 2.
3. 9. 10. 11.
12.
13.114DatTT-DSE-SOICT-HUST
-
8/12/2019 Bai19 Struts Basic
20/20
9/10/201
V d: Action Class1.package hansen.playground;
2.import javax.servlet.http.*;
3.import org.apache.struts.action.*;
4.public final class SubmitAction extends Action {
5. public ActionForward execute(ActionMapping mapping,
6. ActionForm form,
7. HttpServletRequest request,
8. HttpServletResponse response) {
9. SubmitForm f = (SubmitForm) form; // get the form bean
10. // and take the last name value
11. String lastName = f.getLastName();
12. // Translate the name to upper case
13. //and save it in the request object
14. request.setAttribute("lastName", lastName.toUpperCase());
15.
16. // Forward control to the specified success target
17. return (mapping.findForward("success"));
18. }
19.} 115DatTT-DSE-SOICT-HUST