This example highlights how to incorporate several features of the Struts framework and tag libraries in a Web application generated using webAF:
The example has an initial greeting page, a page with a form to submit comments about the Web site, and an acknowledgement page that is displayed when the submission is successful. This is the greeting page:
Clicking the Comment Submission Form link displays the form to fill out:
Clicking the Submit button validates the form values. If validation fails, errors are displayed like this:
After correcting errors and passing validation, the acknowledgement page displays, showing the user's name entered on the form.
See Struts for a brief overview of the Struts framework. Since there are several books available about Struts, we will not attempt to describe it in any detail. Instead we will focus on presenting a simple example that illustrates a few key Struts features, and one way to get started with Struts using webAF as your development platform. In order to keep things simple and emphasize the Struts concepts, this example does not have any technology specific to SAS, although you can easily integrate SAS taglibs too.
See struts.apache.org/ for more detailed information on Jakarta Struts, which is an open source framework for building web applications. The project is hosted by the Apache Software Foundation (ASF). At publication time, Struts 1.1 has not yet had its final release; webAF 3.0 includes Version 1.1-b3 of Struts. Since this is a rapidly evolving technology, please refer to this Web site for current information.
The example index.jsp page shows how to use the Struts <bean:message>
tag to isolate locale-dependent text strings in a .properties file. webAF
generates a standard application.properties
file for Struts-based content.
This example just adds a couple of application-specific strings to that file.
(Note: For simplicity and clarity, only the index.jsp
page renders text through the
resource bundle, but the same technique could be used throughout).
webAF automatically generates the servlet declaration and action mapping in
the web.xml
file to support a standard Struts ActionServlet to intercept and
process requests for URLs ending in ".do
". The logical flow of your Web app
is controlled through declarations you make in the struts-config.xml
file.
This file is also generated automatically by webAF for Struts-based projects.
The example only adds a few lines to control the dispersal
of HTTP requests.
In conventional design, each Form generally needs a class to
extract the relevant request parameters, perform the validation, and
provide error information that will be displayed to the user if
validation fails. This coding effort for this can be substantial.
Struts provides a flexible alternative. You declare some information
about your form and its request parameters in the struts-config.xml
file,
and Struts will automatically instantiate a class to do the "heavy lifting".
By adding a few lines to the validation.xml
file (which is also
auto-generated by webAF for Struts-based projects), the example
declares what fields in the form are required. It also uses
regular expressions to declare the allowable format for fields. Struts
automatically performs the validation on the field values. If validation
fails, a collection of error messages is generated automatically. In
this example, the errors are displayed by the jsp using a
Struts <html:messages>
tag. In "real" web applications, the validation
declarations can be re-used, and also augmented with hard-coded verification
classes for more complex verification.
The example jsp pages contain no java code scriptlets or expressions at all. Everything is handled through Struts tag libraries, so web page designers can concentrate on presentation and developers can concentrate on business logic.
The diagram at the right is a "flow chart" of the Web applicationlication. The solid lines represent classes or files we create, and the dashed boxes represent classes that are part of Struts or that Struts auto-instantiates. ActionServlet applicationears multiple times in the diagram because it is the controller that processes all requests. Note however, that there is only one instance of ActionServlet.
The entry point into this web applicationlication is index.jsp
. It uses Struts
<bean:message>
tags to render text content using a resource bundle
for I18N support. The "Comment Submission Form" link has an attribute
href="view.submitcomment.do"
. The web.xml
file declares that requests with a
URL pattern of ".do" are handled by the Struts Action controller servlet,
which in turn consults the action-mapplicationings in struts-config.xml
to decide how to handle
the request.
Because the <action>
element for target "/view.submitcomment
" in
struts-config.xml
specifies validate="false"
, the request is
forwarded to submitcomment.jsp
directly, without any validation occurring first. This page renders the
comment submission form. After the user enters
text in the fields and clicks the Submit button, the specified HTML form
action of "/submitcomment"
is handled according to its <action>
element
in struts-config.xml
.
Because this action specifies attribute validate="true"
, Struts validates the
submitted form's request parameters. The parameters are declared in
<form-parameter>
sub-elements of the <form-bean>
for our "commentForm
".
Struts uses this information and the declarations in validation.xml
to instantiate an object of type org.apache.struts.validator.DynaValidatorForm
that will perform the validation.
The <form>
element of validation.xml
for "commentForm
" specifies
which fields in the form are required, and a regular expression to validate
the format of the value specified. For each field value that fails validation
(either due to being missing but required, or being invalid according to the regular
expression), a message string is generated using the applicationropriate entries from
file applicationlication.properties
, and this string is added to the collection of
messages. Since validation has failed, the attribute input="/submitcomment.htm"
for our action in struts-config.xml
forwards the request to the submitcomment.jsp
page again.
This time the messages collection attached to the request is not empty, so
the <html:messages>
tag near the top of submitcomment.jsp
renders the list
of error message strings to tell the user what needs to be corrected. Assuming
the user enters correct information and clicks the Submit button on the form again,
the validation process is repeated successfully.
The attribute type="application.SubmitCommentAction"
for the "/submitcomment
" action
in struts-config.xml
specifies the name of our class (that extends org.apache.struts.Action),
whose execute()
method should be called upon successful validation. This is the "hook"
to our business logic. Our execute()
method in SubmitCommentAction.java
does
almost nothing other than return "success", but in a real applicationlication, this is
where you would perform your business logic on the field values submitted from the
form.
Finally, when execute()
returns, the <forward name="success" path="thankyou.htm" />
sub-element of our action declaration in struts-config.xml
specifies that the request gets
forwarded to thankyou.jsp
. This page displays the acknowledgement of receipt of the
comment form. It uses the Struts <bean:parameter>
and <bean:write>
tags
to extract and render the form's userId
value from the request. Then it uses the Struts
<logic:present>
tag to conditionally render a message depending on whether or
not the user checked "Let forum viewers see this email address" in the form.
See also: