1 / 23

Adding Validation and Error Handling

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()

jlai
Download Presentation

Adding Validation and Error Handling

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Adding Validation and Error Handling

  2. 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. 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

  4. 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. 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. 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. 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">

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

  9. 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. 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. 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. Validation Results • 2. User enters no data (form bean validation). • 1. User enters incorrect login (action validation). • 3. User enters correct login.

  13. 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. 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.

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

  16. 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. 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>

  18. Struts Validator Output • Validator messages can be displayed on the page or in a JavaScript window:

  19. 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. JavaScript • JavaScript is supported in JDeveloper as a simple way to incorporate validation.

  21. Enhancing the View • Use control hints to modify the way an attribute is displayed in a client.

  22. 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. 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

More Related