SlideShare uma empresa Scribd logo
1 de 32
Baixar para ler offline
Declarative input validation
with JSR 303 and ExtVal
April 13th, 2011 – Bart Kummel
Who am I?


●   Bart Kummel
●   10 years experience
    in software development
    ●   Of which 6 years in Java EE
●   Consultant @ Transfer Solutions,
    Leerdam, NL
●   Competence manager @
    Transfer Solutions, Leerdam, NL
●   Author of Apache MyFaces 1.2
    Web Application Development
    ●   See http://tinyurl.com/am12wad


                                         2
Photo: Salar de Uyuni, Bolivia; © 2010 by Bart Kummel   3
Don’t Repeat Yourself



●   Less code = less bugs

●   Duplicated code = duplicated bugs

●   Duplicated code = duplicated maintenance

●   Dupliacted maintenance = forgotten maintenance




                                                     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




                                                              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




                                                       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




                                                                    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




                                                                         8
How to add Bean Validation to a Java EE 5 application



●   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




                                                        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)




                                                                     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




                                                                11
Example: classic validation code in bean




private int capacity;

public void setCapacity(int capacity) {
  if(capacity >= 0 && capacity <= 100000) {
    this.capacity = capacity;
  } else {
    // throw exception
  }
}




                                              12
Example: JSR 303 annotations



@Min(0)
@Max(100000)
private int capacity;

public void setCapacity(int capacity) {
  this.capacity = capacity;
}



                                         enefits:
                                 Extra b
                                 – less code
                                             eadable
                                  – better r


                                                       13
Example: classic validation in JSF page



<h:inputText value="#{room.capacity}" >
  <f:validateLongRange minimum = "0"
                       maximum = "100000" />
</h:inputText>




                                               14
Example: no validation in JSF page!



<h:inputText value="#{room.capacity}" />




                                           15
Demo 1:
Bean Validation basics in Java EE 6
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




                                                    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!
                                                                  18
Configuring ExtVal



●   Just add the ExtVal .jar files to your project




                                                    19
Demo 2:
Adding the ExtVal .jar files to our project
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...




                                                                  21
Demo 3:
Cross validation
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




                                                                        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




                                                                     24
Demo 4:
Setting violation severity to “warning”
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




                                                                             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




                                                 27
Demo 5:
Creating a custom annotation and a
custom validation strategy
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


                                                                                 29
Summary



●   With annotation based validation, 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




                                                    30
More info...

●   Workshop tomorrow: ‘Rule your model with Bean-Validation’
    ●   08:00 – 10:00, Room 2 – Gerhard Petracek

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

                                                                31
Questions & answers




     A wise man can learn more from a
    foolish question than a fool can learn
             from a wise answer.
                                  Bruce Lee




                                              32

Mais conteúdo relacionado

Mais procurados

Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit TestingMike Lively
 
Realtime selenium interview questions
Realtime selenium interview questionsRealtime selenium interview questions
Realtime selenium interview questionsKuldeep Pawar
 
Testing untestable code - STPCon11
Testing untestable code - STPCon11Testing untestable code - STPCon11
Testing untestable code - STPCon11Stephan Hochdörfer
 
Testing untestable code - phpconpl11
Testing untestable code - phpconpl11Testing untestable code - phpconpl11
Testing untestable code - phpconpl11Stephan Hochdörfer
 
Mockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksMockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksEndranNL
 
Lecture java basics
Lecture   java basicsLecture   java basics
Lecture java basicseleksdev
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkHumberto Marchezi
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnitinTwentyEight Minutes
 
Efficient JavaScript Unit Testing, March 2013
Efficient JavaScript Unit Testing, March 2013Efficient JavaScript Unit Testing, March 2013
Efficient JavaScript Unit Testing, March 2013Hazem Saleh
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesNarendra Pathai
 
Dev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_testDev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_testMasanori Kato
 
Test and refactoring
Test and refactoringTest and refactoring
Test and refactoringKenneth Ceyer
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2Tricode (part of Dept)
 

Mais procurados (19)

Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Spring frame work
Spring frame workSpring frame work
Spring frame work
 
Automated Unit Testing
Automated Unit TestingAutomated Unit Testing
Automated Unit Testing
 
Realtime selenium interview questions
Realtime selenium interview questionsRealtime selenium interview questions
Realtime selenium interview questions
 
Testing untestable code - STPCon11
Testing untestable code - STPCon11Testing untestable code - STPCon11
Testing untestable code - STPCon11
 
Testing untestable code - phpconpl11
Testing untestable code - phpconpl11Testing untestable code - phpconpl11
Testing untestable code - phpconpl11
 
Mockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworksMockito vs JMockit, battle of the mocking frameworks
Mockito vs JMockit, battle of the mocking frameworks
 
Lecture java basics
Lecture   java basicsLecture   java basics
Lecture java basics
 
Android develop guideline
Android develop guidelineAndroid develop guideline
Android develop guideline
 
C++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing FrameworkC++ Unit Test with Google Testing Framework
C++ Unit Test with Google Testing Framework
 
Unit testing best practices with JUnit
Unit testing best practices with JUnitUnit testing best practices with JUnit
Unit testing best practices with JUnit
 
Efficient JavaScript Unit Testing, March 2013
Efficient JavaScript Unit Testing, March 2013Efficient JavaScript Unit Testing, March 2013
Efficient JavaScript Unit Testing, March 2013
 
Test driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practicesTest driven development - JUnit basics and best practices
Test driven development - JUnit basics and best practices
 
Php unit (eng)
Php unit (eng)Php unit (eng)
Php unit (eng)
 
Dev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_testDev fest kyoto_2021-flutter_test
Dev fest kyoto_2021-flutter_test
 
Test and refactoring
Test and refactoringTest and refactoring
Test and refactoring
 
Test Driven Development
Test Driven DevelopmentTest Driven Development
Test Driven Development
 
Unit testing php-unit - phing - selenium_v2
Unit testing   php-unit - phing - selenium_v2Unit testing   php-unit - phing - selenium_v2
Unit testing php-unit - phing - selenium_v2
 
TDD & BDD
TDD & BDDTDD & BDD
TDD & BDD
 

Semelhante a Declarative Input Validation with JSR 303 and ExtVal

Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Gl...
Testing Rest with Spring  by Kostiantyn Baranov (Senior Software Engineer, Gl...Testing Rest with Spring  by Kostiantyn Baranov (Senior Software Engineer, Gl...
Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Gl...GlobalLogic Ukraine
 
Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Ivan Ivanov
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkOnkar Deshpande
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...Dan Allen
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your codePascal Larocque
 
Testing experience - Vision team, Mar 2016
Testing experience - Vision team, Mar 2016Testing experience - Vision team, Mar 2016
Testing experience - Vision team, Mar 2016Van Huong
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Hong Le Van
 
Introduzione a junit + integrazione con archibus
Introduzione a junit + integrazione con archibusIntroduzione a junit + integrazione con archibus
Introduzione a junit + integrazione con archibusDavide Fella
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)Thierry Gayet
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...DicodingEvent
 
Java EE web project introduction
Java EE web project introductionJava EE web project introduction
Java EE web project introductionOndrej Mihályi
 
Build, logging, and unit test tools
Build, logging, and unit test toolsBuild, logging, and unit test tools
Build, logging, and unit test toolsAllan Huang
 
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023Sam Brannen
 
Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Sam Becker
 
Testing with JUnit 5 and Spring
Testing with JUnit 5 and SpringTesting with JUnit 5 and Spring
Testing with JUnit 5 and SpringVMware Tanzu
 

Semelhante a Declarative Input Validation with JSR 303 and ExtVal (20)

Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Gl...
Testing Rest with Spring  by Kostiantyn Baranov (Senior Software Engineer, Gl...Testing Rest with Spring  by Kostiantyn Baranov (Senior Software Engineer, Gl...
Testing Rest with Spring by Kostiantyn Baranov (Senior Software Engineer, Gl...
 
Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!Test it! Unit, mocking and in-container Meet Arquillian!
Test it! Unit, mocking and in-container Meet Arquillian!
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
Throwing complexity over the wall: Rapid development for enterprise Java (Jav...
 
Test driven development - Zombie proof your code
Test driven development - Zombie proof your codeTest driven development - Zombie proof your code
Test driven development - Zombie proof your code
 
Unit testing (eng)
Unit testing (eng)Unit testing (eng)
Unit testing (eng)
 
Testing experience - Vision team, Mar 2016
Testing experience - Vision team, Mar 2016Testing experience - Vision team, Mar 2016
Testing experience - Vision team, Mar 2016
 
Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++Test driven development and unit testing with examples in C++
Test driven development and unit testing with examples in C++
 
TDD - Unit Testing
TDD - Unit TestingTDD - Unit Testing
TDD - Unit Testing
 
Introduzione a junit + integrazione con archibus
Introduzione a junit + integrazione con archibusIntroduzione a junit + integrazione con archibus
Introduzione a junit + integrazione con archibus
 
A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)A la découverte des google/test (aka gtest)
A la découverte des google/test (aka gtest)
 
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
Play with Testing on Android - Gilang Ramadhan (Academy Content Writer at Dic...
 
Java EE web project introduction
Java EE web project introductionJava EE web project introduction
Java EE web project introduction
 
Build, logging, and unit test tools
Build, logging, and unit test toolsBuild, logging, and unit test tools
Build, logging, and unit test tools
 
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
Testing with Spring, AOT, GraalVM, and JUnit 5 - Spring I/O 2023
 
Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8Test all the things! Automated testing with Drupal 8
Test all the things! Automated testing with Drupal 8
 
Spring Test Framework
Spring Test FrameworkSpring Test Framework
Spring Test Framework
 
Testacular
TestacularTestacular
Testacular
 
Testing with JUnit 5 and Spring
Testing with JUnit 5 and SpringTesting with JUnit 5 and Spring
Testing with JUnit 5 and Spring
 

Último

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfhans926745
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsMaria Levchenko
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)wesley chun
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdflior mazor
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityPrincipled Technologies
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?Igalia
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 

Último (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 

Declarative Input Validation with JSR 303 and ExtVal

  • 1. Declarative input validation with JSR 303 and ExtVal April 13th, 2011 – Bart Kummel
  • 2. Who am I? ● Bart Kummel ● 10 years experience in software development ● Of which 6 years in Java EE ● Consultant @ Transfer Solutions, Leerdam, NL ● Competence manager @ Transfer Solutions, Leerdam, NL ● Author of Apache MyFaces 1.2 Web Application Development ● See http://tinyurl.com/am12wad 2
  • 3. Photo: Salar de Uyuni, Bolivia; © 2010 by Bart Kummel 3
  • 4. Don’t Repeat Yourself ● Less code = less bugs ● Duplicated code = duplicated bugs ● Duplicated code = duplicated maintenance ● Dupliacted maintenance = forgotten maintenance 4
  • 5. 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 5
  • 6. 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 6
  • 7. 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 7
  • 8. 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 8
  • 9. How to add Bean Validation to a Java EE 5 application ● 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 9
  • 10. 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) 10
  • 11. 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 11
  • 12. Example: classic validation code in bean private int capacity; public void setCapacity(int capacity) { if(capacity >= 0 && capacity <= 100000) { this.capacity = capacity; } else { // throw exception } } 12
  • 13. Example: JSR 303 annotations @Min(0) @Max(100000) private int capacity; public void setCapacity(int capacity) { this.capacity = capacity; } enefits: Extra b – less code eadable – better r 13
  • 14. Example: classic validation in JSF page <h:inputText value="#{room.capacity}" > <f:validateLongRange minimum = "0" maximum = "100000" /> </h:inputText> 14
  • 15. Example: no validation in JSF page! <h:inputText value="#{room.capacity}" /> 15
  • 16. Demo 1: Bean Validation basics in Java EE 6
  • 17. 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 17
  • 18. 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! 18
  • 19. Configuring ExtVal ● Just add the ExtVal .jar files to your project 19
  • 20. Demo 2: Adding the ExtVal .jar files to our project
  • 21. 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... 21
  • 23. 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 23
  • 24. 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 24
  • 25. Demo 4: Setting violation severity to “warning”
  • 26. 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 26
  • 27. Customization on all levels ● ExtVal is full of customization hooks ● A lot of ready-made add-ons are available ● see http://os890.blogspot.com 27
  • 28. Demo 5: Creating a custom annotation and a custom validation strategy
  • 29. 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 29
  • 30. Summary ● With annotation based validation, 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 30
  • 31. More info... ● Workshop tomorrow: ‘Rule your model with Bean-Validation’ ● 08:00 – 10:00, Room 2 – Gerhard Petracek ● 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/ 31
  • 32. Questions & answers A wise man can learn more from a foolish question than a fool can learn from a wise answer. Bruce Lee 32