Resources

SAS® AppDev Studio 3.0 Developer's Site

About the Struts Declarative Form Validation Example

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:

Browsing index.htm

Clicking the Comment Submission Form link displays the form to fill out:

Filling out form

Clicking the Submit button validates the form values. If validation fails, errors are displayed like this:

Form validation errors

After correcting errors and passing validation, the acknowledgement page displays, showing the user's name entered on the form.

Thank You page

How It Works

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.

Benefits of Struts Illustrated in this Example

Web Application Flow

Web Application Flow Chart

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: