StrutsBasics Speakernoted

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 151

12/03/2003

Struts Basics

The goal of this session is to learn the basic concept of Struts and
its architecture.
12/03/2003

Sang Shin
sang.shin@sun.com
www.javapassion.com
Java™ Technology Evangelist
Sun Microsystems, Inc.
2
12/03/2003

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:
– I borrowed from presentation slides from the following sources

? “ Web application frameworks” codecamp material authored by


Doris Chen of Sun Microsystems
? “ Using the Struts framework” presentation material from Sue
Spielman of Switchback Software
(sspielman@switchbacksoftware.com)
? “ Struts” presentation by Roger W Barnes of Project Refinery, Inc.
? Pictures from “Struts 1.1” presentation by Chuck Cavaness
– Struts' user's guide is also used in creating slides and speaker notes
– Source code examples are from Keld Hansen

3
12/03/2003

Revision History
?
11/10/2003: version 1: created by Sang Shin
? Things to do
– Speaker notes need to be added to some slides
– Contents still need to be polished

4
12/03/2003

Agenda
? What is and Why Struts?
? Struts architecture
– Controller
– Model
– View
? Struts tag library
? Internationalization
? Validation and error handling
? Tools
5

This is the agenda. We will learn what is and why Struts first. 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 validation and error handling can be done. At the end we will
learn some of the Struts utility classes and J2EE patterns useed.
12/03/2003

What is Struts?

So what is Struts?
12/03/2003

Jakarta Struts
? Struts is an open source Web application
framework developed as Apache Jakarta
project
– http://jakarta.apache.org/struts/

I assume most of you know Struts is an open source web application framework developed as
Apache Jakarta project.
12/03/2003

What is Struts?
? Model-View-Controller (MVC) framework
? Used for constructing web applications
using Servlets and JSPs
? Pattern oriented
– Singleton, composition, delegate
– Easy to use and learn
? Includes JSP custom tag libraries
? Utility classes that support:
– XML parsing
– JavaBean property population
– Internationalization 8

Let me give you a quick high-level overview on what Struts is from a technical
standpoint.

Struts is based on MVC (Model View Controller) framework. It is used to build


Web applications based on Servlet and JSP. So Struts application is a genuine
Web application.

Struts leverages J2EE design patterns that have been identified quite useful in
building J2EE applications.

Struts has its own set of custom tag libraries. Struts also support utility classes.
12/03/2003

Struts allows Web Application


Developers to ...
? fashion their JSP/Servlet web applications
using the MVC design pattern
? leverage ready-to-usable framework objects
through xml configuration files
? leverage built-in design patterns in the
framework
? leverage extra features such as input
validation, internationalization
9

So why do you want to use Struts as a Web application developer?


There are a few reasons. First, it let you fashion your JSP/Servlet
applications to use MVC design pattern. Since we talked about what
and why you want to use MVC design pattern in previous class, I am
not going to talk about it here.

Struts also leverages ready-to-usable framework objects such as


Action class or ActionMapping through XML configuration files. And
we will talk about these framework objects in detail in the later part of
this presentation.

Struts also leverages “time-proven” design patterns internally in the


framework. So you don't have to create your design patterns.

Struts also provides useful features such as input form validation,


internationalization as built-in features.
12/03/2003

Why Struts?

10

Now let's talk about benefits of using Struts in a bit more


detail.
12/03/2003

Why Struts?
? Takes much of the complexity out of building your
own MVC framework
? Encourages good design practice and modeling
? Easy to learn and use
? Feature-rich
? Many supported 3rd-party tools
? Flexible and extensible
? Large user community
? Stable and mature
? Open source 11

So we know you want to build your Web application based on MVC framework. Now
instead of building your own MVC, you can use Struts.

Struts also encourages good design practices and modeling because the framework is
designed with “time-proven” design patterns. Struts is relatively simple, thus is easy to learn
and use. It support many useful features.

Another reason Struts is popular is there are many 3rd-party tools. Struts is designed to be
extensible and flexible. Ever since its advent, Struts enjoyed a very large following from
developer community. Struts has been around quite a while and is considered very stable
and mature. And of course, being a open source project helps Struts' adoption.
12/03/2003

Why Struts?
? Integrates well with J2EE
? Good taglib support
? Works with existing web apps
? Easy to retain form state
? Unified error handling (via ActionError)
? Easily construct processes spanning multiple
steps
? Clear delineation of responsibility makes long
term maintenance easier (more modular)
12

Struts is very well integrated with J2EE. For example, many people use EJB as
model objects. Struts provides good tag libraries. It is relatively easy to adapt
existing Web applications to the Struts framework.

Struts allows capturing input form data into JavaBean objects called ActionForms.
It also provides standard error handling.
12/03/2003

Struts Architecture
(Quick Overview)

13
12/03/2003

Model-View-Control (model 2)
MVC Design Pattern

1
Request
(Controller)
Servlet
BROWSER

Ins
tan
Redirect 3 2

tia
te
5 (View) (Model )
Java Bean
Response JSP 4

Servlet Container (EIS)

14

This is a rehash of what we learned in “MVC pattern and


framework” session.

A Model 2 architecture introduces a controller servlet between the


browser and the JSP pages.

The controller centralizes the logic for dispatching requests to the


next view based on the request URL, input parameters, and
application state. The controller also handles view selection, which
de-couples JSP pages and servlets from one another.

Model 2 applications are easier to maintain and extend, because


views do not refer to each other directly. The Model 2 controller
servlet provides a single point of control for security and logging,
and often encapsulates incoming data into a form usable by the
back-end MVC model.

For these reasons, the Model 2 architecture is recommended for


most web applications.
12/03/2003

How Struts Works

15

When initialized, the controller parses a configuration file (struts-config.xml) and


uses it to deploy other control layer objects. Together, these objects form the
Struts Configuration. The Struts Configuration defines (among other things) the
ActionMappings [org.apache.struts.action.ActionMappings] for an application.

The Struts controller servlet consults the ActionMappings as it routes HTTP


requests to other components in the framework. Requests may be forwarded to
JavaServer Pages or Action [org.apache.struts.action.Action] subclasses
provided by the Struts developer. Often, a request is first forwarded to an Action
and then to a JSP (or other presentation page). The mappings help the controller
turn HTTP requests into application actions.

The Action object can handle the request and respond to the client (usually a
Web browser) or indicate that control should be forwarded elsewhere. For
example, if a login succeeds, a login action may wish to forward the request onto
the mainMenu page.

Action objects have access to the application's controller servlet, and so have
access to that servlet's methods. When forwarding control, an Action object can
indirectly forward one or more shared objects, including JavaBeans, by placing
them in one of the standard contexts shared by Java Servlets.
12/03/2003

Struts: MVC-based Architecture


? Central controller mediates application flow
and delegates to appropriate handler
? Handlers are tied to model components
? Model encapsulates business logic
? Control forwarded back through the
Controller to the appropriate View
– The forwarding can be determined by consulting a
set of mappings in configuration file, which provides
a loose coupling between the View and Model
16

In the MVC design pattern, application flow is mediated by a central


Controller. The Controller delegates requests - in our case, HTTP
requests - to an appropriate handler.

The handlers are tied to a Model, and each handler acts as an adapter
between the request and the Model. The Model represents, or
encapsulates, an application's business logic or state.

Control is usually then forwarded back through the Controller to the


appropriate View. The forwarding can be determined by consulting a
set of mappings, usually loaded from a database or configuration file.
This provides a loose coupling between the View and Model, which
can make applications significantly easier to create and maintain.
12/03/2003

Struts: MVC-based Architecture


? 3 Major Components in Struts
– Servlet controller (Controller)
– Java Server Pages or any other presentation
technology (View)
– Application Business Logic in the form of
whatever suits the application (Model)
? Struts is focused on Controller
– Struts is Model and View independent

17

So the three major components of Struts are ActionServlet which functions


as a controller. Java Server Pages and any other presentations
technologies that function as View and Application business logic in the
form of whatever suites the application which functions as Model.

Now key aspect of Struts is controller. That is, the Struts framework has a
concrete set of programming API's and classes for Controller functions
while Struts does not dictate any Model or View technologies. In fact, it is
said that Struts is model and view independent because Struts allow any
kind of View and Model technologies.
12/03/2003

Struts: MVC-based Architecture


? Configuration file contains action
mappings
– Determines forwarding/navigation
? Controller uses mappings to turn HTTP
requests into application actions
? Mapping must specify
– A request path
– Action to act upon the request

18

I
12/03/2003

Controller

19
12/03/2003

What does Controller do?


? Is the switch board of MVC architecture
? Every request goes through the controller
? Responsible for flow control (action
mapping) of the request handling
– reads configuration file to determine the flow control

20

The Controller portion of the application is focused on receiving


requests from the client (typically a user running a web browser),
deciding what business logic function is to be performed, and then
delegating responsibility for producing the next phase of the user
interface to an appropriate View component.
12/03/2003

21
source: Chuck Cavaness

In Struts, the primary component of the Controller is a servlet of class


ActionServlet. This servlet is configured by defining a set of
ActionMappings. An ActionMapping defines a path that is matched
against the request URI of the incoming request, and usually specifies
the fully qualified class name of an Action class. All Actions are
subclassed from org.apache.struts.action.Action. Actions encapsulate
the business logic, interpret the outcome, and ultimately dispatch
control to the appropriate View component to create the response.
12/03/2003

Controller in Struts Framework


? Struts framework provides a base servlet
– org.apache.struts.action.ActionServlet
– Servlet mapping has to be configured in web.xml
? Configuration done through
struts-config.xml
– Action Mapping defines request URI of incoming
requests and specifies Action class
– Defines application modules (1.1)

22
12/03/2003

Developer Responsibility
? Write an Action class (that is, an extension of the
Action class) for each logical request that may
be received
– override execute() method (perform() method
in Struts 1.0)
? Write the action mapping configuration file (in
XML) that is used to configure the controller
servlet (struts-config.xml)
? Update the web application deployment
descriptor file (in XML) for your application to
include the necessary Struts components
23
12/03/2003

Controller Components in
Struts Framework
? ActionServlet (Provided by Struts)
? RequestProcessor (Struts 1.1)(Provided by Struts)
– One for each module
? Action
– Developer extends Struts-provided Action class
? Action Mapping
– Developer specifies action mapping in
struts-config.xml file
– Struts framework creates ActionMapping object and
passes it to Action object
24

In Struts, the primary component of the Controller is a servlet of class


ActionServlet. This servlet is configured by defining a set of
ActionMappings. An ActionMapping defines a path that is matched
against the request URI of the incoming request, and usually specifies
the fully qualified class name of an Action class. All Actions are
subclassed from org.apache.struts.action.Action. Actions encapsulate
the business logic, interpret the outcome, and ultimately dispatch
control to the appropriate View component to create the response.

Struts also supports the ability to use ActionMapping classes that have
additional properties beyond the standard ones required to operate the
framework. This allows you to store additional information specific to
your application, but still utilize the remaining features of the
framework. In addition, Struts lets you define logical "names" to which
control should be forwarded so that an action method can ask for the
"Main Menu" page (for example), without knowing what the actual
name of the corresponding JSP page is. These features greatly assist
you in separating the control logic (what to do) with the view logic
(how it's rendered).
12/03/2003

ActionServlet
(Provided by Framework)
25
12/03/2003

What Does ActionServlet Do?


? Performs the role of Controller
– Process user requests
– Determine what the user is trying to achieve
according to the request
– Pull data from the model (if necessary) to be given
to the appropriate view, and
– Select the proper view to respond to the user
? Delegates most of this grunt work to Action
classes

26

For those of you familiar with MVC architecture, the ActionServlet


represents the C - the controller. The job of the controller is to:
# process user requests,
# determine what the user is trying to achieve according to the request,
# pull data from the model (if necessary) to be given to the appropriate
view, and
# select the proper view to respond to the user.

The Struts controller delegates most of this grunt work to Action classes.
12/03/2003

What Does ActionServlet Do?


? Is responsible for initialization and clean-
up of resources
– loads the application config corresponding to the
"config" init-param's in web.xml
– goes through an enumeration of all init-param
elements, looking for those elements who's name
starts with config/ for modules
– To access the module foo, you would use a URL
like:
? http://localhost:8080/myApp/foo/someAction.do

27

In addition to being the controller for your application, the ActionServlet


instance also is responsible for initialization and clean-up of resources.
When the controller initializes, it first loads the application config
corresponding to the "config" init-param. It then goes through an
enumeration of all init-param elements, looking for those elements who's
name starts with config/. For each of these elements, Struts loads the
configuration file specified by the value of that init-param, and assigns a
"prefix" value to that module's ModuleConfig instance consisting of the
piece of the init-param name following "config/". For example, the module
prefix specified by the init-param config/foo would be "foo". This is
important to know, since this is how the controller determines which module
will be given control of processing the request. To access the module foo,
you would use a URL like:

http://localhost:8080/myApp/foo/someAction.do

For each request made of the controller, the method


process(HttpServletRequest, HttpServletResponse) will be called. This
method simply determines which module should service the request and
then invokes that module's RequestProcessor's process method, passing
the same request and response.
12/03/2003

Flow control by Controller

28
12/03/2003

Struts Flow (Struts 1.0)


Http://myhost/authorize.do

Server configured to pass *.do extensions to


org.apache.struts.action.ActionServlet via a
web.xml configuration file

ActionServlet object inspects the URI and tries to match it


against an ActionMapping located in the struts-config.xml file

Instance of appropriate Action class is found and it’s perform()


is called.

Action object handles the request and returns control to a view.


View is identified by ActionForward objerct.
29

ActionServlet uses a configuration file called struts-config.xml to read


mapping data called action mappings
ActionServlet class reads the incoming URI and tries to match the URI
against a set of action mappings to see if it can find a controller class
which would be willing to handle the request
This process is described in a diagram on the following page
12/03/2003

RequestProcessor
(Provided by Framework)

30

The RequestProcessor is where the majority of the core processing occurs for
each request. Let's take a look at the helper functions the process method
invokes in-turn:
12/03/2003

What Does RequestProcessor Do?


? processPath
– Determine the path that invoked us. This will be
used later to retrieve an ActionMapping.
? processLocale
– Select a locale for this request, if one hasn't already
been selected, and place it in the request.
? processContent
– Set the default content type (with optional character
encoding) for all responses if requested.

31

(read slide)
12/03/2003

What Does RequestProcessor Do?


? processNoCache
– If appropriate, set the following response headers:
"Pragma", "Cache-Control", and "Expires".
? processPreprocess
– This is one of the "hooks" the RequestProcessor
makes available for subclasses to override. The
default implementation simply returns true. If you
subclass RequestProcessor and override
processPreprocess you should either return true
(indicating process should continue processing the
request) or false (indicating you have handled the
request and the process should return) 32

(read slide)
12/03/2003

What Does RequestProcessor Do?


? processMapping
– Determine the ActionMapping associated with this
path.
? processRoles
– If the mapping has a role associated with it, ensure
the requesting user is has the specified role. If they
do not, raise an error and stop processing of the
request.
? processActionForm
– Instantiate (if necessary) the ActionForm
associated with this mapping (if any) and place it
into the appropriate scope. 33

(read slide)
12/03/2003

What Does RequestProcessor Do?


? processPopulate
– Populate the ActionForm associated with this
request, if any.
? processValidate
– Perform validation (if requested) on the ActionForm
associated with this request (if any).
? processForward
– If this mapping represents a forward, forward to the
path specified by the mapping.

34

(read slide)
12/03/2003

What Does RequestProcessor Do?


? processInclude
– If this mapping represents an include, include the
result of invoking the path in this request.
? processActionCreate
– Instantiate an instance of the class specified by the
current ActionMapping (if necessary).
? processActionPerform
– This is the point at which your action's perform() or
execute() method will be called.

35

(read slide)
12/03/2003

What Does RequestProcessor Do?


? processForwardConfig
– Finally, the process method of the
RequestProcessor takes the ActionForward
returned by your Action class, and uses to select
the next resource (if any). Most often the
ActionForward leads to the presentation page that
renders the response.

36

(read slide)
12/03/2003

Action Mapping
(You provide it)
37
12/03/2003

Action Mapping in Struts


Config File
? Struts controller ActionServlet needs to
know several things about how each
request URI should be mapped to an
appropriate Action class
? These requirements are encapsulated in a
Java interface named ActionMapping
– Struts framework creates ActionMapping object
from <ActionMapping> configuration element of
struts-config.xml file

38

The developer's responsibility is to create an XML file named struts-


config.xml and place it in the WEB-INF directory of your application. This
format of this document is described by the Document Type Definition
(DTD) maintained at http://jakarta.apache.org/struts/dtds/struts-
config_1_1.dtd. This chapter covers the configuration elements that you will
typically write as part of developing your application. There are several
other elements that can be placed in the struts-config file to customize your
application.

The controller uses an internal copy of this document to parse the


configuration; an Internet connection is not required for operation.
12/03/2003

Struts Config File


(struts-config.xml)
? struts-config.xml contains three important
elements used to describe actions:
– <form-beans> contains FormBean definitions
including name and type (classname)
– <action-mapping> contains action definitions
? Use an <action> element for each action defined
– <global-forwards> contains your global forward
definitions

39

The outermost XML element must be <struts-config>. Inside of the <struts-


config> element, there are three important elements that are used to describe
your actions:

* <form-beans>
* <global-forwards>
* <action-mappings>
12/03/2003

Struts Config File


(struts-config.xml)
? <form-bean>
– This section contains your form bean definitions.
– You use a <form-bean> element for each form
bean, which has the following important attributes:
? name: The name of the request or session level
attribute that this form bean will be stored as
?
type: The fully-qualified Java classname of your form
bean

40

<form-bean> section section contains your form bean definitions. Form


beans are descriptors that are used to create ActionForm instances at
runtime. You use a <form-bean> element for each form bean, which has the
following important attributes:

* name: A unique identifier for this bean, which will be used to reference
it in corresponding action mappings. Usually, this is also the name of the
request or session attribute under which this form bean will be stored.
* type: The fully-qualified Java classname of the ActionForm subclass to
use with this form bean.
12/03/2003

Struts Config File


(struts-config.xml)
? <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:
?
path: The application context-relative path to the
action
? type: The fully qualified java classname of your
Action class
?
name: The name of your <form-bean> element to
use with this action
41

<action-mapping> section contains your action definitions. You use an


<action> element for each of the mappings you would like to define. Most
action elements will define at least the following attributes:

* path: The application context-relative path to the action.


* type: The fully qualified java classname of your Action class.
* name: The name of your <form-bean> element to use with this action

Other often-used attributes include:

* parameter: A general-purpose attribute often used by "standard"


Actions to pass a required property.
* roles: A comma-delimited list of the user security roles that can access
this mapping.
12/03/2003

struts-config.xml: <form-beans>
1 <?xml version="1.0" encoding="ISO-8859-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>
1 <!-- ========== Action Mapping Definitions ============ -->
2 <action-mappings>
3
4 <action path="/submit"
5 type="hansen.playground.SubmitAction"
6 name="submitForm"
7 input="/submit.jsp"
8 scope="request"
9 validate="true">
10 <forward name="success" path="/submit.jsp"/>
11 <forward name="failure" path="/submit.jsp"/>
12 </action>
13
14 </action-mappings>
15
16 </struts-config>
43
12/03/2003

Action Mapping Config File


? <global-forwards>
– Forwards are instances of the ActionForward class
returned from an ActionForm's execute method
– These map logical names to specific resources
(typically JSPs)
– <forward> element has following attributes
? name: logical name of the forward
– used within execute() method of Action class to
forward to the next resource
?
path: to-be-forwarded resource
?
redirect: redirect (true) or forward (false)
44

<global-forwards> section contains your global forward definitions.

Forwards are instances of the ActionForward class returned from an


ActionForm's execute method. These map logical names to specific
resources (typically JSPs), allowing you to change the resource without
changing references to it throughout your application. You use a <forward>
element for each forward definition, which has the following important
attributes:

* name: The logical name for this forward. This is used in your
ActionForm's execute method to forward to the next appropriate resource.
Example: homepage
* path: The context relative path to the resource. Example: /index.jsp or
/index.do
* redirect: True or false (default). Should the ActionServlet redirect to the
resource instead of forward?
12/03/2003

Global Forwarding
1 <struts-config>
2 <form-beans>
3 <form-bean
4 name="logonForm"
5 type="org.apache.struts.webapp.example.LogonForm" />
6 </form-beans>
7 <global-forwards
8 type="org.apache.struts.action.ActionForward">
9 <forward
10 name="logon"
11 path="/logon.jsp"
12 redirect="false" />
13 </global-forwards>
14

45

First the form bean is defined. A basic bean of class


"org.apache.struts.webapp.example.LogonForm" is mapped to the
logical name "logonForm". This name is used as a request attribute
name for the form bean.

The "global-forwards" section is used to create logical name mappings


for commonly used presentation pages. Each of these forwards is
available through a call to your action mapping instance, i.e.
mapping.findForward("logicalName").
12/03/2003

Global Forwarding
1
2 <action-mappings>
3 <action
4 path="/logon"
5 type="org.apache.struts.webapp.example.LogonAction"
6 name="logonForm"
7 scope="request"
8 input="/logon.jsp"
9 unknown="false"
10 validate="true" />
11 </action-mappings>
12 </struts-config>

46

As you can see, this mapping matches the path /logon (actually,
because the MailReader example application uses extension mapping,
the request URI you specify in a JSP page would end in /logon.do).
When a request that matches this path is received, an instance of the
LogonAction class will be created (the first time only) and used. The
controller servlet will look for a bean in request scope under key
logonForm, creating and saving a bean of the specified class if needed.
12/03/2003

Local Forwarding
1 <!-- Edit mail subscription -->
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

Optional but very useful are the local "forward" elements. In the
MailReader example application, many actions include a local
"success" and/or "failure" forward as part of an action mapping.

Using just these two extra properties, the Action classes are almost
totally independent of the actual names of the presentation pages. The
pages can be renamed (for example) during a redesign, with negligible
impact on the Action classes themselves. If the names of the "next"
pages were hard coded into the Action classes, all of these classes
would also need to be modified. Of course, you can define whatever
local forward properties makes sense for your own application.
12/03/2003

ActionForm
(You provide it)
48
12/03/2003

ActionForm Bean (Form bean)


? Provided by developer
– Define an ActionForm bean (that is, a Java class
extending the ActionForm class) for the input form
– Extend Struts-provided ActionForm class
– Define it in servlet-config.xml file
?
<form-bean>
?
name attribute of <Action> class
? Contains only property getter and property
setter methods for each field-no business
logic
? Provides standard validation mechnism 49

While ActionForm beans often have properties that correspond to


properties in your Model beans, the form beans themselves should be
considered a Controller component. As such, they are able to transfer
data between the Model and View layers.

The Struts framework generally assumes that you have defined an


ActionForm bean (that is, a Java class extending the ActionForm
class) for the input forms in your application.

ActionForm beans are sometimes just called "form beans". These


may be finely-grained objects, so that there is one bean for each form,
or coarsely-grained so that one bean serves several forms, or even an
entire application.
12/03/2003

ActionForm Bean & Controller


? For each ActionForm bean defined in
servlet-config.xml file, Controller
(ActionServlet) will
– Check session scope for an instance of ActionForm
bean
? If not exists, controller creates one

– Call corresponding setter method of ActionForm


bean for every request parameter whose name
corresponds to the name of a property of the bean
– Pass the updated ActionForm bean as a parameter
to execute() method of Action class
50

If you declare such beans in your Struts configuration file (see " Building
the Controller Components"), the Struts controller servlet will
automatically perform the following services for you, before invoking the
appropriate Action method:

* Check for an instance of a bean of the appropriate class, under the


appropriate key, in the appropriate scope (request or session).
* If there is no such bean instance available, a new one is automatically
created and added to the appropriate scope (request or session).
* For every request parameter whose name corresponds to the name of a
property in the bean, the corresponding setter method will be called. This
operates in a manner similar to the standard JSP action <jsp:setProperty>
when you use the asterisk wildcard to select all properties.
* The updated ActionForm bean will be passed to the execute method of
an Action class [org.apache.struts.Action], so that the values can be made
available to your system state and business logic beans.
12/03/2003

How to write ActionForm Bean


? Add just getter and setter methods for
each property of a input form
– Do not include any business logic code
? Add a standard validation method
– Controller will call this validation
? Define a property (with associated getXxx
and setXxx methods) for each field that is
present in the form

51

When you code your ActionForm beans, keep the following principles
in mind:

* The ActionForm class itself requires no specific methods to be


implemented. It is used to identify the role these particular beans play in
the overall architecture. Typically, an ActionForm bean will have only
property getter and property setter methods, with no business logic.
* The ActionForm object also offers a standard validation
mechanism. If you override a "stub" method, and provide error
messages in the standard application resource, Struts will automatically
validate the input from the form (using your method). See " Action Form
Validation" for details. Of course, you can also ignore the ActionForm
validation and provide your own in the Action object.
* Define a property (with associated getXxx and setXxx methods) for
each field that is present in the form. The field name and property name
must match according to the usual JavaBeans conventions (see the
Javadoc for the java.beans.Introspector class for a start on information
about this). For example, an input field named username will cause the
setUsername method to be called.
12/03/2003

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>
24 </html:select><br>
25 <html:submit/>
26 </html:form> 52

So this slide shows submit.jsp page in which a form page prompts 5 properties
- lastname, address, sex, married, age. And in the following slide, we will see
how these properties are reflected as getter and setter methods of ActionForm
class.
12/03/2003

Model: ActionForm
1 package hansen.playground;
2
3 import javax.servlet.http.HttpServletRequest;
4 import org.apache.struts.action.*;
5
6 public final class SubmitForm extends ActionForm {
7
8 /* Last Name */
9 private String lastName = "Hansen"; // default value
10 public String getLastName() {
11 return (this.lastName);
12 }
13 public void setLastName(String lastName) {
14 this.lastName = lastName;
15 }
16
17 /* Address */
18 private String address = null;
19 public String getAddress() {
20 return (this.address);
21 }
22 public void setAddress(String address) {
23 this.address = address;
24 }
25 ...
53

This is an example code of ActionForm class. Here the SubmitForm extends


ActionForm class. And it contains getter and setter methods for each field of an
input form that are defined in the previous slide. In this slide we show getter and
setter methods of only lastname and address properties.
12/03/2003

struts-config.xml: <form-beans>
1 <?xml version="1.0" encoding="ISO-8859-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>
1 <!-- ========== Action Mapping Definitions ============ -->
2 <action-mappings>
3
4 <action path="/submit"
5 type="hansen.playground.SubmitAction"
6 name="submitForm"
7 input="/submit.jsp"
8 scope="request"
9 validate="true">
10 <forward name="success" path="/submit.jsp"/>
11 <forward name="failure" path="/submit.jsp"/>
12 </action>
13
14 </action-mappings>
15
16 </struts-config>
55
12/03/2003

DynaActionForm in Struts 1.1


? Instead of creating a new ActionForm
subclass and new get/set methods for
each of your bean's properties, you can
list its properties, type, and defaults in the
Struts configuration file
– Use DanaActionForm whenever possible
? We will learn more about this in
Advanced Struts session

56

Maintaining a separate concrete ActionForm class for each form in your


Struts application is time-consuming. It is particularly frustrating when all the
ActionForm does is gather and validate simple properties that are passed
along to a business JavaBean.

This bottleneck can be alleviated through the use of DynaActionForm


classes. Instead of creating a new ActionForm subclass and new get/set
methods for each of your bean's properties, you can list its properties, type,
and defaults in the Struts configuration file.
12/03/2003

Action
(You create one)
57
12/03/2003

source: Chuck Cavaness 58


12/03/2003

Action Class
? Focus on control flow
– Process client request by calling other objects
(BusinessLogic beans) inside its execute() method
– Returns an ActionForward object that identifies
where control should be forwarded
?
JSP
? Tile definition
?
Velocity template
?
Another Action

59

The goal of an Action class is to process a request, via its execute method,
and return an ActionForward object that identifies where control should be
forwarded (e.g. a JSP, Tile definition, Velocity template, or another Action)
to provide the appropriate response.
12/03/2003

What is Action Class?


? Java class that does the “work” of your
application
– Handle request
– Perform business logic
? Can be simple or sophisticated
– Simple action class does handle business logic by
itself
– Sophisticated ones invoke Model components
?
Action class functions as a Facade pattern in
this case

60
12/03/2003

Example Action: Logon


? Application needs to
– Authenticate a User
– Establish a User Session
– Error handling
? Develop a “LogonAction”

61

T
12/03/2003

Developer Responsibility:
Action Class
? Extend org.jakarta.struts.action.Action
? Override
– execute() method (in Struts 1.1)
– perform() method (in Struts 1.0)

62
12/03/2003

execute(..) method of Action


class (Struts 1.1 only)
? Invoked by controller

public ActionForward execute(


ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws Exception;

63

Please note that the execute() method receive ActionMapping and


ActionForm objects as input parameters. These two objects are created by
the Struts controller.

In Struts 1.0, Actions called a perform method instead of the now-preferred


execute method. These methods use the same parameters and differ only in
which exceptions they throw. The elder perform method throws
SerlvetException and IOException. The new execute method simply throws
Exception. The change was to facilitate the Declarative Exception handling
feature introduced in Struts 1.1.
12/03/2003

perform(..) method of Action


class (Struts 1.0 only)
? Deprecated

public ActionForward perform(


ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException;

64

The perform method may still be used in Struts 1.1 but is deprecated. The
Struts 1.1 method simply calls the new execute method and wraps any
Exception thrown as a ServletException.
12/03/2003

execute() method of Action class


? If input validation has not yet occurred, validate
the form bean properties as necessary
? Perform the processing required to deal with
this request
? Update the server-side objects (Scope
variables) that will be used to create the next
page of the user interface
? Return an appropriate ActionForward object

65

In the MVC/Model 2 design pattern, a typical Action class will often implement logic
like the following in its execute method:

* Validate the current state of the user's session (for example, checking that the user
has successfully logged on). If the Action class finds that no logon exists, the request
can be forwarded to the presentation page that displays the username and password
prompts for logging on. This could occur because a user tried to enter an application
"in the middle" (say, from a bookmark), or because the session has timed out, and the
servlet container created a new one.
* If validation is not complete, validate the form bean properties as needed. If a
problem is found, store the appropriate error message keys as a request attribute, and
forward control back to the input form so that the errors can be corrected.
* Perform the processing required to deal with this request (such as saving a row
into a database). This can be done by logic code embedded within the Action class
itself, but should generally be performed by calling an appropriate method of a
business logic bean.
* Update the server-side objects that will be used to create the next page of the user
interface (typically request scope or session scope beans, depending on how long you
need to keep these items available).
* Return an appropriate ActionForward object that identifies the presentation page
to be used to generate this response, based on the newly updated beans. Typically,
you will acquire a reference to such an object by calling findForward on either the
ActionMapping object you received (if you are using a logical name local to this
mapping), or on the controller servlet itself (if you are using a logical name global to
the application).
12/03/2003

Example: Action Class


1 package hansen.playground;
2
3 import javax.servlet.http.*;
4 import org.apache.struts.action.*;
5
6 public final class SubmitAction extends Action {
7
8 public ActionForward execute(ActionMapping mapping,
9 ActionForm form,
10 HttpServletRequest request,
11 HttpServletResponse response) {
12
13 SubmitForm f = (SubmitForm) form; // get the form bean
14 // and take the last name value
15 String lastName = f.getLastName();
16 // Translate the name to upper case
17 //and save it in the request object
18 request.setAttribute("lastName", lastName.toUpperCase());
19
20 // Forward control to the specified success target
21 return (mapping.findForward("success"));
22 }
23 }
24 66

This is an example of Action class of submitHansen sample code. In this


example, we just get the value of lastname property and then save it in
request scope object after changing it to uppercase. And then it returns
ActionForward object which is associated with success.
12/03/2003

Design Guidelines of Action Class


? The controller Servlet creates only one
instance of your Action class, and uses it
for all requests
– Action class has to be in multi-threaded safe
– Use local variables (as opposed to instanace
variables)
? Catch errors instead of throwing them
? Make Action class a thin layer
– Call Business logic bean for complex logic handling

67

Write code for a multi-threaded environment - The controller servlet creates only one instance of
your Action class, and uses this one instance to service all requests. Thus, you need to write thread-
safe Action classes. Follow the same guidelines you would use to write thread-safe Servlets. Here are
two general guidelines that will help you write scalable, thread-safe Action classes:

Only Use Local Variables - The most important principle that aids in thread-safe coding is to use
only local variables, not instance variables, in your Action class. Local variables are created on a
stack that is assigned (by your JVM) to each request thread, so there is no need to worry about
sharing them. An Action can be factored into several local methods, so long as all variables needed
are passed as method parameters. This assures thread safety, as the JVM handles such variables
internally using the call stack which is associated with a single Thread.

Don't throw it, catch it! - Ever used a commercial website only to have a stack trace or exception
thrown in your face after you've already typed in your credit card number and clicked the purchase
button? Let's just say it doesn't inspire confidence. Now is your chance to deal with these application
errors - in the Action class. If your application specific code throws expections you should catch
these exceptions in your Action class, log them in your application's log (servlet.log("Error
message", exception)) and return the appropriate ActionForward.

It is wise to avoid creating lengthy and complex Action classes. If you start to embed too much
logic in the Action class itself, you will begin to find the Action class hard to understand, maintain,
and impossible to reuse. Rather than creating overly complex Action classes, it is generally a good
practice to move most of the persistence, and "business logic" to a separate application layer. When
an Action class becomes lengthy and procedural, it may be a good time to refactor your application
architecture and move some of this logic to another conceptual layer; otherwise, you may be left with
an inflexible application which can only be accessed in a web-application environment. Struts should
be viewed as simply the foundation for implementing MVC in your applications. Struts provides you
12/03/2003

Example 2: 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 public ActionForward execute(ActionMapping mapping,
6 ActionForm form,
7 HttpServletRequest request,
8 HttpServletResponse response) {
9
10 SubmitForm f = (SubmitForm) form; // get the form bean
11 // and take the last name value
12 String lastName = f.getLastName();
13 if (lastName.startsWith(“Passion”)){
14 // Translate the name to upper case
15 //and save it in the request object
16 request.setAttribute("lastName", lastName.toUpperCase());
17 // Forward control to the specified success target
18 return (mapping.findForward("success"));
19 }
20 else{
21 return (mapping.findForward("failure"));
22 }
23 }
24 } 68

This is an example of Action class of submitHansen sample code. In this


example, we just get the value of lastname property and then save it in
request scope object after changing it to uppercase. And then it returns
ActionForward object which is associated with success.
12/03/2003

struts-config.xml: ActionMapping
1
2 <!-- ========== Action Mapping Definitions ============ -->
3 <action-mappings>
4
5 <action path="/submit"
6 type="hansen.playground.SubmitAction"
7 name="submitForm"
8 input="/submit.jsp"
9 scope="request"
10 validate="true">
11 <forward name="success" path="/submitSuccess.jsp"/>
12 <forward name="failure" path="/submitFailure.jsp"/>
13 </action>
14
15 </action-mappings>
16
17 </struts-config>
69
12/03/2003

Pre-built Action Classes


? 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
? Model divided into concepts
– Internal state of the system
– Actions that can change that state
? Internal state of system represented by
– JavaBeans
– Enterprise JavaBeans
– POJO's
– JDO
– JDBC
– Whatever 72

The Model portion of an MVC-based system can be often be divided into two major
subsystems -- the internal state of the system and the actions that can be taken to
change that state.

In grammatical terms, we might think about state information as nouns (things) and
actions as verbs (changes to the state of those things).

Many applications represent the internal state of the system as a set of one or more
JavaBeans. The bean properties represent the details of the system' state. Depending
on your application's complexity, these beans may be self contained (and know how
to persist their own state), or they may be facades that know how to retrieve the
system's state from another component. This component may be a database, a search
engine, an Entity Enterprise JavaBean, a LDAP server, or something else entirely.

Many requirements documents used for building web applications focus on the View.
However, you should ensure that the processing required for each submitted request is
also clearly defined from the Model's perspective. In general, the developer of the
Model components will be focusing on the creation of JavaBeans classes that support
all of the functional requirements. The precise nature of the beans required by a
particular application will vary widely depending on those requirements, but they can
generally be classified into several categories discussed below.
12/03/2003

73
source: Chuck Cavaness

This picture shows where the model components fits. As the picture
shows, actual model components can be in any form including
JavaBeans, JDO, CORBA, EJB, JDBC or whatever that can be used
to maintain some internal state.
12/03/2003

Model Components
? JavaBeans and Scope
– Page – visible within a single JSP page, for the
lifetime of the current request
– Request – visible within a single JSP page, as
well as to any page or servlet that is included in
this page, or forwarded to by this page
– Session – visible to all JSP pages and servlets
that participate in a particular user session,
across one or more requests
– Application - visible to all JSP pages and servlets
that are part of a web application
74

Within a web-based application, JavaBeans can be stored in (and accessed from) a


number of different collections of "attributes". Each collection has different rules for
the lifetime of that collection, and the visibility of the beans stored there. Together,
the rules defining lifetime and visibility are called the scope of those beans. The
JavaServer Pages (JSP) Specification defines scope choices using the following
terms (with the equivalent servlet API concept defined in parentheses):

# page - Beans that are visible within a single JSP page, for the lifetime of the
current request. (Local variables of the service method)
# request - Beans that are visible within a single JSP page, as well as to any page or
servlet that is included in this page, or forwarded to by this page. (Request
attributes)
# session - Beans that are visible to all JSP pages and servlets that participate in a
particular user session, across one or more requests. (Session attributes)
# application - Beans that are visible to all JSP pages and servlets that are part of a
web application. (Servlet context attributes)
12/03/2003

Model Components
? JSP pages and servlets in the same web
application share the same sets of bean
collections
? Example
– Servlet code
?
MyCart mycart = new MyCart(...);
? request.setAttribute("cart", mycart);
– JSP page
? <jsp:useBean id="cart" scope="request"
?
class="com.mycompany.MyApp.MyCart"/>
75

It is important to remember that JSP pages and servlets in the same web application
share the same sets of bean collections. For example, a bean stored as a request
attribute in a servlet like this:

MyCart mycart = new MyCart(...);


request.setAttribute("cart", mycart);

is immediately visible to a JSP page which this servlet forwards to, using a standard
action tag like this:

<jsp:useBean id="cart" scope="request"


class="com.mycompany.MyApp.MyCart"/>
12/03/2003

Model Components in Struts


Framework
? ActionForm Bean
– Please note that many people don't regard ActionForm
Bean as a Model component, instead it just represents
input data entered by a user
? SystemState Bean
– This is a conceptual term: Struts does not provide any
programming API
? BusinessLogic Bean
– This is a conceptual term: Struts does not provide any
programming API
76

(read slide)
12/03/2003

Struts Model Components

System State Bean &


Business logic Bean
77
12/03/2003

System State Bean


? Struts does not define formal class of this
? Defines the current state
– Could be represented as a set of one or more
JavaBeans classes, whose properties define the
current state
? Example: a shopping cart system:
– Contains a bean that represents the cart being
maintained for each individual shopper
– Includes the set of items that the shopper has
currently selected for purchase
78

The actual state of a system is normally represented as a set of one or


more JavaBeans classes, whose properties define the current state. A
shopping cart system, for example, will include a bean that represents
the cart being maintained for each individual shopper, and will (among
other things) include the set of items that the shopper has currently
selected for purchase. Separately, the system will also include different
beans for the user's profile information (including their credit card and
ship-to addresses), as well as the catalog of available items and their
current inventory levels.
12/03/2003

SystemState Bean
? For small scale systems or for state
information that need not be kept for a long
period of time
– a set of system state beans may contain all the
knowledge that the system ever has
? Large scale application
– System state beans may represent information that
is stored permanently in some external database
? CustomerBean object that corresponds to a particular
row in the CUSTOMERS table
– EJB might be used 79

For small scale systems, or for state information that need not be kept
for a long period of time, a set of system state beans may contain all
the knowledge that the system ever has of these particular details. Or,
as is often the case, the system state beans will represent information
that is stored permanently in some external database (such as a
CustomerBean object that corresponds to a particular row in the
CUSTOMERS table), and are created or removed from the server's
memory as needed. Entity Enterprise JavaBeans are also used for this
purpose in large scale applications.
12/03/2003

BuesinessLogic Bean
? Struts does not define formal class of this
– Can be an ordinary JavaBean
– Can be stateful or stateless EJB
? Encapsulates functional logic of an
application using method calls
? Action object should translate the HTTP
request then call BusinessLogic bean

80

You should encapsulate the functional logic of your application as method calls
on JavaBeans designed for this purpose. These methods may be part of the
same classes used for the system state beans, or they may be in separate
classes dedicated to performing the logic. In the latter case, you will usually
need to pass the system state beans to be manipulated to these methods as
arguments.
Depending on the complexity and scope of your application, business logic
beans might be ordinary JavaBeans that interact with system state beans
passed as arguments, or ordinary JavaBeans that access a database using
JDBC calls. For larger applications, these beans will often be stateful or
stateless Enterprise JavaBeans (EJBs) instead.
12/03/2003

BuesinessLogic Bean
? Ideally should be designed so that they do
not know they are being executed in a web
application environment
– should not refer any Web application objects
– enhanced reusability

81

For maximum code re-use, business logic beans should be


designed and implemented so that they do not know they are
being executed in a web application environment. If you find
yourself having to import a javax.servlet.* class in your bean,
you are tying this business logic to the web application
environment. Consider rearranging things so that your Action
classes (part of the Controller role, as described below) translate
all required information from the HTTP request being processed
into property setter calls on your business logic beans, after
which a call to an execute method can be made. Such a business
logic class can be reused in environments other than the web
application for which they were initially constructed.
12/03/2003

View Components
(You provide them)

82
12/03/2003

83
source: Chuck Cavaness

This picture shows where the view components fit in the bug picture.
12/03/2003

View Components
? JSP files which you write for your specific
application
? Set of JSP custom tag libraries
? Resource files for internationalization
? Allows for fast creation of forms for an
application
? Works in concert with the controller Servlet

84

Many applications rely on JavaServer Pages (JSP) technology to create


the presentation layer (View). The Struts distribution includes a
comprehensive JSP tag library that provides support for building
internationalized applications, as well as for interacting with input forms.
12/03/2003

View
? ActionForward object tells Servlet controller
which JSP page is to be dispatched to
? JSP pages use ActionForm beans to get
output Model data to display
? Struts contains a series of tag libraries
– Facilitates communication between HTML designers
and developers
– Facilitates dynamic Web content

85
12/03/2003

Forms and FormBean Interactions


? If the user makes an error, the application
should allow them to fix just what needs
to be changed
? With just JSP, you have to do
<input type="text" name="username"
value="<%= loginBean.getUsername() >"/>
? With Struts, you can do
<html:text property="username"/>;

86

At one time or another, most web developers have built forms using the standard
capabilities of HTML, such as the <input> tag. Users have come to expect interactive
applications to have certain behaviors, and one of these expectations relates to error
handling -- if the user makes an error, the application should allow them to fix just
what needs to be changed -- without having to re-enter any of the rest of the
information on the current page or form.

Fulfilling this expectation is tedious and cumbersome when coding with standard
HTML and JSP pages. For example, an input element for a username field might look
like this (in JSP):
<input type="text" name="username"
value="<%= loginBean.getUsername() >"/>
which is difficult to type correctly, confuses HTML developers who are not
knowledgeable about programming concepts, and can cause problems with HTML
editors. Instead, Struts provides a comprehensive facility for building forms, based
on the Custom Tag Library facility of JSP 1.1. The case above would be rendered
like this using Struts:
<html:text property="username"/>;
with no need to explicitly refer to the JavaBean from which the initial value is
retrieved. That is handled automatically by the JSP tag, using facilities provided by
the framework.
12/03/2003

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>
24 </html:select><br>
25 <html:submit/>
87
26 </html:form>

This slide shows submit.jsp page which is the only JSP page used in
the very simple application we built.
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>

88
12/03/2003

web.xml
89
12/03/2003

Web App Deployment


Descriptor (web.xml)
? Struts application is a Web application
– Follows the same rule
– Has to have web.xml deployment descriptor
? web.xml includes:
– Configure ActionServlet instance and mapping
– Resource file as <init-param>
– servlet-config.xml file as <init-param>
– Define the Struts tag libraries
? web.xml is stored in WEB-INF/web.xml
90

Since Struts application is a genuine Web application, it has to follow the


same rule that any Web application has to follow. One of the rules is every
Web application has to have a web.xml deployment descriptor.

In aStruts application, web.xml contains servlet definition and mapping of


ActionServlet. It also contains resource file and struts-config.xml
definitions. Also it has definitions of struts tag libraries.

Just like any other Web application, web.xml resides under WEB-INF
directory either in packed form (*.war file) or unpacked form.
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 <!-- 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
21 </web-app>
22 92
12/03/2003

Struts Tag Library

93
12/03/2003

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
94
12/03/2003

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
95

Some of the features in this taglib are also available in the


JavaServer Pages Standard Tag Library (JSTL). The Struts team
encourages the use of the standard tags over the Struts specific
tags when possible.
12/03/2003

Interaction with JSTL


? Struts-el taglibs allow for using expression
values instead of just rtexprvalue
Runtime: <bean:message key='<%= stringvar %>'/>
Expression: <bean-el:message key="${stringvar}"/>
? Set of optional taglibs that can be used with
the JSTL expression language (EL)
? Implements many (but not all) of the Struts
tags.
? Located in the contrib folder of the Struts
release
? Container with servlet 2.3 support required 96
12/03/2003

Access to Tag Libraries


? All tag libraries are defined in web.xml using
<taglib> element

<!-- Struts Tag Library Descriptors -->


<taglib>
<taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>

<taglib>
<taglib-uri>/WEB-INF/struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib> 97

As was mentioned before, the tag libraries need to be declared in


web.xml deployment descriptor.
12/03/2003

Bean Tags
98
12/03/2003

Bean Tags
? Tags for accessing beans and their
properties
? Enhancements to <jsp:useBean>
? Convenient mechanisms to create new
beans based on the value of:
– Cookies
– Request Headers
– Parameters

99

The "struts-bean" tag library contains JSP custom tags useful in


defining new beans (in any desired scope) from a variety of
possible sources, as well as a tag to render a particular bean (or
bean property) to the output response.

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.
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>

100

Here in this example, the lastname property of bean is rendered to output


reponse being created by the JSP page.
12/03/2003

HTML Tags
101
12/03/2003

HTML Tags
? Form bridge between JSP view and other
components
? Input forms are important for gathering data
? Most of the actions of the HTML taglib
involve HTML forms
? Error messages, hyperlinking,
internationalization
? HTML tags must be nested within a form tag
– inform tag handler which bean to use for initializing
displayed values
102

The tags in the Struts HTML library form a bridge between a JSP
view and the other components of a Web application. Since a
dynamic Web application often depends on gathering data from a
user, input forms play an important role in the Struts framework.
Consequently, the majority of the HTML tags involve HTML
forms.

The HTML taglib contains tags used to create Struts input forms,
as well as other tags generally useful in the creation of HTML-
based user interfaces. The output is HTML 4.01 compliant or
XHTML 1.0 when in XHTML mode.
HTML Tag Resources
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

This is the list of HTML tags that allowssome types of inputs


from a user.

In every case, a field tag must be nested within a form tag, so that
the field knows what bean to use for initializing displayed values.
12/03/2003

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>
24 </html:select><br>
25 <html:submit/> 104
26 </html:form>
12/03/2003

Logic Tags
105
12/03/2003

Logic Tags
? Provides presentation logic tags that
eliminate need for scriptlets
? Value comparisons
Include: = != <= >= < >
? Substring matching
– match, notmatch
? Presentation logic
– forward, redirect
? Collections
– iterate 106

The "struts-logic" tag library contains tags that are useful in


managing conditional generation of output text, looping over
object collections for repetitive generation of output text, and
application flow management
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
? 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

109
12/03/2003

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

110
12/03/2003

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>

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

A few years ago, application developers could count on having to


support only residents of their own country, who are used to only
one (or sometimes two) languages, and one way to represent
numeric quantities like dates, numbers, and monetary values.
However, the explosion of application development based on web
technologies, as well as the deployment of such applications on the
Internet and other broadly accessible networks, have rendered
national boundaries invisible in many cases. This has translated (if
you will pardon the pun) into a need for applications to support
internationalization (often called "i18n" because 18 is the number of
letters in between the "i" and the "n") and localization.
12/03/2003

Internationalization
? Extends basic approach of
java.util.ResourceBundle
– org.apache.struts.util.MessageResources
? Allows specification of dynamic locale key
on a per user basis
? Limited to presentation, not input
? Configure resource bundles in web.xml file

114

Struts builds upon the standard classes available on the Java


platform to build internationalized and localized applications. The
key concepts to become familiar with are:

* Locale - The fundamental Java class that supports


internationalization is Locale. Each Locale represents a particular
choice of country and language (plus an optional language
variant), and also a set of formatting assumptions for things like
numbers and dates.
* ResourceBundle - The java.util.ResourceBundle class
provides the fundamental tools for supporting messages in
multiple languages. See the Javadocs for the ResourceBundle
class, and the information on Internationalization in the
documentation bundle for your JDK release, for more
information.

Please note that the i18n support in a framework like Struts is


limited to the presentation of internationalized text and images to
the user. Support for Locale specific input methods (used with
languages such as Japanese, Chinese, and Korean) is left up to
the client device, whichis usually a web browser.
12/03/2003

Internationalization:
Developer responsibilities
? Create resource file for a default language
? Create resource files for each language
you want to support
? Define base name of the resource bundle
in an initialization parameter
? In JSP page
– Use <html:errors/> to display locale specific error
messages

115
12/03/2003

Resource files
? MyApplication.properties
– Contains the messages in the default language for
your server
– If your default language is English, you might have
an entry like this:
? prompt.hello=Hello
? MyApplication_xx.properties
– Contains the same messages in the language
whose ISO language code is "xx"
? prompt.hello=Bonjour
116

Assume that your source code is created in package


com.mycompany.mypackage, so it is stored in a directory (relative to your
source directory) named com/mycompany/mypackage. To create a resource
bundle called com.mycompany.mypackage.MyApplication, you would create
the following files in the com/mycompany/mypackage directory:

* MyApplication.properties - Contains the messages in the default language


for your server. If your default language is English, you might have an entry like
this: prompt.hello=Hello
* MyApplication_xx.properties - Contains the same messages in the language
whose ISO language code is "xx" (See the ResourceBundle Javadoc page for a
link to the current list). For a French version of the message shown above, you
would have this entry: prompt.hello=Bonjour You can have resource bundle files
for as many languages as you need.
12/03/2003

Example:
ApplicationResource.properties
1 errors.header=<h4>Validation Error(s)</h4><ul>
2 errors.footer=</ul><hr>
3
4 error.lastName=<li>Enter your last name
5 error.address=<li>Enter your address
6 error.sex=<li>Enter your sex
7 error.age=<li>Enter your age

117
12/03/2003

Example: 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>

118

When you configure the controller servlet in the web application deployment
descriptor, one of the things you will need to define in an initialization
parameter is the base name of the resource bundle for the application. In the
case described above, it would be
com.mycompany.mypackage.MyApplication.

<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.MyResources
</param-value>
</init-param>
<!-- ... -->
</servlet>
12/03/2003

(Input) Validation

119

Validation is sometimes called input validation or form


validation.
12/03/2003

Validation: Developer
responsibilities (Struts 1.0)
? Indicate you want input validation as
attributes of <action> element under
<action-mapping> in servlet-config.xml file
– validate=”true”
? Specify the JSP page that needs to be
displayed when validation fails
– input=”/errorpage.jsp”
? Override validate() method within
ActionForm class
– optional 120

(read slide)
12/03/2003

validate() method
? Called by the controller servlet
– after the bean properties have been populated
– but before the corresponding action class's
execute() method is invoked
? Optional
– default method returns null
? Syntax
public ActionErrors validate(ActionMapping mapping,
HttpServletRequest request);

121

In addition to the form and bean interactions described above, Struts offers an additional
facility to validate the input fields it has received. To utilize this feature, override the
following method in your ActionForm class:

validate(ActionMapping mapping, HttpServletRequest request);

The validate method is called by the controller servlet after the bean properties have been
populated, but before the corresponding action class's execute method is invoked.

The validate method is called by the controller servlet after the bean properties have been
populated, but before the corresponding action class's execute method is invoked.
12/03/2003

Example: validate() method


? In Login application
– Make sure both “username” and “password” are
entered
– Make sure “password” is more than 6 chracters

122
12/03/2003

validate() method
? After performing validation
– if no validation error, return null
– If validation error, return ActionErrors
? Each ActionError contains error message key

into the application's MessageResources


bundle
?
The controller servlet stores ActionErrors array
as a request attribute suitable for use by the
<html:errors> tag
? The controller then forwards control back to the
input form (identified by the input property for
this ActionMapping)
123

The validate method has the following options:

* Perform the appropriate validations and find no problems -- Return either null
or a zero-length ActionErrors instance, and the controller servlet will proceed to
call the perform method of the appropriate Action class.
* Perform the appropriate validations and find problems -- Return an
ActionErrors instance containing ActionError's, which are classes that contain the
error message keys (into the application's MessageResources bundle) that should
be displayed. The controller servlet will store this array as a request attribute
suitable for use by the <html:errors> tag, and will forward control back to the
input form (identified by the input property for this ActionMapping).
12/03/2003

ActionError Class
? Mechanism used to return errors during
input validation
? Encapsulate errors
– message key used for text lookup from resource
file
? Supports parametric replacement
? ActionErrors is a collection of ActionError

124
12/03/2003

struts-config.xml: Validation
1
2 <!-- ========== Action Mapping Definitions ============ -->
3 <action-mappings>
4
5 <action path="/submit"
6 type="hansen.playground.SubmitAction"
7 name="submitForm"
8 input="/submit.jsp"
9 scope="request"
10 validate="true">
11 <forward name="success" path="/submit.jsp"/>
12 <forward name="failure" path="/submit.jsp"/>
13 </action>
14
15 </action-mappings>
16
17 </struts-config>
125

This shows the struts-config.xml settings for validation. The input


attribute is set to /submit.jsp, which means when input validation error
occurs, we display the same submit.jsp page. Again, you can change
the value of this attribute to be another JSP page.
12/03/2003

ActionForm
1 public final class SubmitForm extends ActionForm {
2
3 ...
4 public ActionErrors validate(ActionMapping mapping,
5 HttpServletRequest request) {
6
7 ...
8
9 // Check for mandatory data
10 ActionErrors errors = new ActionErrors();
11 if (lastName == null || lastName.equals("")) {
12 errors.add("Last Name", new ActionError("error.lastName"));
13 }
14 if (address == null || address.equals("")) {
15 errors.add("Address", new ActionError("error.address"));
16 }
17 if (sex == null || sex.equals("")) {
18 errors.add("Sex", new ActionError("error.sex"));
19 }
20 if (age == null || age.equals("")) {
21 errors.add("Age", new ActionError("error.age"));
22 }
23 return errors;
24 }
25 ..
26 } 126

This is the example validate() code in submitHansen sample code.


Here the validation logic is basically checking if the value has been
entered or not.

Please note that for each input validation error, ActionError object
is created and then added to ActionErrors array.
12/03/2003

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 <h3>Example Submit Page</h3>
10
11 <html:errors/>
12
13 <html:form action="submit.do">
14 Last Name: <html:text property="lastName"/><br>
15 Address: <html:textarea property="address"/><br>
16 Sex: <html:radio property="sex" value="M"/>Male
17 <html:radio property="sex" value="F"/>Female<br>
18 Married: <html:checkbox property="married"/><br>
19 Age: <html:select property="age">
20 <html:option value="a">0-19</html:option>
21 <html:option value="b">20-49</html:option>
22 <html:option value="c">50-</html:option>
23 </html:select><br>
24 <html:submit/>
25 </html:form> 127

This is submit.jsp page in which the error information that is collected in the
form of ActionErrors array object gets displayed via <html:errors/> tag.
12/03/2003

Validator Framework in Struts


1.1
? Validation logic can be specified in struts-
config.xml configuration file
– Instead of writing Java code - validate() method
? We will learn about it in Advanced Struts
session

128

(read slide)
12/03/2003

Error Handling

129
12/03/2003

Input validation vs.


Business logic Validation
? Perform simple, prima facia validations
using the ActionForm validate() method
– Even this is optional
? Handle the "business logic" validation in
the Action class

130

As mentioned earlier, this feature is entirely optional. The default


implementation of the validate method returns null, and the controller servlet
will assume that any required validation is done by the action class.

One common approach is to perform simple, prima facia validations using


the ActionForm validate method, and then handle the "business logic"
validation from the Action.

The Struts Validator , may be used to easily validate ActionForms.


12/03/2003

Application Errors in Action


Class
? Capture errors from System State bean or
Business Logic bean
? Create ActionErrors object and return
? ActionServlet can take action if a certain
exception is thrown
– Can be global or Action-based
– Can either be a simple forward, or passed to a
custom error handler class
– Through defaut ExceptionHandler
131
12/03/2003

CustomExceptionHandler (1.1)
? Define a custom ExceptionHandler to
execute when an Action's execute()
method throws an Exception
– 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
– Define one for java.lang.Exception for debugging
purpose
132

The default Struts exception handler class creates an ActionError object and
stores it in the appropriate scope object. This allows the JSP pages to use
the errors to inform the user of a problem. If this behavior does not fulfill
your requirements, you are free to plug in one of your own
ExceptionHandler classes.

You can define an custom ExceptionHandler to execute when an Action's


execute method throws an Exception. First, you need to subclass
org.apache.struts.action.ExceptionHandler and override the execute method.
Your execute method should process the Exception and return an
ActionForward object to tell Struts where to forward to next.

You can override global exception handlers by defining a handler inside an


action definition.

A common use of ExceptionHandlers is to configure one for


java.lang.Exception so it's called for any exception and log the exception to
some data store.
12/03/2003

Custom ExceptionHandler (1.1)


? Configure your custom exception handler in
struts-config.xml
<global-exceptions>
<exception
key="some.key"
type="java.io.IOException"
handler="com.yourcorp.ExceptionHandler"/>
</global-exceptions>
? com.yourcorp.ExceptionHandler.execute()
will be called when any IOException is
thrown by an Action
133

Once you have created your specialized ExceptionHandler class,


you will need to inform the Struts framework to use your version
instead of the default Struts exception handler. To do this, you
will just need to declare your class in the Struts configuration file.

Then you configure your handler in struts-config.xml like this:

<global-exceptions>
<exception
key="some.key"
type="java.io.IOException"
handler="com.yourcorp.ExceptionHandler"/>
</global-exceptions>

This configuration element says that


com.yourcorp.ExceptionHandler.execute will be called when any
IOException is thrown by an Action. The key is a key into your
message resources properties file that can be used to retrieve an
error message.
12/03/2003

CustomExceptionHandler (1.1)
? Can be either global or per action
<action ...>
<exception
key="some.key"
type="java.io.IOException"
handler="com.yourcorp.ExceptionHandler"/>
</action>

134

T
12/03/2003

Example: Throwing an Exception


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) {
9
10 SubmitForm f = (SubmitForm) form; // get the form bean
11 // and take the last name value
12 String lastName = f.getLastName();
13 if (lastName.startsWith(“Passion”)){
14 // Translate the name to upper case
15 //and save it in the request object
16 request.setAttribute("lastName", lastName.toUpperCase());
17 // Forward control to the specified success target
18 return (mapping.findForward("success"));
19 }
20 else{
21 throw new WrongLastNameExcetion(lastName);
22 }
23 }
24 } 135

This is an example of Action class of submitHansen sample code. In this


example, we just get the value of lastname property and then save it in
request scope object after changing it to uppercase. And then it returns
ActionForward object which is associated with success.
12/03/2003

Custom ExceptionHandler (1.1)


? Different ExceptionHandler for different error
<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

You can create multiple exception handlers, each one worrying


about different exceptions. In this case, when an exception is
thrown, the framework will attempt to find an ExceptionHandler
configured for the exact match. If there's no exact match, the
framework will proceed up the superclass chain of the exception
until a match is found. With this approach, you can have a
hierarchical relationship of handlers, and all of it declarative.
12/03/2003

View Selection

137
12/03/2003

View Selection: Developer


responsibilities
? In struts-config file,
– Indicate “to be forwarded JSP page” for each
outcome via <forward> child element of <action>
element
? In execute() method of Action class,
– Return ActionForward object which is associated
with a particular outcome

138
12/03/2003

struts-config.xml: ActionMapping
1
2 <!-- ========== Action Mapping Definitions ============ -->
3 <action-mappings>
4
5 <action path="/submit"
6 type="hansen.playground.SubmitAction"
7 name="submitForm"
8 input="/submit.jsp"
9 scope="request"
10 validate="true">
11 <forward name="success" path="/submit.jsp"/>
12 <forward name="failure" path="/submit.jsp"/>
13 </action>
14
15 </action-mappings>
16
17 </struts-config>
139
12/03/2003

Example: Action Class


1 package hansen.playground;
2
3 import javax.servlet.http.*;
4 import org.apache.struts.action.*;
5
6 public final class SubmitAction extends Action {
7
8 public ActionForward execute(ActionMapping mapping,
9 ActionForm form,
10 HttpServletRequest request,
11 HttpServletResponse response) {
12
13 SubmitForm f = (SubmitForm) form; // get the form bean
14 // and take the last name value
15 String lastName = f.getLastName();
16 // Translate the name to upper case
17 //and save it in the request object
18 request.setAttribute("lastName", lastName.toUpperCase());
19
20 // Forward control to the specified success target
21 return (mapping.findForward("success"));
22 }
23 }
140
24
12/03/2003

Tools

141
12/03/2003

Struts Console
? Visual editor of
– JSP Tag Library files
– Struts 1.0 and 1.1 config files
– Tiles config file
– Validator config files
? Can be used as a standalone or plugin to
major IDE's
? Download it from
– http://www.jamesholmes.com/struts/console/

142

The Struts Console is a FREE standalone Java Swing application


for developing and managing Struts-based applications. With the
Struts Console you can visually edit JSP Tag Library, Struts, Tiles
and Validator configuration files.

The Struts Console also plugs into multiple, popular Java IDEs for
seamless management of Struts applications from one central
development tool.
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

Live your life


with Passion!

151

You might also like