Presentation is loading. Please wait.

Presentation is loading. Please wait.

13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling.

Similar presentations


Presentation on theme: "13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling."— Presentation transcript:

1 13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling

2 13-2 Copyright © 2004, Oracle. All rights reserved. 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

3 13-3 Copyright © 2004, Oracle. All rights reserved. Overview of Validation Method Validators, Control Hints ADF Business Components Struts Form Bean Struts Action Form Bean validate() method, Struts Validator JSP JavaScriptNew Validation Methods

4 13-4 Copyright © 2004, Oracle. All rights reserved. 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.

5 13-5 Copyright © 2004, Oracle. All rights reserved. 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

6 13-6 Copyright © 2004, Oracle. All rights reserved. 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; }

7 13-7 Copyright © 2004, Oracle. All rights reserved. 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

8 13-8 Copyright © 2004, Oracle. All rights reserved. Printing Errors in the JSP Ensure that the JSP contains an tag: Note that you can specify the property attribute of the tag to print only the corresponding error: … …

9 13-9 Copyright © 2004, Oracle. All rights reserved. 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

10 13-10 Copyright © 2004, Oracle. All rights reserved. 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; }

11 13-11 Copyright © 2004, Oracle. All rights reserved. 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"); }

12 13-12 Copyright © 2004, Oracle. All rights reserved. Validation Results 2. User enters no data (form bean validation). 1. User enters incorrect login (action validation). 3. User enters correct login.

13 13-13 Copyright © 2004, Oracle. All rights reserved. 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

14 13-14 Copyright © 2004, Oracle. All rights reserved. Setting Up the Struts Validator 1.Specify the validator class in the 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.

15 13-15 Copyright © 2004, Oracle. All rights reserved. Utilizing the Struts Validator Add ValidatorPlugIn to the tag and specify the path for validator-rules.xml and validation.xml :

16 13-16 Copyright © 2004, Oracle. All rights reserved. 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

17 13-17 Copyright © 2004, Oracle. All rights reserved. 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" >

18 13-18 Copyright © 2004, Oracle. All rights reserved. Struts Validator Output Validator messages can be displayed on the page or in a JavaScript window:

19 13-19 Copyright © 2004, Oracle. All rights reserved. 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

20 13-20 Copyright © 2004, Oracle. All rights reserved. JavaScript JavaScript is supported in JDeveloper as a simple way to incorporate validation.

21 13-21 Copyright © 2004, Oracle. All rights reserved. Enhancing the View Use control hints to modify the way an attribute is displayed in a client.

22 13-22 Copyright © 2004, Oracle. All rights reserved. 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

23 13-23 Copyright © 2004, Oracle. All rights reserved. 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

24 13-24 Copyright © 2004, Oracle. All rights reserved. Practice 13-1

25 13-25 Copyright © 2004, Oracle. All rights reserved. Practice 13-1

26 13-26 Copyright © 2004, Oracle. All rights reserved. Practice 13-1


Download ppt "13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling."

Similar presentations


Ads by Google