Académique Documents
Professionnel Documents
Culture Documents
.
Disclaimer & Acknowledgments
?
Even though Sang Shin is a full-time employees of Sun Microsystems, the
contents here are created as their own personal endeavor and thus does
not reflect any official stance of Sun Microsystems.
?
Sun Microsystems is not responsible for any inaccuracies in the contents.
?
Acknowledgments:
– Struts' user's guide is also used in creating slides and speaker notes
2
Revision History
? 11/10/2003: version 1: created by Sang Shin
? Things to do
– Speaker notes need to be added to some slides
3
Agenda
? Struts tag libraries
? Struts and JSTL
? Struts-EL
This is the agenda. We will learn first what is and why Struts. Then we
will look into Struts architecture as one that follows MVC pattern.
Struts comes with extensive tag library so we will learn how to use them.
We will also learn how internationalization is done in Struts. We will
learn how input form validation and error handling can be done. At the
end, I will talk about “Struts console” tool that you can use to graphically
edit Struts configuration file.
Struts Tag Libraries
5
Tag Libraries Overview
? Number of taglibs included as part of Struts
– Usage is not required, but helpful
? Bean tags
– Tags for accessing Beans and their properties
? Html tags
– Form bridge between JSP view and other components
? Logic tags
– Provides presentation logic tags that eliminate need for scriptlets
? Template tags (Tiles in v1.1)
– Tags to form JSP templates that include parameterized content
? Nested Tags (v1.1)
– Allows for object hierarchy
– Helpful for rendering lists of lists
6
Access to Tag Libraries
? All tag libraries are defined in web.xml using
<taglib> element
<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib> 7
…
This tag library contains tags useful in accessing beans and their
properties, as well as defining new beans (based on these
accesses) that are accessible to the remainder of the page via
scripting variables and page scope attributes. Convenient
mechanisms to create new beans based on the value of request
cookies, headers, and parameters are also provided.
Attributes of Bean Tags
? id - define a bean
? name - refer to an existing bean (the value is
either the value of an id attribute in a previous
tag, or is found in application, session,
request, or page scope)
? property - a property from a bean
? scope - scope to search for the bean. If
scope is not specified then the bean is
searched for in page, request, session and
application order
10
This tag library contains tags useful in accessing beans and their
properties, as well as defining new beans (based on these
accesses) that are accessible to the remainder of the page via
scripting variables and page scope attributes. Convenient
mechanisms to create new beans based on the value of request
cookies, headers, and parameters are also provided.
Bean Tags
? <bean:define/>
? <bean:write/>
? <bean:message/>
? <bean:include/>
? <bean:resource/>
? <bean:cookie>
? <bean:header>
? <bean:parameter>
? <bean:size>
11
.
<bean:define/>
? For creating variables from beans and
properties
– Without it, you would have to create Java code-
based scripting variables in your JSP pages
? The variables are used later in the JSP
page
? For exposing Java objects (i.e. Collections)
that are created in a Action class to a JSP
12
.
Examples: <bean:define/>
? <bean:define id="string" value="Struts in
Javaboutique"/>
– Get a bean with a String constant
? <bean:define id="copy" name="dvd"/>
– Get an existing bean
? <bean:define id="title" name="copy"
property="title"/>
– Get a single property from a bean
13
.
Example1: favorites.jsp (ch 03)
1 <c:forEach var="theColor" items="${FavoritesForm.colors}"
varStatus="loopStatus">
2 <bean:define id="ctr">
3 <c:out value="${loopStatus.index}"/>
4 </bean:define>
5 <br/><html:text property='<%="color["+ctr+"]"%>'/>
6 </c:forEach>
14
17
.
Example1: <bean:write/>
<jsp:useBean id="dvd" class="hansen.playground.DVD"
scope="request"/>
...
<jsp:getProperty name="dvd" property="title"/>
18
.
Example2: submitAction.java
1 public final class SubmitAction extends Action {
2
3 // The execute() method is where you provide your business logic
4 public ActionForward execute(ActionMapping mapping,
5 ActionForm form,
6 HttpServletRequest request,
7 HttpServletResponse response) {
8
9 // Cast ActionForm object to SubmitForm type
10 SubmitForm f = (SubmitForm) form;
11
12 // Retrieve the value of lastname field
13 String lastName = f.getLastName();
14
15 // Translate the lastname to upper case and save it Request scope
16 request.setAttribute("lastName", lastName.toUpperCase());
17
18 // Create and return ActionForward object with "success" outcome
19 return (mapping.findForward("success"));
20 }
21 } 19
22
20
22
.
Example: index.jsp (ch 03)
1 <html:html locale="true">
2 <head>
3 <title><bean:message key="index.title"/></title>
4 <html:base/>
5 </head>
6 <body bgcolor="white">
7 <h2>Struts Chapter 3 Examples</h2>
8
9 <p>
10 <bean:message key="msg.hello"/>
11 </p>
23
24
.
HTML Tags
25
HTML Tags
? Form bridge between JSP view and other
components
? Input forms are important for gathering user-
entered data
? Most of the actions of the HTML taglib involve
HTML forms
? Error messages, hyperlinking,
internationalization
26
28
.
<html:errors/>
? Simplest way to display error messages
– It is expected that ActionErrors is created (either in
the validate() method of an ActionForm class or in
execute() method of an Action class)
? Place the tag anywhere on the page you
want the list of errors to be displayed
? Iterates over the errors writing unescaped
contents to the page
– Messages need to have HTML tags, which are not
desirable
29
.
Example: 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
6 <html>
7 <head><title>Submit example</title></head>
8 <body>
9
10 <h3>Example Submit Page</h3>
11
12 <html:errors/>
13
14 <html:form action="submit.do">
15 Last Name: <html:text property="lastName"/><br>
16 Address: <html:textarea property="address"/><br>
17 Sex: <html:radio property="sex" value="M"/>Male
18 <html:radio property="sex" value="F"/>Female<br>
19 Married: <html:checkbox property="married"/><br>
20 Age: <html:select property="age">
21 <html:option value="a">0-19</html:option>
22 <html:option value="b">20-49</html:option>
23 <html:option value="c">50-</html:option> 30
24 </html:select><br>
Example: ApplicationResources.properties
1 errors.header= <h4> Validation Error(s)</h4><ul>
2
3 error.lastName=<li>Enter your last name
4 error.address= <li>Enter your address
5 error.sex= <li>Enter your sex
6 error.age=<li>Enter your age
7 error.birthYear=<li>Enter the year you were born between 1900 and 2004
inclusive
8
9 errors.footer= </ul><hr>
31
<html:messages/>
? Corrects the problem of <html:errors/>
– Allows you to keep HTML tags in JSP pages not in
the resource file
? By default, it looks for error messages
stored in the request scope
? The id attribute defines the name of the
scripting variable used to expose the error
message text
32
.
Logic Tags
33
Logic Tags
? Provides presentation logic tags that
eliminate need for scriptlets
? Value comparisons
Include: = != <= >= < >
? Substring matching
– match, notmatch
? Presentation logic
– forward, redirect
? Collections
– iterate 34
35
.
<logic:present/> &
<logic:notPresent> tags
? The body of the <logic:present/> tag is
evaluated whenever the JavaBean, or its
property, is present within the JSP page
? Attributes for evaluation
– name
– parameter
– cookie
– header
– property
36
.
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>
< 37
<logic:equal/> & <logic:notEqual>
tags
? Checks against a specific value in a bean
? Assumes the bean exists
– Exception occurs if not
? <logic:equal/> tag compares the bean's
toString() value aganst the value property
? If property attribute is specified, then the
value attribute is compared against the
bean's property
38
.
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>
< 39
Usage Example: <logic:equal/> &
<logic:notEqual> tags
? You want to present different messages or
buttons on a page depending upon the type
of action you migt perform
? Example:
– Depending what a user wants to do (mode) –
view, edit, or delete, you want to present different
set of buttons
– View mode: Show only view button
– Edit mode: Show view and edit buttons
– Delete mode: Show only delete button
40
.
Example: subscription.jsp (ch 13)
1 <html:html>
2 <head>
3 <logic:equal name="SubscriptionForm" property="action"
4 scope="request" value="Create">
5 <title><bean:message key="subscription.title.create"/></title>
6 </logic:equal>
7 <logic:equal name="SubscriptionForm" property="action"
8 scope="request" value="Delete">
9 <title><bean:message key="subscription.title.delete"/></title>
10 </logic:equal>
11 <logic:equal name="SubscriptionForm" property="action"
12 scope="request" value="Edit">
13 <title><bean:message key="subscription.title.edit"/></title>
14 </logic:equal>
15 <html:base/>
< 41
Template Tags
42
Template Tags
? Templates are JSP pages that include
parameterized content
? Useful for creating dynamic JSP templates
for pages that share a common format
? Functionality provided is similar to what
can be achieved using the standard JSP
include directive, but these tags allow for
dynamic rather than static content
43
Template Tags
? Three template tags work in an
interrelated function:
– Get - Gets the content from request scope
that was put there by a put tag.
– Insert - Includes a template
– Put - Puts content into request scope
44
Template sample (insert/put)
<template:insert template='/layout.jsp'>
<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>
45
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>
46
Struts and
JSTL
47
When to JSTL in your Struts
application?
? Developers should evaluate when to use
the JSTL
? Many of the Struts taglib features are now
available in the JSTL
? It’s simple: If the tag exists in the JSTL –
use it
? Continue using the Struts tags where
appropriate, they will continue to be
supported
48
50
Struts EL Extension
? Extension of the Struts tag library
? Uses the expression evaluation engine in the
Jakarta Taglibs implementation of the JSP
Standard Tag Library (version 1.0) to evaluate
attribute values
? Some of the Struts tags were not ported to this
library
– their functionality was entirely supplied by the
JSTL
In addition, some of the Struts tags were not ported to this library, as it was
determined that their functionality was entirely supplied by the JSTL. These
particular Struts tags, and the reason for their non-porting will be described in the
documentation for this library.
In order to fully understand the correct utilization of this library, you must
understand the use and operation of the Struts tag library, and the use and operation
of the JavaServer Pages Standard Tag Library (hereafter called the "JSTL"), along
with the expression language (sometimes called the "EL") used for evaluating
attribute values.
The Struts-EL tag library requires the use of the Struts tag library, and the Java
Server Pages Standard Tag Library. It is not necessary for JSP pages using the Struts-
EL tag library to also use the Struts tags or the JSTL tags, but the Struts and JSTL tag
libraries need to be part of the application utilizing the Struts-EL tag library.
This is because the Struts-EL tag classes are all subclasses of Struts tag classes, and
their implementation uses classes provided by the JSTL.
Tag Mapping
? Every Struts tag that provides a feature that
is not covered by the JSTL (1.0) library is
mapped into the Struts-EL library
? Bean Tag Library Tags NOT Implemented
in Struts-EL
– cookie (in Struts): c:set, EL (in JSTL)
– define (in Struts), c:set, EL (In JSTL)
52
In implementing the Struts-EL library, every Struts tag that provides a feature that
is not covered by the JSTL (1.0) library is mapped into the Struts-EL library. This
section reviews which Struts tags are NOT implemented in the Struts-EL library,
and which JSTL tags provide that feature.
Many of the non-porting decisions were based on the fact that the JSTL
expression language itself provides the same functionality. In those cases, in
addition to a possible JSTL tag name, the symbol "EL" will be listed.
How to use Struts EL
? Struts
– <bean:message key='<%= stringvar %>'/>
? Struts EL
– <bean-el:message key="${stringvar}"/>
53
The base Struts tag library contains tags which rely on the evaluation of
"rtexprvalue"s (runtime scriptlet expressions) to evaluate dynamic attribute
values. For instance, to print a message from a properties file based on a resource
key, you would use the bean:write tag, perhaps like this:
This assumes that stringvar exists as a JSP scripting variable. If you're using the
Struts-EL library, the reference looks very similar, but slightly different, like this:
<bean-el:message key="${stringvar}"/>
Best Practice
Guidelines
54
Follow Good MVC Practice
? JSP pages must “know” as little as possible
about the back-end architecture
? JSP page should only concern itself with
rendering the view and not manipulating
any data logic
55
.
Passion!
56