13 copyright © 2004, oracle. all rights reserved. adding validation and error handling

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

Upload: osborn-wiggins

Post on 06-Jan-2018

224 views

Category:

Documents


0 download

DESCRIPTION

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

TRANSCRIPT

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

13Copyright © 2004, Oracle. All rights reserved.

Adding Validation and Error Handling

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

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

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

13-3 Copyright © 2004, Oracle. All rights reserved.

Overview of Validation

Method Validators, Control Hints

ADF Business

ComponentsStruts

Form BeanStrutsAction

Form Bean validate() method, Struts Validator

JSP

JavaScript New Validation Methods

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

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.

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

13-5 Copyright © 2004, Oracle. All rights reserved.

Client-Side Validation

To perform validation by using Struts, you must:1. Create a form bean class2. Overwrite the form bean validate() method,

passing an error to the action3. Create the error message in

ApplicationResources.properties4. Add the input attribute for the form to the action to

indicate where the error message should appear

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

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; }

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

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

<action name="logonbean" path="/logon" input="showLogon.jsp" type="view.LogonAction">

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

13-8 Copyright © 2004, Oracle. All rights reserved.

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" />

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

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

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

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; }}

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

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"); }

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

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.

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

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

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

13-14 Copyright © 2004, Oracle. All rights reserved.

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.

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

13-15 Copyright © 2004, Oracle. All rights reserved.

Utilizing the Struts Validator

Add ValidatorPlugIn to the <plug-in> tag and specify the path for validator-rules.xml and validation.xml:

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

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=usernameLogon.password=password

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

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"><form-validation> <formset>

<form name="logonbean"><field property="password" depends="required" ><arg0 key="logon.password" /></field>

</form></formset></form-valiidation>

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

13-18 Copyright © 2004, Oracle. All rights reserved.

Struts Validator Output

Validator messages can be displayed on the page or in a JavaScript window:

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

13-19 Copyright © 2004, Oracle. All rights reserved.

Exception Handling

Exception handling is implemented by:1. Creating a class for exception handling, which

subclassesorg.apache.struts.action.ExceptionHandler

2. Overriding the execute() method to process the exception

3. Returning an ActionForward object4. Configuring the exception handler in the struts-

config.xml file5. Creating an exception message in

ApplicationResources.properties

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

13-20 Copyright © 2004, Oracle. All rights reserved.

JavaScript

JavaScript is supported in JDeveloper as a simple way to incorporate validation.

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

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.

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

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

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

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