SlideShare uma empresa Scribd logo
1 de 32
Baixar para ler offline
WWW.TRANSFER-SOLUTIONS.COM
SPREKER :
E-MAIL :
DATUM :
Declarative input validation
with JSR 303 and ExtVal
BART KUMMEL
BKUMMEL@TRANSFER-SOLUTIONS.COM
3 NOVEMBER 2010
© COPYRIGHT TRANSFER SOLUTIONS B.V. 2
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. 3Photo: Salar de Uyuni, Bolivia; © 2010 by Bart KummelPhoto: Salar de Uyuni, Bolivia; © 2010 by Bart Kummel
© COPYRIGHT TRANSFER SOLUTIONS B.V. 4
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. 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
© COPYRIGHT TRANSFER SOLUTIONS B.V. 6
Remove validation code from View
Let View generate validation based on Model
Let’s fix this
How to fix it?
That’s why Bean Validation (JSR 303) was created
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
© COPYRIGHT TRANSFER SOLUTIONS B.V. 9
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. 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)
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
© COPYRIGHT TRANSFER SOLUTIONS B.V. 12
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. 13
Example: JSR 303 annotations
@Min(0)
@Max(100000)
private int capacity;
public void setCapacity(int capacity) {
this.capacity = capacity;
}
Extra benefits:
– less code
– better readable
© COPYRIGHT TRANSFER SOLUTIONS B.V. 14
Example: classic validation in JSF page
<h:inputText value="#{room.capacity}" >
<f:validateLongRange minimum = "0"
maximum = "100000" />
</h:inputText>
© COPYRIGHT TRANSFER SOLUTIONS B.V. 15
Example: no validation in JSF page!
<h:inputText value="#{room.capacity}" />
Benefits:
– less code
– DRY!
16
WWW.TRANSFER-SOLUTIONS.COM
Demo 1:
Bean Validation basics in Java EE 6
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
Custom meta data
demos
coming
up!
© COPYRIGHT TRANSFER SOLUTIONS B.V. 19
Configuring ExtVal
Just add the ExtVal .jar files to your project
20
WWW.TRANSFER-SOLUTIONS.COM
Demo 2:
Adding the ExtVal .jar files to our project
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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...
22
WWW.TRANSFER-SOLUTIONS.COM
Demo 3:
Cross validation
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
25
WWW.TRANSFER-SOLUTIONS.COM
Demo 4:
Setting violation severity to “warning”
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
28
WWW.TRANSFER-SOLUTIONS.COM
Demo 5: Creating a custom annotation
and a custom validation strategy
© COPYRIGHT TRANSFER SOLUTIONS B.V. 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
© COPYRIGHT TRANSFER SOLUTIONS B.V. 30
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. 31
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. 32
&Q u e s t i o n s
A n s w e r s
CONSULTING | MANAGED SERVICES | EDUCATION
WWW.TRANSFER-SOLUTIONS.COM

Mais conteúdo relacionado

Mais procurados

Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven DevelopmentArif Huda
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETDevLabs Alliance
 
Lecture java basics
Lecture   java basicsLecture   java basics
Lecture java basicseleksdev
 
Top 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdetTop 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdetDevLabs Alliance
 
Top 20 cucumber interview questions for sdet
Top 20 cucumber interview questions for sdetTop 20 cucumber interview questions for sdet
Top 20 cucumber interview questions for sdetDevLabs Alliance
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And RefactoringNaresh Jain
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDevLabs Alliance
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy CodeNaresh Jain
 
Selenium interview-questions-freshers
Selenium interview-questions-freshersSelenium interview-questions-freshers
Selenium interview-questions-freshersNaga Mani
 
May 05 test_code_states
May 05 test_code_statesMay 05 test_code_states
May 05 test_code_statesKyungHo Jung
 
Building a web application with ontinuation monads
Building a web application with ontinuation monadsBuilding a web application with ontinuation monads
Building a web application with ontinuation monadsSeitaro Yuuki
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 
Top 20 software testing interview questions for sdet
Top 20 software testing interview questions for sdetTop 20 software testing interview questions for sdet
Top 20 software testing interview questions for sdetDevLabs Alliance
 
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDETDevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDETDevLabs Alliance
 
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...DevLabs Alliance
 
Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013Hazem Saleh
 

Mais procurados (19)

Android Test Driven Development
Android Test Driven DevelopmentAndroid Test Driven Development
Android Test Driven Development
 
Top 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDETTop 20 basic java interview questions for SDET
Top 20 basic java interview questions for SDET
 
Lecture java basics
Lecture   java basicsLecture   java basics
Lecture java basics
 
Refactoring
RefactoringRefactoring
Refactoring
 
Top 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdetTop 20 Junit interview questions for sdet
Top 20 Junit interview questions for sdet
 
Top 20 cucumber interview questions for sdet
Top 20 cucumber interview questions for sdetTop 20 cucumber interview questions for sdet
Top 20 cucumber interview questions for sdet
 
TDD And Refactoring
TDD And RefactoringTDD And Refactoring
TDD And Refactoring
 
Core java
Core javaCore java
Core java
 
Dev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdetDev labs alliance top 20 basic java interview questions for sdet
Dev labs alliance top 20 basic java interview questions for sdet
 
Working Effectively With Legacy Code
Working Effectively With Legacy CodeWorking Effectively With Legacy Code
Working Effectively With Legacy Code
 
Selenium interview-questions-freshers
Selenium interview-questions-freshersSelenium interview-questions-freshers
Selenium interview-questions-freshers
 
May 05 test_code_states
May 05 test_code_statesMay 05 test_code_states
May 05 test_code_states
 
Building a web application with ontinuation monads
Building a web application with ontinuation monadsBuilding a web application with ontinuation monads
Building a web application with ontinuation monads
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Top 20 software testing interview questions for sdet
Top 20 software testing interview questions for sdetTop 20 software testing interview questions for sdet
Top 20 software testing interview questions for sdet
 
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDETDevLabs Alliance Top 50 Selenium Interview Questions for SDET
DevLabs Alliance Top 50 Selenium Interview Questions for SDET
 
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
DevLabs Alliance Top 20 Software Testing Interview Questions for SDET - by De...
 
Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013Efficient JavaScript Unit Testing, JavaOne China 2013
Efficient JavaScript Unit Testing, JavaOne China 2013
 
Test Automation and Keyword-driven testing af Brian Nielsen, CISS/AAU
Test Automation and Keyword-driven testing af Brian Nielsen, CISS/AAUTest Automation and Keyword-driven testing af Brian Nielsen, CISS/AAU
Test Automation and Keyword-driven testing af Brian Nielsen, CISS/AAU
 

Semelhante a Declarative input validation with JSR 303 and ExtVal

Apache MyFaces 1.2 Web Application Development
Apache MyFaces 1.2 Web Application DevelopmentApache MyFaces 1.2 Web Application Development
Apache MyFaces 1.2 Web Application DevelopmentBart Kummel
 
How do I test these new mobile applications, and how does CA Application Test...
How do I test these new mobile applications, and how does CA Application Test...How do I test these new mobile applications, and how does CA Application Test...
How do I test these new mobile applications, and how does CA Application Test...Ian Kelly
 
Resume_sindhu
Resume_sindhu Resume_sindhu
Resume_sindhu Sindhu B
 
Manual testing interview questions
Manual testing interview questionsManual testing interview questions
Manual testing interview questionsBABAR MANZAR
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsJim Jeffers
 
Improving Software Quality- 2-day Tester Training
Improving Software Quality- 2-day Tester TrainingImproving Software Quality- 2-day Tester Training
Improving Software Quality- 2-day Tester TrainingAnna Russo
 
BDD and Test Automation in Evalutionary Product Suite
BDD and Test Automation in Evalutionary Product SuiteBDD and Test Automation in Evalutionary Product Suite
BDD and Test Automation in Evalutionary Product SuiteLasantha Ranaweera
 
Take Your Web Development To The Next Level With These Top 2 Libraries
Take Your Web Development To The Next Level With These Top 2 LibrariesTake Your Web Development To The Next Level With These Top 2 Libraries
Take Your Web Development To The Next Level With These Top 2 LibrariesPolyxer Systems
 
JCON_15FactorWorkshop.pptx
JCON_15FactorWorkshop.pptxJCON_15FactorWorkshop.pptx
JCON_15FactorWorkshop.pptxGrace Jansen
 
Manual testing interview question by INFOTECH
Manual testing interview question by INFOTECHManual testing interview question by INFOTECH
Manual testing interview question by INFOTECHPravinsinh
 
Using DevOps to Improve Software Quality in the Cloud
Using DevOps to Improve Software Quality in the CloudUsing DevOps to Improve Software Quality in the Cloud
Using DevOps to Improve Software Quality in the CloudTechWell
 
What is Regression Testing Definition, Tools, Examples.pdf
What is Regression Testing Definition, Tools, Examples.pdfWhat is Regression Testing Definition, Tools, Examples.pdf
What is Regression Testing Definition, Tools, Examples.pdfRohitBhandari66
 
Salesforce API Series: Release Management with the Metadata API webinar
Salesforce API Series: Release Management with the Metadata API webinarSalesforce API Series: Release Management with the Metadata API webinar
Salesforce API Series: Release Management with the Metadata API webinarSalesforce Developers
 
Microsoft DevOps AZ-400 Real Dumps 2023
Microsoft DevOps AZ-400 Real Dumps 2023Microsoft DevOps AZ-400 Real Dumps 2023
Microsoft DevOps AZ-400 Real Dumps 2023VictoriaMeisel
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing FundamentalsRichard Paul
 
Manual testing interview questions by infotech
Manual testing interview questions by infotech Manual testing interview questions by infotech
Manual testing interview questions by infotech suhasreddy1
 
Manualtestinginterviewquestionbyinfotech 100901071035-phpapp01
Manualtestinginterviewquestionbyinfotech 100901071035-phpapp01Manualtestinginterviewquestionbyinfotech 100901071035-phpapp01
Manualtestinginterviewquestionbyinfotech 100901071035-phpapp01Anshuman Rai
 
VAST 7.5 and Beyond
VAST 7.5 and BeyondVAST 7.5 and Beyond
VAST 7.5 and BeyondESUG
 

Semelhante a Declarative input validation with JSR 303 and ExtVal (20)

Apache MyFaces 1.2 Web Application Development
Apache MyFaces 1.2 Web Application DevelopmentApache MyFaces 1.2 Web Application Development
Apache MyFaces 1.2 Web Application Development
 
How do I test these new mobile applications, and how does CA Application Test...
How do I test these new mobile applications, and how does CA Application Test...How do I test these new mobile applications, and how does CA Application Test...
How do I test these new mobile applications, and how does CA Application Test...
 
Resume_sindhu
Resume_sindhu Resume_sindhu
Resume_sindhu
 
Manual testing interview questions
Manual testing interview questionsManual testing interview questions
Manual testing interview questions
 
Building Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in RailsBuilding Mobile Friendly APIs in Rails
Building Mobile Friendly APIs in Rails
 
Improving Software Quality- 2-day Tester Training
Improving Software Quality- 2-day Tester TrainingImproving Software Quality- 2-day Tester Training
Improving Software Quality- 2-day Tester Training
 
BDD and Test Automation in Evalutionary Product Suite
BDD and Test Automation in Evalutionary Product SuiteBDD and Test Automation in Evalutionary Product Suite
BDD and Test Automation in Evalutionary Product Suite
 
Take Your Web Development To The Next Level With These Top 2 Libraries
Take Your Web Development To The Next Level With These Top 2 LibrariesTake Your Web Development To The Next Level With These Top 2 Libraries
Take Your Web Development To The Next Level With These Top 2 Libraries
 
JCON_15FactorWorkshop.pptx
JCON_15FactorWorkshop.pptxJCON_15FactorWorkshop.pptx
JCON_15FactorWorkshop.pptx
 
Manual testing interview question by INFOTECH
Manual testing interview question by INFOTECHManual testing interview question by INFOTECH
Manual testing interview question by INFOTECH
 
Using DevOps to Improve Software Quality in the Cloud
Using DevOps to Improve Software Quality in the CloudUsing DevOps to Improve Software Quality in the Cloud
Using DevOps to Improve Software Quality in the Cloud
 
Integration testing.
Integration testing. Integration testing.
Integration testing.
 
What is Regression Testing Definition, Tools, Examples.pdf
What is Regression Testing Definition, Tools, Examples.pdfWhat is Regression Testing Definition, Tools, Examples.pdf
What is Regression Testing Definition, Tools, Examples.pdf
 
Salesforce API Series: Release Management with the Metadata API webinar
Salesforce API Series: Release Management with the Metadata API webinarSalesforce API Series: Release Management with the Metadata API webinar
Salesforce API Series: Release Management with the Metadata API webinar
 
Microsoft DevOps AZ-400 Real Dumps 2023
Microsoft DevOps AZ-400 Real Dumps 2023Microsoft DevOps AZ-400 Real Dumps 2023
Microsoft DevOps AZ-400 Real Dumps 2023
 
Journey toward3rdplatform
Journey toward3rdplatformJourney toward3rdplatform
Journey toward3rdplatform
 
Unit Testing Fundamentals
Unit Testing FundamentalsUnit Testing Fundamentals
Unit Testing Fundamentals
 
Manual testing interview questions by infotech
Manual testing interview questions by infotech Manual testing interview questions by infotech
Manual testing interview questions by infotech
 
Manualtestinginterviewquestionbyinfotech 100901071035-phpapp01
Manualtestinginterviewquestionbyinfotech 100901071035-phpapp01Manualtestinginterviewquestionbyinfotech 100901071035-phpapp01
Manualtestinginterviewquestionbyinfotech 100901071035-phpapp01
 
VAST 7.5 and Beyond
VAST 7.5 and BeyondVAST 7.5 and Beyond
VAST 7.5 and Beyond
 

Último

Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demoHarshalMandlekar2
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningLars Bell
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embeddingZilliz
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxBkGupta21
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersRaghuram Pandurangan
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Manik S Magar
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii SoldatenkoFwdays
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 

Último (20)

Sample pptx for embedding into website for demo
Sample pptx for embedding into website for demoSample pptx for embedding into website for demo
Sample pptx for embedding into website for demo
 
DSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine TuningDSPy a system for AI to Write Prompts and Do Fine Tuning
DSPy a system for AI to Write Prompts and Do Fine Tuning
 
Training state-of-the-art general text embedding
Training state-of-the-art general text embeddingTraining state-of-the-art general text embedding
Training state-of-the-art general text embedding
 
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptxPasskey Providers and Enabling Portability: FIDO Paris Seminar.pptx
Passkey Providers and Enabling Portability: FIDO Paris Seminar.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
unit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptxunit 4 immunoblotting technique complete.pptx
unit 4 immunoblotting technique complete.pptx
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Generative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information DevelopersGenerative AI for Technical Writer or Information Developers
Generative AI for Technical Writer or Information Developers
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!Anypoint Exchange: It’s Not Just a Repo!
Anypoint Exchange: It’s Not Just a Repo!
 
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data PrivacyTrustArc Webinar - How to Build Consumer Trust Through Data Privacy
TrustArc Webinar - How to Build Consumer Trust Through Data Privacy
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko"Debugging python applications inside k8s environment", Andrii Soldatenko
"Debugging python applications inside k8s environment", Andrii Soldatenko
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 

Declarative input validation with JSR 303 and ExtVal

  • 1. WWW.TRANSFER-SOLUTIONS.COM SPREKER : E-MAIL : DATUM : Declarative input validation with JSR 303 and ExtVal BART KUMMEL BKUMMEL@TRANSFER-SOLUTIONS.COM 3 NOVEMBER 2010
  • 2. © COPYRIGHT TRANSFER SOLUTIONS B.V. 2 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
  • 3. © COPYRIGHT TRANSFER SOLUTIONS B.V. 3Photo: Salar de Uyuni, Bolivia; © 2010 by Bart KummelPhoto: Salar de Uyuni, Bolivia; © 2010 by Bart Kummel
  • 4. © COPYRIGHT TRANSFER SOLUTIONS B.V. 4 Don’t Repeat Yourself Less code = less bugs Duplicated code = duplicated bugs Duplicated code = duplicated maintenance Dupliacted maintenance = forgotten maintenance
  • 5. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 6. © COPYRIGHT TRANSFER SOLUTIONS B.V. 6 Remove validation code from View Let View generate validation based on Model Let’s fix this How to fix it? That’s why Bean Validation (JSR 303) was created
  • 7. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 8. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 9. © COPYRIGHT TRANSFER SOLUTIONS B.V. 9 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
  • 10. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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)
  • 11. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 12. © COPYRIGHT TRANSFER SOLUTIONS B.V. 12 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 } }
  • 13. © COPYRIGHT TRANSFER SOLUTIONS B.V. 13 Example: JSR 303 annotations @Min(0) @Max(100000) private int capacity; public void setCapacity(int capacity) { this.capacity = capacity; } Extra benefits: – less code – better readable
  • 14. © COPYRIGHT TRANSFER SOLUTIONS B.V. 14 Example: classic validation in JSF page <h:inputText value="#{room.capacity}" > <f:validateLongRange minimum = "0" maximum = "100000" /> </h:inputText>
  • 15. © COPYRIGHT TRANSFER SOLUTIONS B.V. 15 Example: no validation in JSF page! <h:inputText value="#{room.capacity}" /> Benefits: – less code – DRY!
  • 17. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 18. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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 Custom meta data demos coming up!
  • 19. © COPYRIGHT TRANSFER SOLUTIONS B.V. 19 Configuring ExtVal Just add the ExtVal .jar files to your project
  • 20. 20 WWW.TRANSFER-SOLUTIONS.COM Demo 2: Adding the ExtVal .jar files to our project
  • 21. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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...
  • 23. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 24. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 26. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 27. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 28. 28 WWW.TRANSFER-SOLUTIONS.COM Demo 5: Creating a custom annotation and a custom validation strategy
  • 29. © COPYRIGHT TRANSFER SOLUTIONS B.V. 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
  • 30. © COPYRIGHT TRANSFER SOLUTIONS B.V. 30 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
  • 31. © COPYRIGHT TRANSFER SOLUTIONS B.V. 31 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/
  • 32. © COPYRIGHT TRANSFER SOLUTIONS B.V. 32 &Q u e s t i o n s A n s w e r s CONSULTING | MANAGED SERVICES | EDUCATION WWW.TRANSFER-SOLUTIONS.COM