Professional Documents
Culture Documents
Struts Basics
Struts Basics
Struts Basics
12/03/2003
Sang Shin
sang.shin@sun.com
www.javapassion.com
Java Technology Evangelist
Sun Microsystems, Inc.
2
12/03/2003
?
?
Struts' user's guide is also used in creating slides and speaker notes
12/03/2003
Revision History
?
?
12/03/2003
Agenda
?
?
?
?
?
?
Controller
Model
View
12/03/2003
What is Struts?
12/03/2003
Jakarta Struts
?
http://jakarta.apache.org/struts/
12/03/2003
What is Struts?
?
?
?
?
XML parsing
JavaBean property population
Internationalization
12/03/2003
12/03/2003
Why Struts?
10
12/03/2003
Why Struts?
?
?
?
?
?
?
?
?
?
12/03/2003
Why Struts?
?
?
?
?
?
?
12/03/2003
Struts Architecture
(Quick Overview)
13
12/03/2003
Model-View-Control (model 2)
MVC Design Pattern
1
Request
Redirect
5
Response
(View)
JSP
te
tia
tan
Ins
BROWSER
(Controller)
Servlet
(Model)
Java Bean
Servlet Container
(EIS)
14
12/03/2003
15
12/03/2003
?
?
?
12/03/2003
17
12/03/2003
Determines forwarding/navigation
A request path
Action to act upon the request
18
12/03/2003
Controller
19
12/03/2003
20
12/03/2003
21
12/03/2003
org.apache.struts.action.ActionServlet
Servlet mapping has to be configured in web.xml
22
12/03/2003
Developer Responsibility
?
12/03/2003
Controller Components in
Struts Framework
?
?
Action
Action Mapping
12/03/2003
ActionServlet
(Provided by Framework)
25
12/03/2003
12/03/2003
http://localhost:8080/myApp/foo/someAction.do
27
12/03/2003
28
12/03/2003
12/03/2003
RequestProcessor
(Provided by Framework)
30
12/03/2003
processPath
processLocale
processContent
12/03/2003
processNoCache
processPreprocess
32
12/03/2003
processMapping
processRoles
processActionForm
33
12/03/2003
processPopulate
processValidate
processForward
34
12/03/2003
processInclude
processActionCreate
processActionPerform
35
12/03/2003
processForwardConfig
36
12/03/2003
Action Mapping
(You provide it)
37
12/03/2003
12/03/2003
39
12/03/2003
<form-bean>
40
12/03/2003
<action-mappings>
41
12/03/2003
struts-config.xml: <form-beans>
1
2
3 <!DOCTYPE struts-config PUBLIC
4
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
5
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
6
7 <struts-config>
8
9
<!-- ========== Form Bean Definitions ================= -->
10
<form-beans>
11
12
<form-bean
name="submitForm"
13
type="hansen.playground.SubmitForm"/>
14
15
</form-beans>
42
12/03/2003
struts-config.xml:
<action-mappings>
<!-- ========== Action Mapping Definitions ============ -->
1
<action-mappings>
2
3
<action path="/submit"
4
type="hansen.playground.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
13
</action-mappings>
14
15
</struts-config>
16
43
12/03/2003
<global-forwards>
?
?
12/03/2003
Global Forwarding
1
2
3
4
5
6
7
8
9
10
11
12
13
14
<struts-config>
<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>
45
12/03/2003
Global Forwarding
1
2
3
4
5
6
7
8
9
10
11
12
<action-mappings>
<action
path="/logon"
type="org.apache.struts.webapp.example.LogonAction"
name="logonForm"
scope="request"
input="/logon.jsp"
unknown="false"
validate="true" />
</action-mappings>
</struts-config>
46
12/03/2003
Local Forwarding
1
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>
47
12/03/2003
ActionForm
(You provide it)
48
12/03/2003
Provided by developer
49
12/03/2003
12/03/2003
12/03/2003
Example: submit.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
52
12/03/2003
Model: ActionForm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package hansen.playground;
import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.*;
public final class SubmitForm extends ActionForm {
/* Last Name */
private String lastName = "Hansen"; // default value
public String getLastName() {
return (this.lastName);
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
/* Address */
private String address = null;
public String getAddress() {
return (this.address);
}
public void setAddress(String address) {
this.address = address;
}
...
53
12/03/2003
struts-config.xml: <form-beans>
1
2
3 <!DOCTYPE struts-config PUBLIC
4
"-//Apache Software Foundation//DTD Struts Configuration 1.1//EN"
5
"http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
6
7 <struts-config>
8
9
<!-- ========== Form Bean Definitions ================= -->
10
<form-beans>
11
12
<form-bean
name="submitForm"
13
type="hansen.playground.SubmitForm"/>
14
15
</form-beans>
54
12/03/2003
struts-config.xml: <action-mappings>
<!-- ========== Action Mapping Definitions ============ -->
1
<action-mappings>
2
3
<action path="/submit"
4
type="hansen.playground.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
13
</action-mappings>
14
15
</struts-config>
16
55
12/03/2003
56
12/03/2003
Action
(You create one)
57
12/03/2003
58
12/03/2003
Action Class
?
JSP
Tile definition
Velocity template
Another Action
59
12/03/2003
Handle request
Perform business logic
12/03/2003
Application needs to
Authenticate a User
Establish a User Session
Error handling
Develop a LogonAction
61
12/03/2003
Developer Responsibility:
Action Class
?
?
Extend org.jakarta.struts.action.Action
Override
62
12/03/2003
Invoked by controller
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception;
63
12/03/2003
Deprecated
public ActionForward perform(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException;
64
12/03/2003
65
12/03/2003
66
12/03/2003
?
?
12/03/2003
68
12/03/2003
struts-config.xml: ActionMapping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
12/03/2003
ForwardAction
DispatchAction
We will learn more about these in
Advanced Struts session
70
12/03/2003
Model Components
(You provide them)
71
12/03/2003
Model Components
?
JavaBeans
Enterprise JavaBeans
POJO's
JDO
JDBC
W hatever
72
12/03/2003
73
12/03/2003
Model Components
?
12/03/2003
Model Components
?
Servlet code
?
request.setAttribute("cart", mycart);
JSP page
?
class="com.mycompany.MyApp.MyCart"/>
75
12/03/2003
ActionForm Bean
SystemState Bean
BusinessLogic Bean
12/03/2003
12/03/2003
12/03/2003
SystemState Bean
?
79
12/03/2003
BuesinessLogic Bean
?
80
12/03/2003
BuesinessLogic Bean
?
81
12/03/2003
View Components
(You provide them)
82
12/03/2003
83
12/03/2003
View Components
?
?
?
?
84
12/03/2003
View
?
85
12/03/2003
86
12/03/2003
Example: submit.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
87
12/03/2003
Example: submit.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
88
12/03/2003
web.xml
89
12/03/2003
web.xml includes:
12/03/2003
Example: web.xml
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">
4
5 <web-app>
6
<display-name>Advanced J2EE Programming Class Sample App</display-name>
7
8
<!-- Standard Action Servlet Configuration (with debugging) -->
9
<servlet>
10
<servlet-name>action</servlet-name>
11
<servlet-class>
12
org.apache.struts.action.ActionServlet
13
</servlet-class>
14
<init-param>
15
<param-name>application</param-name>
16
<param-value>ApplicationResources</param-value>
17
</init-param>
18
<init-param>
19
<param-name>config</param-name>
20
<param-value>/WEB-INF/struts-config.xml</param-value>
21
</init-param>
22 </servlet>
91
12/03/2003
Example: web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
12/03/2003
93
12/03/2003
Bean tags
Logic tags
Html tags
12/03/2003
12/03/2003
96
12/03/2003
97
12/03/2003
Bean Tags
98
12/03/2003
Bean Tags
?
?
?
Cookies
Request Headers
Parameters
99
12/03/2003
Example: submit.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
100
12/03/2003
HTML Tags
101
12/03/2003
HTML Tags
?
?
?
12/03/2003
HTML Tags
?
?
?
?
?
?
?
?
?
?
?
checkboxes
hidden fields
password input fields
radio buttons
reset buttons
select lists with embedded option or options items
option
options
submit buttons
text input fields
textareas
103
12/03/2003
Example: submit.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
104
12/03/2003
Logic Tags
105
12/03/2003
Logic Tags
?
Substring matching
Presentation logic
match, notmatch
forward, redirect
Collections
iterate
106
12/03/2003
Example: 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>
11
12 </body>
13 </html>
107
12/03/2003
Template Tags
108
12/03/2003
Template Tags
?
12/03/2003
Template Tags
?
110
12/03/2003
<template:put name='title'
content='CD Manager Logon/>
<template:put name='header' content='/header.jsp' />
<template:put name='content
content='/logonContent.jsp'/>
<template:put name='footer' content='/footer.jsp' />
</template:insert>
111
12/03/2003
layout.jsp
<html>
<head>
<title> <template:get name='title'/> </title>
</head>
<body >
<table>
<tr><td> <template:get name='header'/> </td></tr>
<tr><td> <template:get name='content'/> </td></tr>
<tr><td> <template:get name='footer'/> </td></tr>
</table>
</body>
</html>
112
12/03/2003
Internationalization
113
12/03/2003
Internationalization
?
?
?
org.apache.struts.util.MessageResources
114
12/03/2003
Internationalization:
Developer responsibilities
?
?
12/03/2003
Resource files
?
MyApplication.properties
prompt.hello=Hello
MyApplication_xx.properties
prompt.hello=Bonjour
116
12/03/2003
Example:
ApplicationResource.properties
1
2
3
4
5
6
7
errors.header=<h4>Validation Error(s)</h4><ul>
errors.footer=</ul><hr>
error.lastName=<li>Enter your last name
error.address=<li>Enter your address
error.sex=<li>Enter your sex
error.age=<li>Enter your age
117
12/03/2003
Example: web.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>
org.apache.struts.action.ActionServlet
</servlet-class>
<init-param>
<param-name>application</param-name>
<param-value>
com.mycompany.mypackage.MyApplication
</param-value>
</init-param>
<!-- ... -->
</servlet>
118
12/03/2003
(Input) Validation
119
12/03/2003
Validation: Developer
responsibilities (Struts 1.0)
?
validate=true
input=/errorpage.jsp
optional
120
12/03/2003
validate() method
?
Optional
Syntax
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request);
121
12/03/2003
In Login application
122
12/03/2003
validate() method
?
12/03/2003
ActionError Class
?
?
?
124
12/03/2003
struts-config.xml: Validation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
12/03/2003
ActionForm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
..
}
12/03/2003
Example: submit.jsp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
127
12/03/2003
128
12/03/2003
Error Handling
129
12/03/2003
130
12/03/2003
?
?
12/03/2003
CustomExceptionHandler (1.1)
?
Subclass
org.apache.struts.action.ExceptionHandler
Your execute() method should process the
Exception and return an ActionForward object to
tell Struts where to forward to next
Example
12/03/2003
com.yourcorp.ExceptionHandler.execute()
will be called when any IOException is
thrown by an Action
133
12/03/2003
CustomExceptionHandler (1.1)
?
134
12/03/2003
135
12/03/2003
<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"/>
</global-exceptions>
136
12/03/2003
View Selection
137
12/03/2003
In struts-config file,
138
12/03/2003
struts-config.xml: ActionMapping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
12/03/2003
140
12/03/2003
Tools
141
12/03/2003
Struts Console
?
Visual editor of
http://www.jamesholmes.com/struts/console/
142
12/03/2003
Struts Console
143
12/03/2003
Struts Console
144
12/03/2003
Struts Console
145
12/03/2003
Struts Console
146
12/03/2003
Struts Console
147
12/03/2003
Struts Console
148
12/03/2003
Struts Console
149
12/03/2003
Struts Console
150
12/03/2003