5/03/2011

JSR 303: Validation in JSF 2.0 - Part 1

This post is about the JSR 303 validation that JSF 2.0 applications may use. We will create a first "Hello World" example and won´t go that much into detail. First, download the reference implementation of JSR 303:


http://www.hibernate.org/subprojects/validator.html




1) Create a new JSF 2.0 Dynamic Web Project 




As you can see, I created a ManageBean called "MyBean". Then I added all hibernate jars from the downloaded jar file to the WEB-INF\lib dir. This is just to keep the functionality in the web application. If you want, you can add them to your servers lib dir. Then I created a blank "index.xhtml" which we will use to reference the bean.





















2) Create the Bean Class






Here is the snippet of the ManagedBean I created for this example. Annotate the bean with "@ManagedBean", then create a simple String that will hold our input.
Annotate it with the hibernate annotation"@NotEmpty". This will prevent the user from submitting a form where entries are missing.






3) Create the view




Create a simple JSF 2.0 application: Add an input field and a commandButton: That´s it ! 


You can test the validation by entering nothing into the input field and clicking the submit button.






If you like this tutorial it would be very nice, if you could click on some of the google ads you see on the right side. It helps me run this block and motivates me ;)

If you have any questions, feel free to leave a comment.