230 likes | 248 Views
Adding Validation and Error Handling. Objectives. After completing this lesson, you should be able to do the following: Validate form input Use declarative validation Use client-side validation Utilize control hints to modify the view. Overview of Validation. Form Bean validate()
E N D
Objectives • After completing this lesson, you should be able to do the following: • Validate form input • Use declarative validation • Use client-side validation • Utilize control hints to modify the view
Overview of Validation Form Bean validate() method, Struts Validator Method Validators, Control Hints ADF Business Components Struts Form Bean Struts Action JSP JavaScript New Validation Methods
Need for Validation • In Web applications, the user usually does not receive training on how to complete fields correctly. Thus, an application must provide feedback to the user for these types of actions: • Entering required values • Specifying values within a specified range • Comparing values • For example, Password1 must match Password2.
Client-Side Validation • To perform validation by using Struts, you must: 1. Create a form bean class 2. Overwrite the form bean validate() method, passing an error to the action 3. Create the error message in ApplicationResources.properties 4. Add the input attribute for the form to the action to indicate where the error message should appear
Form Bean Validation Method • The form bean contains a validate() method for validating form fields. Overwrite this method to perform custom validation: public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); if ((username == null) || (username.trim().equals(""))) { errors.add("username", new ActionError("error.username.required")); } return errors; }
Creating the Error Message • To display the error message, specify the message in ApplicationResources.properties: • Define where the error message is to be displayed by using the input attribute: error.username.required=The Username Value must be Supplied <action name="logonbean" path="/logon" input="showLogon.jsp" type="view.LogonAction">
Printing Errors in the JSP • Ensure that the JSP contains an <html:errors> tag: • Note that you can specify the property attribute of the <html:errors> tag to print only the corresponding error: <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html"%> <%@ page contentType="text/html;charset=windows-1252"%> <html> … <body> <html:errors /> … <html:text property="username"></html:text> • <html:errors property="username" />
Validating Actions • A second type of validation is to overwrite the execute method in the action class. This type of validation is useful when: • You have previously created classes that check the validity of a given value • You do not want the form values to be reset after validation • The validation logic is complex
Creating a Validation Class • The first step in validating user input is to create a method for validation. This can be done in a simple class file, as shown: package view; public class LoginValidation { boolean checkUsernamePassword(String un, String pw) { if ( un.equals("scott") && pw.equals("tiger") ) return true; else return false; } }
The execute() Method • To validate user input in the action class, overwrite the execute() method, calling your validation method: public ActionForward execute… { LogonActionForm logonForm = (LogonActionForm) form; String un = logonForm.getUsername(); String pw = logonForm.getPassword(); LoginValidation loginvalidation = new LoginValidation(); if ( loginvalidation.checkUsernamePassword(un,pw)) { return mapping.findForward("success"); } else return mapping.findForward("failure"); }
Validation Results • 2. User enters no data (form bean validation). • 1. User enters incorrect login (action validation). • 3. User enters correct login.
Struts Validator • Declaratively validate form fields by using the Struts Validator. The validator plug-in: • Is XML based • validator-rules.xml (Validation rules) • validations.xml (Usages) • Defines rules for each field in a form bean • Can provide client validation using JavaScript • Is extensible
Setting Up the Struts Validator 1. Specify the validator class in the <plug-in> tag of struts-config.xml. 2. Add validation-rules.xml to your project. 3. Modify the form bean class to subclass ValidatorForm or DynaValidatorForm. 4. Create a usage file to specify form field rules. 5. Add error messages to ApplicationResources.properties.
Utilizing the Struts Validator • Add ValidatorPlugIn to the <plug-in> tag and specify the path for validator-rules.xml and validation.xml:
Utilizing the Struts Validator • Specify the form bean to use the Struts Validator by subclassing ValidatorForm or DynaValidatorForm: • Create validation.xml to validate form fields, and ensure that each form field to be validated contains an entry in ApplicationResources.properties: import org.apache.struts.validator.ValidatorForm; public class LogonActionForm extends ValidatorForm {… Logon.username=username Logon.password=password
validation.xml: Example <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd"> <form-validation> <formset> <form name="logonbean"> <field property="password" depends="required" > <arg0 key="logon.password" /> </field> </form> </formset> </form-valiidation>
Struts Validator Output • Validator messages can be displayed on the page or in a JavaScript window:
Exception Handling • Exception handling is implemented by: 1. Creating a class for exception handling, which subclasses org.apache.struts.action.ExceptionHandler 2. Overriding the execute() method to process the exception 3. Returning an ActionForward object 4. Configuring the exception handler in the struts-config.xml file 5. Creating an exception message in ApplicationResources.properties
JavaScript • JavaScript is supported in JDeveloper as a simple way to incorporate validation.
Enhancing the View • Use control hints to modify the way an attribute is displayed in a client.
Summary • In this lesson, you should have learned how to: • Validate form input using: • The validate() method • The action class • The Struts Validator • Develop JavaScript validation • Customize the view by using control hints
Practice 13-1: Overview • This practice covers the following topics: • Validating form fields by using the validate() method • Creating validation methods • Calling validation methods from actions • Utilizing the Struts Validator • Handling exceptions