Loading…

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

Like this presentation? Why not share!

Declarative input validation with JSR 303 and ExtVal

on

  • 2,630 views

Slides of my session at J-Fall 2010

Slides of my session at J-Fall 2010

Statistics

Views

Total Views
2,630
Views on SlideShare
2,628
Embed Views
2

Actions

Likes
0
Downloads
5
Comments
0

1 Embed 2

http://www.linkedin.com 2

Accessibility

Categories

Upload Details

Uploaded via as Adobe PDF

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

Declarative input validation with JSR 303 and ExtVal Declarative input validation with JSR 303 and ExtVal Presentation Transcript

  • Declarative input validation with JSR 303 and ExtVal SPREKER : BART KUMMEL E-MAIL : BKUMMEL@TRANSFER-SOLUTIONS.COM DATUM : 3 NOVEMBER 2010 WWW.TRANSFER-SOLUTIONS.COM
  • Who am I? Bart Kummel Nearly 10 years experience in software development Of which 5 years in Java EE Consultant @ Transfer Solutions Competence Manager Java EE @ Transfer Solutions Author of Apache MyFaces 1.2 Web Application Development See http://tinyurl.com/am12wad © COPYRIGHT TRANSFER SOLUTIONS B.V. 2
  • Photo: Salar de Uyuni, Bolivia; © 2010 by © COPYRIGHT TRANSFER SOLUTIONS B.V. Bart Kummel 3
  • Don’t Repeat Yourself Less code = less bugs Duplicated code = duplicated bugs Duplicated code = duplicated maintenance Dupliacted maintenance = forgotten maintenance © COPYRIGHT TRANSFER SOLUTIONS B.V. 4
  • DRY violations in classic Java EE apps Validation is programmed in Model beans Because that’s where it belongs Validation is repeated in View layer Because you have to use JSF Validators Validation is even repeated multiple times in the View Because the same bean is used in multiple JSF pages © COPYRIGHT TRANSFER SOLUTIONS B.V. 5
  • Let’s fix this Remove validation code from View Let View generate validation based on Model How to fix it? That’s why Bean Validation (JSR 303) was created © COPYRIGHT TRANSFER SOLUTIONS B.V. 6
  • JSR 303: the idea Standardized way to express validation constraints Any UI technology can interpret those constraints and enforce them Non-UI technologies can also use the validation information © COPYRIGHT TRANSFER SOLUTIONS B.V. 7
  • JSR 303: the idea implemented JSR 303 is part of Java EE 6 The reference implementation is Hibernate Validator 4.* See http://hibernate.org/subprojects/validator.html Hibernate Validator 4.* can also be used in Java EE 5 A JSR 303 implementation is only the way to express the validation constraints You don’t get UI validation logic if the UI framework doesn’t support JSR 303 © COPYRIGHT TRANSFER SOLUTIONS B.V. 8
  • Bean Validation in Java EE 5 Add Hibernate Validator 4.* as library ...and some extra libraries, provided in the Hibernate Validator package Use JSR 303 annotations in your beans Use MyFaces ExtVal 1.2.* to add declarative validation support to JSF 1.2 © COPYRIGHT TRANSFER SOLUTIONS B.V. 9
  • Bean Validation in Java EE 6 No need to add a JSR 303 implementation JSR 303 is part of the Java EE 6 platform Use JSR 303 annotations in your beans JSF 2.0 has support for JSR 303 annotations out of the box But support is limited You can (and should!) still use ExtVal (2.0.*) and get lots of benefits (more on that later) © COPYRIGHT TRANSFER SOLUTIONS B.V. 10
  • Side note: ExtVal versioning There are three current versions of ExtVal 1.1.* for JSF 1.1 1.2.* for JSF 1.2 2.0.* for JSF 2.0 The latest stable release is release 3 That is: 1.1.3, 1.2.3 and 2.0.3 Lots of exciting new stuff is going into the next version Snapshot releases of ExtVal are very high quality © COPYRIGHT TRANSFER SOLUTIONS B.V. 11
  • Example: classic validation code in bean @Min(0) @Max(100000) private int capacity; public void setCapacity(int capacity) { if(capacity >= 0 && capacity <= 100000) { this.capacity = capacity; } else { // throw exception } } © COPYRIGHT TRANSFER SOLUTIONS B.V. 12
  • Example: JSR 303 annotations @Min(0) @Max(100000) private int capacity; public void setCapacity(int capacity) { this.capacity = capacity; } ts: Extra benefi e –l ess cod adable e –b etter r © COPYRIGHT TRANSFER SOLUTIONS B.V. 13
  • Example: classic validation in JSF page <h:inputText value="#{room.capacity}" > <f:validateLongRange minimum = "0" maximum = "100000" /> </h:inputText> © COPYRIGHT TRANSFER SOLUTIONS B.V. 14
  • Example: no validation in JSF page! <h:inputText value="#{room.capacity}" /> : B enefits – less code ! – DRY © COPYRIGHT TRANSFER SOLUTIONS B.V. 15
  • Demo 1: Bean Validation basics in Java EE 6 16 WWW.TRANSFER-SOLUTIONS.COM
  • So why do we need ExtVal? To use Bean Validation in Java EE 5 / JSF 1.2 To have advanced options in Java EE 6 © COPYRIGHT TRANSFER SOLUTIONS B.V. 17
  • ExtVal on Java EE 6: advanced options Cross validation Violation severity i.o.w. give warnings instead of errors More flexibility in choice of annotations to use JSR 303, JPA, ExtVal, own annotation or any combination Customization on all levels, e.g.: Custom message resolvers Custom validation strategies demos Custom meta data coming up! © COPYRIGHT TRANSFER SOLUTIONS B.V. 18
  • Configuring ExtVal Just add the ExtVal .jar files to your project © COPYRIGHT TRANSFER SOLUTIONS B.V. 19
  • Demo 2: Adding the ExtVal .jar files to our project 20 WWW.TRANSFER-SOLUTIONS.COM
  • Cross validation Examples of cross validation check if two values are equal check if date is before or after other date value is only required if other value is empty (or not) etcetera... © COPYRIGHT TRANSFER SOLUTIONS B.V. 21
  • Demo 3: Cross validation 22 WWW.TRANSFER-SOLUTIONS.COM
  • Demo 3 – Summary @DateIs can be used for date-related cross validations Use DateIsType.before, DateIsType.after or DateIsType.same Other cross validation annotations: @Equals and @NotEquals for equality-based cross validation of any type @RequiredIf for conditional required fields Use RequiredIfType.empty or RequiredIfType.not_empty © COPYRIGHT TRANSFER SOLUTIONS B.V. 23
  • Violation severity Give certain validation rules a severity level of “warning” A warning will be given to the user, but “invalid” data can be submitted © COPYRIGHT TRANSFER SOLUTIONS B.V. 24
  • Demo 4: Setting violation severity to “warning” 25 WWW.TRANSFER-SOLUTIONS.COM
  • Demo 4 – summary Violation severity is not part of the JSR 303 standard We use payload to add violation severity level as custom meta data JPA also interprets JSR 303 contraints before persisting data, but does not recognise violation severity Solution: use ExtVal annotations instead © COPYRIGHT TRANSFER SOLUTIONS B.V. 26
  • Customization on all levels ExtVal is full of customization hooks A lot of ready-made add-ons are available see http://os890.blogspot.com © COPYRIGHT TRANSFER SOLUTIONS B.V. 27
  • Demo 5: Creating a custom annotation and a custom validation strategy 28 WWW.TRANSFER-SOLUTIONS.COM
  • Demo 5 – summary Technically, creating a custom annotation is not an ExtVal feature It is just a Java feature We need an ExtVal validation strategy to make a custom annotation work We need to map our annotation to our validation strategy We can create a startup listener for this As an alternative we can use ExtVal plugins to use alternative ways of configuration © COPYRIGHT TRANSFER SOLUTIONS B.V. 29
  • Summary With annotation based valition, we can finally create DRY JSF applications ExtVal gives us the opportunity to use annotation-based validation on Java EE 5 On Java EE 6, ExtVal gives us: More powerful annotation-based validation More flexibility © COPYRIGHT TRANSFER SOLUTIONS B.V. 30
  • More info... I will put links to slides & demo code on my blog http://www.bartkummel.net Chapter 10 of MyFaces 1.2 Web Application Development http://tinyurl.com/am12wad MyFaces ExtVal: http://myfaces.apache.org/extensions/validator http://os890.blogspot.com/ © COPYRIGHT TRANSFER SOLUTIONS B.V. 31
  • & Questions Answers CONSULTING | MANAGED SERVICES | EDUCATION WWW.TRANSFER-SOLUTIONS.COM © COPYRIGHT TRANSFER SOLUTIONS B.V. 32