Professional Documents
Culture Documents
Ni dung
Controller
Model
View
3. web.xml
4. Internationalization
5. Input Validation
6. X l li
7. Chn View tr v
DatTT-DSE-SOICT-HUST
Jakarta Struts
1.1. Struts l g?
DatTT-DSE-SOICT-HUST
DatTT-DSE-SOICT-HUST
1.1. Struts l g?
http://jakarta.apache.org/struts/
9/10/2011
DatTT-DSE-SOICT-HUST
DatTT-DSE-SOICT-HUST
Model-View-Control (model 2)
DatTT-DSE-SOICT-HUST
10
Struts hot ng nh th no
BROWSER
(Controller)
Servlet
Redirect
5
Response
(View)
JSP
(Model)
Java Bean
Servlet Container
DatTT-DSE-SOICT-HUST
(EIS)
11
DatTT-DSE-SOICT-HUST
12
9/10/2011
13
14
nh 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
2.1. Controller
Mapping phi m t c
DatTT-DSE-SOICT-HUST
15
Controller c nhim v g?
DatTT-DSE-SOICT-HUST
16
DatTT-DSE-SOICT-HUST
17
DatTT-DSE-SOICT-HUST
18
9/10/2011
Nhim v ca LTV
org.apache.struts.action.ActionServlet
Phi cu hnh Servlet mapping cho servlet ny
trong file web.xml
struts-config.xml
DatTT-DSE-SOICT-HUST
web.xml
19
DatTT-DSE-SOICT-HUST
20
2.1.1. ActionServlet
(Framework cung cp)
Cho mi module
LTV k tha lp Action do Struts cung cp
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
DatTT-DSE-SOICT-HUST
21
DatTT-DSE-SOICT-HUST
Nhim v ca ActionServlet?
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
22
23
http://localhost:8080/myApp/foo/someAction.do
DatTT-DSE-SOICT-HUST
24
9/10/2011
Http://myhost/authorize.do
DatTT-DSE-SOICT-HUST
25
DatTT-DSE-SOICT-HUST
26
Nhim v ca RequestProcessor?
2.1.2. RequestProcessor
(Framework cung cp)
processPath
processLocale
processMapping
DatTT-DSE-SOICT-HUST
processPopulate
processValidate
processActionForm
Nu mapping c kt hp vi mt role, cn m bo
user c role tng ng . Nu khng, to ra mt error
v dng x l request.
DatTT-DSE-SOICT-HUST
29
28
Xc nh ActionMapping ng vi ng dn yu cu.
processRoles
27
processContent
DatTT-DSE-SOICT-HUST
Gn ActionForm vi request, nu c
Thc hin validate (nu c yu cu) trn
ActionForm gn vi request ny (nu c).
processForward
DatTT-DSE-SOICT-HUST
30
9/10/2011
processInclude
processForwardConfig
processActionCreate
To mt thc th ca lp c c t trong
ActionMapping ang xt (nu cn).
processActionPerform
31
DatTT-DSE-SOICT-HUST
32
DatTT-DSE-SOICT-HUST
DatTT-DSE-SOICT-HUST
33
<form-beans>
34
35
DatTT-DSE-SOICT-HUST
36
9/10/2011
struts-config.xml: <form-beans>
<action-mappings>
5.<struts-config>
6. <!-- ========== Form Bean Definitions ================= -->
7. <form-beans>
8.
9.
<form-bean
name="submitForm"
type="submit.SubmitForm"/>
10. </form-beans>
DatTT-DSE-SOICT-HUST
37
DatTT-DSE-SOICT-HUST
struts-config.xml: <action-mappings>
<action-mappings>
3. <action path="/submit"
4.
type="submit.SubmitAction"
5.
name="submitForm"
6.
input="/submit.jsp"
7.
scope="request"
8.
validate="true">
9. <forward name="success" path="/submit.jsp"/>
10. <forward name="failure" path="/submit.jsp"/>
11. </action>
12. </action-mappings>
13.</struts-config>
DatTT-DSE-SOICT-HUST
2.
3.
4.
5.
6.
7.
8.
<action-mappings>
<global-forwards>
<action
path="/logon"
type="org.apache.struts.webapp.example.LogonAction"
name="logonForm"
scope="request"
input="/logon.jsp"
validate="true" />
9. </action-mappings>
10.</struts-config>
DatTT-DSE-SOICT-HUST
40
struts-config.xml: <action-mappings>
1.
38
41
42
9/10/2011
Global Forwarding
Local Forwarding
1.<struts-config>
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
2.<action
3. path="/editSubscription"
4. type="org.apache.struts.webapp.example.EditSubscriptionAction"
5. name="subscriptionForm"
6. scope="request"
7. validate="false">
8. <forward
9.
name="failure"
10.
path="/mainMenu.jsp"/>
11. <forward
12.
name="success"
13.
path="/subscription.jsp"/>
14.</action>
<form-beans>
<form-bean
name="logonForm"
type="org.apache.struts.webapp.example.LogonForm" />
</form-beans>
<global-forwards
type="org.apache.struts.action.ActionForward">
<forward
name="logon"
path="/logon.jsp"
redirect="false" />
</global-forwards>
DatTT-DSE-SOICT-HUST
43
DatTT-DSE-SOICT-HUST
44
Vit bi LTV
DatTT-DSE-SOICT-HUST
DatTT-DSE-SOICT-HUST
45
DatTT-DSE-SOICT-HUST
46
<form-bean>
Tn thuc tnh ca lp <Action>
47
48
9/10/2011
V d: submit.jsp
1.<%@
2.<%@
3.<%@
4.<%@
Model: ActionForm
5.<html>
6.<head><title>Submit example</title></head>
7.<body>
1.package submit;
2.import javax.servlet.http.HttpServletRequest;
3.import org.apache.struts.action.*;
9.<html:errors/>
10.<html:form action="submit.do">
11.Last Name: <html:text property="lastName"/><br>
12.Address: <html:textarea property="address"/><br>
13.Sex:
<html:radio property="sex" value="M"/>Male
14.
<html:radio property="sex" value="F"/>Female<br>
15.Married: <html:checkbox property="married"/><br>
16.Age:
<html:select property="age">
17.
<html:option value="a">0-19</html:option>
18.
<html:option value="b">20-49</html:option>
19.
<html:option value="c">50-</html:option>
20.
</html:select><br>
21.
<html:submit/>
DatTT-DSE-SOICT-HUST
22.</html:form>
5. /* Last Name */
6. private String lastName = "Hansen"; // default value
7. public String getLastName() {
8. return (this.lastName);
9. }
10. public void setLastName(String lastName) {
11. this.lastName = lastName;
12. }
49
struts-config.xml: <form-beans>
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. }
DatTT-DSE-SOICT-HUST
21....
50
struts-config.xml: <action-mappings>
<form-bean
name="submitForm"
type="submit.SubmitForm"/>
10. </form-beans>
13.</struts-config>
DatTT-DSE-SOICT-HUST
51
DatTT-DSE-SOICT-HUST
52
2.1.5. Action
(LTV t vit)
53
DatTT-DSE-SOICT-HUST
54
9/10/2011
Lp Action
DatTT-DSE-SOICT-HUST
55
DatTT-DSE-SOICT-HUST
Lp Action l g?
X l request
Thc hin cc x l logic nghip v
DatTT-DSE-SOICT-HUST
58
K tha org.jakarta.struts.action.Action
Override
57
DatTT-DSE-SOICT-HUST
ng dng cn
56
V d Action: Logon
JSP
Tile definition
Velocity template
Action khc
c gi bi controller
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception;
DatTT-DSE-SOICT-HUST
59
DatTT-DSE-SOICT-HUST
60
10
9/10/2011
Deprecated
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException;
DatTT-DSE-SOICT-HUST
61
DatTT-DSE-SOICT-HUST
V d: lp Action
1.package submit;
2.import javax.servlet.http.*;
3.import org.apache.struts.action.*;
4.public final class SubmitAction extends Action {
5.
6.
7.
8.
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.}
DatTT-DSE-SOICT-HUST
Ch nn to cc lp Action n gin
63
V d 2: Lp Action
DatTT-DSE-SOICT-HUST
64
struts-config.xml: ActionMapping
1.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. }
DatTT-DSE-SOICT-HUST
23.}
62
13.</struts-config>
DatTT-DSE-SOICT-HUST
66
11
9/10/2011
Cc lp Action xy dng sn
ForwardAction
DispatchAction
(Advanced Struts)
DatTT-DSE-SOICT-HUST
67
DatTT-DSE-SOICT-HUST
Model Components
Model components
JavaBeans
Enterprise JavaBeans
POJO's
JDO
JDBC
...
DatTT-DSE-SOICT-HUST
69
DatTT-DSE-SOICT-HUST
Model Components
68
JavaBeans v Scope
70
Model Components
Servlet code
MyCart mycart = new MyCart(...);
request.setAttribute("cart", mycart);
JSP page
<jsp:useBean id="cart" scope="request"
class="com.mycompany.MyApp.MyCart"/>
DatTT-DSE-SOICT-HUST
72
12
9/10/2011
ActionForm Bean
SystemState Bean
BusinessLogic Bean
73
DatTT-DSE-SOICT-HUST
75
76
C th s dng EJB
BuesinessLogic Bean
DatTT-DSE-SOICT-HUST
BusinessLogic Bean
Vi ng dng quy m ln
74
77
DatTT-DSE-SOICT-HUST
78
13
9/10/2011
View components
2.3. Cc thnh phn View
(View Components)
LTV phi cung cp
DatTT-DSE-SOICT-HUST
DatTT-DSE-SOICT-HUST
79
View Components
View
DatTT-DSE-SOICT-HUST
5.<html>
6.<head><title>Submit example</title></head>
7.<body>
<html:text property="username"/>;
DatTT-DSE-SOICT-HUST
82
V d: submit.jsp
Vi Struts, ch cn
DatTT-DSE-SOICT-HUST
81
80
83
10.<html:form action="submit.do">
11.Last Name: <html:text property="lastName"/><br>
12.Address: <html:textarea property="address"/><br>
13.Sex:
<html:radio property="sex" value="M"/>Male
14.
<html:radio property="sex" value="F"/>Female<br>
15.Married: <html:checkbox property="married"/><br>
16.Age:
<html:select property="age">
17.
<html:option value="a">0-19</html:option>
18.
<html:option value="b">20-49</html:option>
19.
<html:option value="c">50-</html:option>
20.
</html:select><br>
21.
<html:submit/>
DatTT-DSE-SOICT-HUST
22.</html:form>
84
14
9/10/2011
V d: submit.jsp
1.<logic:present name="lastName" scope="request">
2.Hello
3.<logic:equal name="submitForm" property="age" value="a">
4. young
5.</logic:equal>
6.<logic:equal name="submitForm" property="age" value="c">
7. old
8.</logic:equal>
9.<bean:write name="lastName" scope="request"/>
10.</logic:present>
3. web.xml
11.</body>
12.</html>
DatTT-DSE-SOICT-HUST
85
1.<!DOCTYPE web-app
2. PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
3. "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
web.xml cha:
86
V d: web.xml
DatTT-DSE-SOICT-HUST
87
4.<web-app>
5. <display-name>Advanced J2EE Programming Class Sample App</display-name>
6.
7. <!-- Standard Action Servlet Configuration (with debugging) -->
8. <servlet>
9. <servlet-name>action</servlet-name>
10. <servlet-class>
11. org.apache.struts.action.ActionServlet
12. </servlet-class>
13. <init-param>
14.
<param-name>application</param-name>
15.
<param-value>ApplicationResources</param-value>
16. </init-param>
17. <init-param>
18.
<param-name>config</param-name>
19.
<param-value>/WEB-INF/struts-config.xml</param-value>
20. </init-param>
21.</servlet>
DatTT-DSE-SOICT-HUST
88
V d: web.xml
1. <!-- Standard Action Servlet Mapping -->
2. <servlet-mapping>
3. <servlet-name>action</servlet-name>
4. <url-pattern>*.do</url-pattern>
5. </servlet-mapping>
6.
7. <!-- Struts Tag Library Descriptors -->
8. <taglib>
9. <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
10. <taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
11. </taglib>
12. <taglib>
13. <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
14. <taglib-location>/WEB-INF/struts-html.tld</taglib-location>
15. </taglib>
16. <taglib>
17. <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
18. <taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
19. </taglib>
20.</web-app>
DatTT-DSE-SOICT-HUST
4. Internationalization
89
DatTT-DSE-SOICT-HUST
90
15
9/10/2011
Internationalization
M rng ca java.util.ResourceBundle
org.apache.struts.util.MessageResources
DatTT-DSE-SOICT-HUST
91
Resource files
prompt.hello=Hello
MyApplication_xx.properties
92
1.errors.header=<h4>Validation Error(s)</h4><ul>
2.errors.footer=</ul><hr>
DatTT-DSE-SOICT-HUST
V d: ApplicationResource.properties
MyApplication.properties
prompt.hello=Bonjour
DatTT-DSE-SOICT-HUST
93
DatTT-DSE-SOICT-HUST
94
V d: web.xml
1. <servlet>
2. <servlet-name>action</servlet-name>
3. <servlet-class>
4. org.apache.struts.action.ActionServlet
5. </servlet-class>
6. <init-param>
7.
<param-name>application</param-name>
8.
<param-value>
9.
com.mycompany.mypackage.MyApplication
10. </param-value>
11. </init-param>
12. <!-- ... -->
13.</servlet>
DatTT-DSE-SOICT-HUST
5. (Input) Validation
95
DatTT-DSE-SOICT-HUST
96
16
9/10/2011
validate() method
Ty chn
C php:
input=/errorpage.jsp
DatTT-DSE-SOICT-HUST
DatTT-DSE-SOICT-HUST
DatTT-DSE-SOICT-HUST
100
struts-config.xml: Validation
L c ch c s dng tr v cc li khi
thc hin validate input
ng gi li cc li
99
Lp ActionError
98
97
Ty chn
DatTT-DSE-SOICT-HUST
c gi bi controller servlet
validate=true
ActionErrors l mt tp cc ActionError
DatTT-DSE-SOICT-HUST
101
13.</struts-config>
DatTT-DSE-SOICT-HUST
102
17
9/10/2011
ActionForm
V d: submit.jsp
1.<%@ page language="java" %>
2.<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
3.<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
4.<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
5.<html>
6.<head><title>Submit example</title></head>
7.<body>
8.<h3>Example Submit Page</h3>
9.<html:errors/>
10.<html:form action="submit.do">
11.Last Name: <html:text property="lastName"/><br>
12.Address: <html:textarea property="address"/><br>
13.Sex:
<html:radio property="sex" value="M"/>Male
14.
<html:radio property="sex" value="F"/>Female<br>
15.Married: <html:checkbox property="married"/><br>
16.Age:
<html:select property="age">
17.
<html:option value="a">0-19</html:option>
18.
<html:option value="b">20-49</html:option>
19.
<html:option value="c">50-</html:option>
20.
</html:select><br>
21.
<html:submit/>
DatTT-DSE-SOICT-HUST
22.</html:form>
103
104
6. X l li (Error Handling)
DatTT-DSE-SOICT-HUST
105
DatTT-DSE-SOICT-HUST
CustomExceptionHandler (1.1)
T nh ngha lp ExceptionHandler, c
gi n khi phng thc execute() ca 1
Action nm ra 1 Exception
L lp con ca
org.apache.struts.action.ExceptionHandler
Phng thc execute() phi x l Exception v
tr v 1 i tng ActionForward thng bo
vi Struts s forward n u tip
handler="com.yourcorp.ExceptionHandler"/>
</global-exceptions>
DatTT-DSE-SOICT-HUST
106
107
com.yourcorp.ExceptionHandler.execute() s
c gi khi c bt k mt IOException no
c nm ra bi mt
Action
DatTT-DSE-SOICT-HUST
108
18
9/10/2011
CustomExceptionHandler (1.1)
1.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) {
<action ...>
<exception
key="some.key"
type="java.io.IOException"
handler="com.yourcorp.ExceptionHandler"/>
</action>
DatTT-DSE-SOICT-HUST
V d: nm ra mt Exception
109
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. }
DatTT-DSE-SOICT-HUST
23.}
110
<global-exceptions>
<exception handler=
"com.cavaness.storefront.CustomizedExceptionHandler"
key="global.error.message"
path="/error.jsp"
scope="request"
type="java.lang.Exception"/>
<exception handler=
"com.cavaness.storefront.SecurityExceptionHandler"
key="security.error.message"
path="/login.jsp"
scope="request"
type="com.cavaness.storefront.SecurityException"/>
DatTT-DSE-SOICT-HUST
111
</global-exceptions>
112
DatTT-DSE-SOICT-HUST
struts-config.xml: ActionMapping
7. View Selection
Tr v i tng ActionForward, ng vi
outcome no.
DatTT-DSE-SOICT-HUST
113
13.</struts-config>
DatTT-DSE-SOICT-HUST
114
19
9/10/2011
V d: Action Class
1.package hansen.playground;
2.import javax.servlet.http.*;
3.import org.apache.struts.action.*;
4.public final class SubmitAction extends Action {
5.
6.
7.
8.
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.}
DatTT-DSE-SOICT-HUST
115
20