Professional Documents
Culture Documents
Introduction to Struts
STRUTS
Objectives
Course
Overview
Unit 1 - Model-View-Controller Design
Pattern
Unit 2 - Model Components
Unit 3 - View Components
Unit 4 - Controller Components
Unit 5 - Tag Libraries
Project Refinery, Inc.
STRUTS
Objectives
Unit
Model-View-Controller Design
Pattern
Unit 1
STRUTS
Central
flow
Controller delegates to appropriate
handler
Handlers are tied to model components
Model encapsulates business logic
Control forwarded back through the
Controller to the appropriate View
Project Refinery, Inc.
STRUTS
STRUTS
3
Servlet
controller (Controller)
Java Server Pages (View)
Application Business Logic (Model)
Controller
STRUTS
Configuration
request path
Object type to act upon the request
Model Components
Unit 2
STRUTS
Model
Model Components
Internal
JavaBeans
Enterprise
JavaBeans
10
STRUTS
Model Components
11
STRUTS
Model Components
ActionForm Beans
12
STRUTS
Model Components
13
STRUTS
Model Components
Continued
14
STRUTS
Model Components
15
STRUTS
Model Components
16
STRUTS
Model Components
17
STRUTS
Model Components
18
View Components
Unit 3
19
STRUTS
View Components
Internationalized Messages
Struts builds upon Java platform to provide
assistance for building internationalized and localized
applications
STRUTS
View Components
ApplicationResources.properties
Contains
ApplicationResources_xx.properties
Contains
21
STRUTS
Forms
View Components
HTML
22
STRUTS
View Components
23
STRUTS
View Components
24
STRUTS
View Components
25
STRUTS
View Components
checkboxes
hidden fields
password input fields
radio buttons
reset buttons
select lists
options
submit buttons
text input fields
textareas
Project Refinery, Inc.
26
STRUTS
View Components
27
STRUTS
View Components
28
STRUTS
View Components
29
STRUTS
Page
View Components
The
30
STRUTS
View Components
31
Controller Components
Unit 4
32
STRUTS
Controller Components
Struts
33
STRUTS
Controller Components
34
STRUTS
Action
Controller Components
Classes:
The
35
STRUTS
Controller Components
The
36
STRUTS
Controller Components
37
STRUTS
Controller Components
38
STRUTS
Controller Components
39
STRUTS
Controller Components
40
STRUTS
Controller Components
41
STRUTS
Controller Components
<form-beans>
This section contains your form bean
definitions. You use a <form-bean> element for
each form bean, which has the following
important attributes:
42
STRUTS
Controller Components
<action-mappings>
This section contains your action definitions. You
use an <action> element for each of your actions
you would like to define. Each action element has
requires the following attributes to be defined:
43
STRUTS
Controller Components
One more section of good use is the <datasources> section, which specifies data sources
that your application can use.This is how you
would specify a basic data source for your
application inside of struts-config.xml:
<struts-config>
<data-sources>
<data-source autoCommit="false"
description="Example Data Source Description"
driverClass="org.postgresql.Driver" maxCount="4"
minCount="2" password="mypassword"
url="jdbc:postgresql://localhost/mydatabase"
user="myusername"/>
</data-sources>
</struts-config>
Project Refinery, Inc.
44
STRUTS
Controller Components
The
45
Tag Libraries
Unit 5
46
STRUTS
Tag Libraries
HTML
Tags
Bean Tags
Logic Tags
Template Tags
Custom Tags
47
HTML Tags
48
HTML Tags
button
cancel
checkboxes
file
hidden
image
multibox
password input fields
radio buttons
reset buttons
textareas
49
50
51
Bean Tags
52
Bean Tags
Tag Name
Description
cookie
Define a scripting variable based on the value(s) of the specified request cookie.
define
Define a scripting variable based on the value(s) of the specified bean property.
header
Define a scripting variable based on the value(s) of the specified request header.
include
Load the response from a dynamic application request and make it available as a bean.
message
page
parameter
resource
size
struts
write
Render the value of the specified bean property to the current JspWriter.
53
54
Logic Tags
The
55
Logic Tags
56
Logic Tags
57
Logic Tags
Tag Name
Description
empty
Evaluate the nested body content of this tag if the requested variable is either null or an empty string.
equal
Evaluate the nested body content of this tag if the requested variable is equal to the specified value.
forward
greaterEqual
Evaluate the nested body content of this tag if requested variable is greater than or equal to specified value.
greaterThan
Evaluate the nested body content of this tag if the requested variable is greater than the specified value.
iterate
Repeat the nested body content of this tag over a specified collection.
lessEqual
Evaluate the nested body content of this tag if requested variable is greater than or equal to specified value.
lessThan
Evaluate the nested body content of this tag if the requested variable is less than the specified value.
match
messagesNotPresent
messagesPresent
Evaluate the nested body content of this tag if specified value is an appropriate substring of requested variable.
Generate the nested body content of this tag if the specified message is not present in this request.
Generate the nested body content of this tag if the specified message is present in this request.
notEmpty
Evaluate the nested body content of this tag if the requested variable is neither null nor an empty string.
notEqual
Evaluate the nested body content of this tag if the requested variable is not equal to the specified value.
notMatch
Evaluate the nested body content of tag if specified value not an appropriate substring of requested variable.
notPresent
Generate the nested body content of this tag if the specified value is not present in this request.
present
Generate the nested body content of this tag if the specified value is present in this request.
redirect
58
59
Template Tags
The
60
Template Tags
Insert
61
Custom Tags
<%@ taglib uri="WEB-INF/imagebroker.tld" prefix="broker" %>
<table width=750 cellspacing=0 cellpadding=2 border=2 >
<tr>
<td><broker:form
lob='<%=test.getLob()%>'
unitnbr='<%=test.getUnitnbr()%>'
userid='<%=test.getUserid()%>' >
<broker:doctype value="Invoice"/>
<broker:keyword name="CompanyNbr" value="55555"/>
<broker:keyword name="PONbr" value="M12345"/>
<broker:constraint name="FromDate" value="02/02/2002"/>
<broker:constraint name="ToDate" value="02/28/2002"/>
Image Broker Link Test
</broker:form>
</td>
</tr>
</table>
Project Refinery, Inc.
62
</attribute>
</tag>
63
Hashtable ht = null;
String keyword_count = null;
int iCnt = 0;
HttpServletRequest request = (HttpServletRequest)
pageContext.getRequest();
ht = (Hashtable) request.getAttribute("keyword_parms");
keyword_count = (String)
request.getAttribute("queryobject_count");
iCnt ++;
ht.put("QueryObject" + iCnt, value);
request.setAttribute("keyword_parms", ht);
request.setAttribute("queryobject_count", new String(new
Integer(iCnt).toString()));
return EVAL_PAGE;
}
Project Refinery, Inc.
64
65
developer's responsibility is to
create an XML file named strutsconfig.xml, and place it in the WEB-INF
directory of your application. This format
of this document is constrained by it's
definition in "struts-config_1_0.dtd". The
outermost XML element must be
<struts-config>.
66
67
Struts-config.xml
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration
1.0//EN"
"http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd">
<struts-config>
<!-- ========== Form Bean Definitions =================================== -->
<form-beans>
<form-bean
</form-beans>
<!-- ========== Global Forward Definitions ============================== -->
<global-forwards>
<forward
name="start"
path="/index.html"/>
</global-forwards>
<!-- ========== Action Mapping Definitions ============================== -->
<action-mappings>
<action
path="/CryptForm" type="com.pri.imagebrokerWeb.CryptAction"
name="CryptForm"
<forward name="encrypt"
scope="request"
input="/pgCrypt.jsp">
path="/pgCryptDisplay.jsp"/>
</action>
</action-mappings>
</struts-config>
68
69
Web.xml File
The
70
Web.xml File
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application
2.2//EN"
"http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">
<web-app id="WebApp">
<display-name>imagebrokerWeb</display-name>
<!-- Action Servlet Configuration -->
<servlet id="Servlet_1">
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>application</param-name><param-value>imagebrokerWeb</paramvalue>
</init-param>
<init-param>
<param-name>config</param-name><param-value>WEB-INF/strutsconfig.xml</param-value>
</init-param>
</servlet>
Project Refinery, Inc.
71
72
<taglib>
<taglib-uri>WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
</web-app>
Project Refinery, Inc.
73
74
Application.properties File
error.cryptvalue.required=<li>You must enter some text.</li>
error.lob.required=<li>You must enter the Line of Business.</li>
error.unitnbr.required=<li>You must enter the Unit Number.</li>
error.onbase_dns.required=<li>You must enter the OnBase DNS.</li>
imagebroker.linkname=Project Refinery, Inc.
imagebroker.title=pri Image Broker
imagebrokerlink.title=pri Image Broker Link Test
imagelocationlist.title=Image Location List
imagelocationdetail.title=Image Location Detail
imagelocationinsert.title=Image Location Insert
errors.header=
errors.footer=
75
Resources
Unit 9
76
Resources
Main
http://jakarta.apache.org/struts/index.html
Struts
http://jakarta.apache.org/struts/userGuide/index.html
User Guide
http://jakarta.apache.org/struts/resources.html
http://www.husted.com/struts/
77