Documente Academic
Documente Profesional
Documente Cultură
Rajender Sriramoju
Portal Architect
November, 2004
Error validation and exception handling are important aspects in Web applications.
Having a good design and implementation for error validation and exception handling
makes applications more robust and allows them to gracefully respond in abnormal
conditions.
This article tells how you can leverage the error validation and exception handling
features, which are built into the WebSphere Portal Struts Portlet Framework, in your
portlet applications. A sample Registration application, which accompanies this article as
a download, illustrates the use of these features.
Table of contents
Introduction ......................................................................................................................... 2
About the examples............................................................................................................. 3
Error validation in Struts portlets ........................................................................................ 3
Programmatic error validations....................................................................................... 3
ActionForm as a data collector and buffer ...................................................................... 4
Action firewall – Validates data before the action sees it ............................................... 4
Creating the Struts portlet application................................................................................. 5
JSPs ................................................................................................................................. 6
Classes............................................................................................................................. 6
Resources ........................................................................................................................ 6
Step 1: Create a Struts Portlet project. ............................................................................ 6
Step 2: Create an ActionForm class. ............................................................................... 8
Step 3: Specifiy error messages. ................................................................................... 11
Step 4: Create custom validations. ................................................................................ 11
Step 5: Create and configure an Action class................................................................ 13
Step 6: Run the application. .......................................................................................... 14
Reporting business layer errors to the user ....................................................................... 16
Struts exception handling .................................................................................................. 18
Conclusion......................................................................................................................... 19
Resources .......................................................................................................................... 19
Download .......................................................................................................................... 19
About the author................................................................................................................ 19
Introduction
Most portlet applications accept input from users. For example, the input might come in
a form consisting of input text fields, dropdown lists, radio buttons, or check boxes.
However, the user input does not always make sense. An application might expect
specific date and phone number formats, require certain input fields, and need other
specific forms of data. Therefore, the application needs to validate the input.
Where possible, perform all input validations at the presentation service layer, and
display useful and meaningful error messages so that the user can supply missing
information, or correct invalid data items. Also, display meaningful and useful error
messages when system outages occur, or or when critical business rule validation failures
happen at the business and data service layers.
For example, when a user tries to update or add data records to a backend database
through a portlet, there may be several validations that take place in various modules in
the business and data layers of the system. If any of the validations fails, you need to
display an appropriate error message. If you provide structured error messages with an
error number, you improve customer service and application reliability.
Websphere Portal V5.0 provides the Struts Portlet Framework, which supports
deployment of Struts applications as portlets. You can leverage the error validation and
exception handling features which are built into this framework.
This article describes options for server side error validation and exception handling in
Struts-based portlets.
This article assumes that you have basic knowledge of the IBM Portlet API, Struts, and
J2EE development using Websphere Studio.
The following products were used to develop and deploy the example application.
Using Struts framework, you can implement error validation in two ways:
Figure 1 illustrates the life cycle of input processed using the ActionForm.
The following activity diagram illustrates the life cycle of the Struts ActionForm class. It
shows the steps involved in the ActionForm error validation process when a user enters
input values into a form and submits it.
Figure 1. Life cycle of an ActionForm
Classes
• ActionForm: RegistrationForm.java
Extends ActionForm and includes the custom form validation
• Action: RegistrationAction.java
Extends Action class and executes upon successful form validation and forwards to
confirm.jsp
Resources
• Resources File: ApplicationResources.properties file that includes all error
messages appropriate to the application
Now you see how to implement error validation in a Struts portlet application.
1. Select New -> Project, and create a Portlet project. Creates the registration JSP in the
WebContent folder. In the JSP, create an HTML form, within this tag, with the
required fields.
<html:form action="registration"> </html:form>
2. In order to use the Struts html tag, include the tag libraries at the top of JSP.
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
3. When error validations are performed, if any error messages are present, you can
display them by coding the <html:errors/> tag.
Listing 1. Sample JSP. All Input fields are removed from this listing except the first
name field.
<html:html locale="true">
<head>
<html:base />
<title>Struts Portlet Error Validation Example1</title>
<link rel="stylesheet" href="<%=
response.encodeURL("/stylesheet/sample.css") %>" type="text/css">
</head>
2. Click Next, provide a name for the action form, and choose a package. For example,
the sample application uses RegistrationForm in the
com.demo.struts.registration.form package.
3. Select the validate(…,HttpServletRequest) method.
4. Click Next, so you can choose the form fields from the registration.jsp page. The
ActionForm is created from the form fields of JSP. Choose the appropriate fields and
click Next.
You see all the form fields you selected displays as new fields for the ActionForm
Class.
Application Developer creates the action form, and adds an entry in the Struts
configuration file (struts-Config.xml) form-beans tag.
<!-- Form Beans -->
<form-beans>
<form-bean name="registrationForm"
type="com.demo.struts.registration.form.RegistrationForm">
</form-bean>
</form-beans>
If you look at RegistrationForm, you see that get and set methods are created for
each field chosen for the form.
Now you are ready to include the custom validations in your action form’s validate()
method.
# Error Messages
global.error.registration.groupno=<li>Invalid Group Number. Specify a
valid Group Number</li>
global.error.registration.error=<li>Invalid registration</li>
global.error.registration.passworderror=<li>Password and Reenter
Password does not match</li>
In the listing below, you see several of the validations. See RegistrationForm.java
class, in the download, for the entire source.
public ActionErrors validate(ActionMapping mapping,HttpServletRequest
request)
{
ActionErrors errors = new ActionErrors();
if ((firstName == null) || (firstName.length() == 0)) {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError(
"global.error.registration.requiredfield",
"First Name"));
return errors;
The validate() method checks the user input, determines if a field is null, and displays
an appropriate message.
The ActionErrors object has a request scope. After an instance is created and populated
by the validate() method, it is stored into the request. A JSP can retrieve the object
from the request and iterate through ActionError objects to display the error messages
to the user.
You can create the instance of ActionErrors in the validate()method . As you find
errors, you can add the ActionError object to the ActionErrors object. In a JSP, you
retrieve the errors using the <html:errors /> tag.
To choose an error message for a form field, you can use any of the several constructors
of the ActionError class. Below are some examples.
• new ActionError("global.error.registration.requiredfield",
"Password"))
global.error.registration.requiredfield=<li>The {0} is required for
New Registration</li>
The first string argument corresponds to a key from the application’s resource bundle.
The remaining arguments are used as parametric replacement values for the error
message.
• new ActionError("global.error.registration.groupno"))
The first key argument retrieves the corresponding error message from the application
resource bundle.
global.error.registration.groupno=<li>Invalid Group Number. Specify
a valid Group Number</li>
• If all the errors are to be displayed at the top of the page, you can use the constant
ActionErrors.GLOBAL_ERROR
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError("global.error.registration.passworderror"))
If the validation is enabled for an action class in the struts-config.xml file, then the
ActionForm associated with the action class validate() method runs. If all the
validation conditions succeed in the ActionForm class validate() method, then the
execute()method of the Action class is called.
Important: Make sure to set the Action mapping validation flag to true in the struts-
config.xml so that the Struts framework runs the specified ActionForm’s validate()
method.
<action
path="/registration"
type="com.demo.struts.registration.action.RegistrationAction"
scope="request"
name="registrationForm"
validate="true"
input="/registration.jsp" >
<forward name="success" path="/confirm.jsp" redirect="true"/>
<forward name="failure" path="/registration.jsp"
redirect="true"/>
</action>
You can use the Action class execute() method to call business layer commands and
services. If any remote exceptions or business layer validation errors occur, then you can
use the ActionErrors class to report the errors to the JSP.
<welcome-file-list>
<welcome-file>registration.jsp</welcome-file>
</welcome-file-list>
2. Set up the WebSphere Portal server test environment using the Server perspective.
3. Right-click on the portlet application project, and choose Run on server. The
registration page launches.
4. Without entering any test data,click Register. Error messages display at the top of the
form.
5. Enter different values for Password and Reenter Password fields; you can see does
not match error message at the top of form.
6. Enter appropriate data. When all the form fields pass through the validations,
RegistrationAction class runs the execute() method and forwards control to
confirm.jsp.
The above sample application illustrated the design and implementation of error
validations in a Struts portlet.
Let’s examine the flow of this application and how it reports the business layer errors to
an end user. (See also the Figure1. Lifecycle of an ActionForm.)
The RegistrationAction class creates a Java bean that can be passed to the
business layer.
RegistrationForm registrationForm = (RegistrationForm) form;
RegistrationBean regBean = new RegistrationBean();
if (groupNo.equalsIgnoreCase("GP12345") &&
companyName.equalsIgnoreCase("MyCompany"))
{
// here you can code to access a DAO and get data from backend
return true;
}
else
{
throw new RegistrationException("Registration failed..A
valid group no and company required");
}
}
try {
BeanUtils.copyProperties(regBean, registrationForm);
RegistrationCommand regCommand = new RegistrationCommand();
if (regFlag = true)
forward = mapping.findForward("success");
} catch (RegistrationException re) {
errors.add( ActionErrors.GLOBAL_ERROR,
new ActionError("global.error.registration.error"));
saveErrors(request, errors);
return mapping.findForward("failure");
} catch (Exception e) {
e.printStackTrace();
errors.add( ActionErrors.GLOBAL_ERROR,
new ActionError("global.error.systemfailure.error"));
saveErrors(request, errors);
return mapping.findForward("failure");
}
Struts exception handling
Struts Framework V1.1 provides an exception handling framework for the applications. It
enables developers to declare exceptions using the struts-config.xml file. You can
modify the exception handling logic without recompiling the code.
In the struts-config.xml file, you can specify the exceptions that could occur and the
action to take if they do occur, both at the global level and for a specific action mapping.
<global-exceptions>
<exception
key="global.error.registration.error"
path="/registration.jsp"
scope="request"
type="com.demo.struts.registration.exceptions.RegistrationException"
/>
</global-exceptions>
The exception element of the Action class in the struts-config.xml file specifies the
path to the page to be forwarded when one of the specified exceptions occurs during an
action invocation. Whenever an exception is not handled programmatically (that is, is is
not caught in the try-catch block in the action class execute() method), then
wpsRequestProcessor gets a chance to check if there is a specific exception element
configured. If there is one specified, control is passed to the resource specified in the path
attribute of the exception element.
Conclusion
This article described an approach to designing and implementing error validation and
exception handling in Struts based portlets, using a sample Registration application.
Resources
• Writing a Simple Struts Application using WebSphere Studio V5
http://www.ibm.com/developerworks/websphere/techjournal/0302_fung/fung.html
• Migrating a Struts application to Websphere Portal
http://www.ibm.com/developerworks/websphere/techjournal/0403_yu/0403_yu.html
• Build error-free apps fast: Manage beans and error validation in WebSphere Portal
and Java Server Faces
http://www.ibm.com/developerworks/edu/i-dw-wes-jsfportals-i.html
• Struts User’s Guide
http://struts.apache.org/userGuide/index.html
• Programming Jakarta Struts, 2nd Edition, by Chuck Cavaness
O’Reilly & Associates, 2004. ISBN 0596006519
• Portlet Development guide
http://www.redbooks.ibm.com/abstracts/sg246076.html?Open
• Portal Application design and development guidelines
http://www.redbooks.ibm.com/abstracts/redp3829.html?Open
• Websphere Portal Information Center – Portlet development
http://www.ibm.com/developerworks/websphere/zones/portal/proddoc.html
• developerworks Websphere Studio zone
http://www.ibm.com/developerworks/websphere/zones/studio/
• IBM Websphere Portal Toolkit
http://www.ibm.com/websphere/portal/toolkit
• developerworks Websphere Portal zone
http://www.ibm.com/developerworks/websphere/zones/portal/
Download
To get the error handling example code that accompanies this article see:
http://www.ibm.com/developerworks/websphere/library/techarticles/0411_sriramoju/041
1_sriramoju.html
• DB2, IBM, Lotus, Tivoli, Rational, and WebSphere are trademarks or registered
trademarks of IBM Corporation in the United States, other countries, or both.
• Windows and Windows NT are registered trademarks of Microsoft Corporation in the
United States, other countries, or both.
• Java and all Java-based trademarks and logos are trademarks or registered trademarks
of Sun Microsystems, Inc. in the United States, other countries, or both.
• Other company, product, and service names may be trademarks or service marks of
others.