Saturday, August 1, 2009

More cool in Java EE 6 -- JSF 2.0, Bean Validation

There's definitely more cool stuff in JSF 2.0 that I didn't get to last time -- so let's get right to it...

Annotations for Converters and Validators
The @FacesConverter and @FacesValidator annotations are nice little additions to the framework... they're simple to use -- you write your converter and validator classes in the same way as you always did, but instead of needing to crack open your already-way-too-large-faces-config.xml and look up the syntax for the converter tags, you can now rely on your IDE to help you out -- just throw @FacesConverter on the class with the 'forClass' attribute, and it will automatically register that converter for all form fields of the appropriate class... it doesn't get much easier than that, and of course you can use the 'value' attribute in the same way you used to use the 'converter-id' tag in the faces-config.xml, and then reference it in any of your pages...

Ok, you're saying -- so I get to add a line to my class file and take four lines out of an xml file -- so what? But wait, there's more -- and this is my favorite part... throw an empty faces-config.xml file in your META-INF folder, and these converters and validators will automatically be registered for any JSF web app that includes this jar! This is a great mechanism to promote reuse -- create a utility component that has a bunch of these files, and every one of your web apps can get them with a couple of lines in a Maven .pom file (or whatever it is you use for your builds)... I suspect it won't take long for standard sets of converters and validators to become available in component libraries like RichFaces or IceFaces...

Bean Validation
Speaking of validation, here's where it gets interesting -- with the new Bean Validation integration, it's no longer necessary to think about validation just in terms of JSF... I personally believe that validation could be better approached as an integral part of a well designed domain model (think Domain-Driven Design here), so putting that logic in a JSF specific validator has always seemed awkward -- likewise, adding a 'validate()' method that needs to be called while processing a form submit or just before persisting your object (in the case of a JPA Entity, for example) is just as awkward, if not worse... not only that, but many applications are more than just web apps -- some have batch-processing or scheduled aspects to them which don't have any exposure to JSF... using JSF validators in these cases would be a bit smelly, and duplicating the code is even worse -- this is where Bean Validation comes in...

With the annotations in the javax.validation.constraints package, we can now annotate our domain fields with things like @Min, @Max, @NotNull, @Pattern, etc, so that the validation rules that our domain objects abide by are defined right along-side the fields themselves... there is, of course, an API available for evaluating the rules against a given object, but here's where it gets cool -- JSF and JPA both provide support for this automagically! That means whenever you have a domain object that is exposed through a ManagedBean in a form, the validation rules will be executed for you, and any failures are made available through the standard h:message tags! Not only that, but the validation can also be executed when you persist or merge your domain object via JPA, so batch-processing applications are covered as well -- very cool...

A very similar incarnation has been available through the Hibernate Validation and Seam projects, but it is great to see this get worked into the standards... The basic constraints here are lean, but take care of most use cases -- I have experimented with some simple custom validators, but am looking forward to getting into some more complex rules with multiple fields and see how it handles them...

Still more?
There's still more stuff in EE 6 that I'd like to talk about, including the Facelets support and the new Composite Component stuff, but I need to do a little more fiddling with that -- I came across the new 'Behavior' API as well, which looks like a slick way to make your components do what you want, as well as make them extensible... At some point, I'm going to get my hands on Web Beans, the new JPA 2.0 capabilities, Singleton EJB's, Asynchronous EJB's, etc -- there is a lot of good stuff in this new spec, so stay tuned!

M

3 comments:

Ryan said...

It is great to hear how far JSF has come. Annotations make development a lot easier. It has improved my productivity with Spring MVC significantly. I am going to have to give JSF another shot.

Anonymous said...

Hi, in jsf 2.0 version why the method in the converter getAsObject () is getting called thrice instead of one time. I am using jboss 6 version
Is the converter behaviour changed in jsf 2.0 version .

Anna said...

Great and Useful Article.

J2EE Training

Java EE course

Java EE training

J2EE training in chennai

Java J2EE Training Institutes in Chennai

Java J2EE Training in Chennai

Java EE training

Java Interview Questions