the validator framework the validator framework
play

The Validator Framework The Validator Framework Chuck Cavaness - PowerPoint PPT Presentation

The Validator Framework The Validator Framework Chuck Cavaness Chuck Cavaness Chuck Cavaness Introduction Introduction Senior Technologist (S1 Corporation) Senior Technologist (S1 Corporation) Co Co- -Author of Special


  1. The Validator Framework The Validator Framework Chuck Cavaness Chuck Cavaness Chuck Cavaness

  2. Introduction Introduction • Senior Technologist (S1 Corporation) Senior Technologist (S1 Corporation) • • Co Co- -Author of Special Edition EJB 2.0 and Special Author of Special Edition EJB 2.0 and Special • Edition Java 2 Edition Java 2 • Author of Jakarta Struts from O’Reilly and Struts Author of Jakarta Struts from O’Reilly and Struts • Pocket Reference Pocket Reference • Various Articles for O’Reilly, Linux Magazine, Various Articles for O’Reilly, Linux Magazine, • JavaWorld, etc. JavaWorld, etc.

  3. Agenda Agenda • The Need for Validation The Need for Validation • • Basic Validation Concepts Basic Validation Concepts • • Introduce the Validator Framework Introduce the Validator Framework • • Validator with/without Struts Validator with/without Struts • • Discuss Validator Best Practices Discuss Validator Best Practices •

  4. What’s the Big Deal? What’s the Big Deal? ! Lexical Validation Lexical Validation ! ! Syntactic Validation Syntactic Validation ! ! Semantic Validation Semantic Validation !

  5. Lexical Validation Lexical Validation • Is the single value “well Is the single value “well- -formed”? formed”? • • The Structure of the data is correct The Structure of the data is correct • • But nothing can be said of the content But nothing can be said of the content • • Can typically be done on the client or server Can typically be done on the client or server • • E.g. Did the user enter an integer for an age E.g. Did the user enter an integer for an age • field? field?

  6. Syntactic Validation Syntactic Validation • Well Well- -formed, but does the data follow formed, but does the data follow • the proper syntax the proper syntax • Can typically be done on client, but must Can typically be done on client, but must • watch out for I18N issues watch out for I18N issues • E.g < day> /< month> /< year> E.g < day> /< month> /< year> •

  7. Semantic Validation Semantic Validation • Does the data make sense for your Does the data make sense for your • domain domain • Should be done on server Should be done on server • • E.g. You asked for a future ship date, but E.g. You asked for a future ship date, but • the user entered a date in the past the user entered a date in the past

  8. The Need for Validation The Need for Validation Internal Applications JDBC Web Server/ Servlet Container Cluster Enterprise Native Batch RMI/ IIOP Data JDBC AS4 0 0 RMI/ IIOP SOAP/ XML W AP Server Web Services J2 EE Cluster Business Partners

  9. Where does the Validation Where does the Validation Live? Live? • Business Logic Validation Business Logic Validation • • Presentation Validation Presentation Validation •

  10. Presentation vs. Business Logic Presentation vs. Business Logic Authenticate( ) Enterprise J2 EE SQL Data Query Server EJB I nvocation HttpResponse Web Server/ Servlet HttpRequest HttpRequest Container

  11. Validation without the Validation without the Validator Validator • public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { Request request) { • public ActionErrors validate(ActionMapping mapping, HttpServlet • ActionErrors errors = new ActionErrors(); • ActionErrors errors = new ActionErrors(); • / / Get access to the message resources for this application • / / Get access to the message resources for this application • / / There's not an easy way to access the resources from an ActionForm ctionForm • / / There's not an easy way to access the resources from an A • MessageResources resources = • MessageResources resources = • (MessageResources)request.getAttribute( Globals.MESSAGES_KEY ); EY ); • (MessageResources)request.getAttribute( Globals.MESSAGES_K • / / Validate that the usernam e entered was not null • / / Validate that the usernam e entered was not null • if(getUsernam e() = = null | | getUsernam e().length() < 1) { • if(getUsernam e() = = null | | getUsernam e().length() < 1) { • String usernam eLabel = resources.getMessage( "label.usernam e" ); " ); • String usernam eLabel = resources.getMessage( "label.usernam e • errors.add( ActionErrors.GLOBAL_ERROR, • errors.add( ActionErrors.GLOBAL_ERROR, • new ActionError("errors.required", usernam eLabel )); el )); • new ActionError("errors.required", usernam eLab • } • } • / / Validate that the password entered was not null • / / Validate that the password entered was not null • if(getPassword() = = null | | getPassword().length() < 1) { • if(getPassword() = = null | | getPassword().length() < 1) { • String passwordLabel = resources.getMessage( "label.password" ); " ); • String passwordLabel = resources.getMessage( "label.password • errors.add( ActionErrors.GLOBAL_ERROR, • errors.add( ActionErrors.GLOBAL_ERROR, • new ActionError("errors.required", passwordLabel )); el )); • new ActionError("errors.required", passwordLab • } • } • return errors; • return errors; • } • }

  12. Extrapolating this Approach to Validating Extrapolating this Approach to Validating Order Order Order Logon Customer Logon Form Form Form Form Form Form Account Number, Email, Username and Password Contact Information, etc. Order Order Order Form Form Form Credit Card, Quantity, Shipping Address, etc.

  13. The Validator Framework The Validator Framework • Open source Validation framework Open source Validation framework • • Developed by David Winterfeldt Developed by David Winterfeldt • • Integrated into Struts core during 1.1 Integrated into Struts core during 1.1 •

  14. Features of the Validator Features of the Validator • Supports Client Supports Client- -Side (JavaScript) and Server Side (JavaScript) and Server- - • Side (Java) validation Side (Java) validation • Comes with many pre Comes with many pre- -built validation routines built validation routines • • Allows for regular expressions to be used Allows for regular expressions to be used • • Can be used with or without Struts Can be used with or without Struts • • Pluggable Validators Pluggable Validators •

  15. Architecture of the Validator Architecture of the Validator Action Application Framework Actions Form s Struts Framework Request Processor Plugin Validator Validator Framework Validators Action Form s Validator Application Rules Validator Repository Mappings

  16. Validator Sequence Diagram Validator Sequence Diagram Login Form ActionServlet RequestProcessor ActionForm Validator doPost process() {login.do was sent in request} processValidate() validate() validate()

  17. Basic Validation Rules Basic Validation Rules • Required Required • Date Date • • • Mask Mask • Credit Card Credit Card • • • Range( Range( intRange, • Email Email • • intRange, ) floatRange ) floatRange • Byte,short,Integer Byte,short,Integer • • MaxLength MaxLength • • Long,Double,Float Long,Double,Float • • MinLength MinLength •

  18. Using the Validator with Using the Validator with Struts Struts 1. Add the Validator Plugin Add the Validator Plugin 1. 2. Configure the Validation.xml File Configure the Validation.xml File 2. 3. Extend the Validator ActionForms or use Extend the Validator ActionForms or use 3. Dynamic ActionForms Dynamic ActionForms 4. Set validate= “true” on Action Mappings Set validate= “true” on Action Mappings 4.

  19. Adding Validator Plugin Adding Validator Plugin <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml"/> </plug-in>

  20. Validator Configuration Files Validator Configuration Files • validator validator- -rules.xml rules.xml • • validation.xml validation.xml •

  21. The Rules Configuration File The Rules Configuration File <validator name="required" classname="org.apache.struts.util.StrutsValidator" method="validateRequired" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.action.ActionErrors, javax.servlet.http.HttpServletRequest" msg="errors.required"> //…some text deleted

  22. Validation Configuration File Validation Configuration File … < form name= “ loginForm "> < field property= “ em ail " depends= "required"> < arg0 key= “ label.em ail "/ > < / field> < field property= “ passw ord " depends= "required"> < arg0 key= “ label.passw ord "/ > < / field> < / form> …

Recommend


More recommend