hi i'm cameron mckenzie at cameron mcnz on twitter and i wanted to talk to you about
doing annotation based validation of data in struts say goodbye to that old validate method
say hello to annotation based validators as you can see my application here which takes a
first name last name email and age it does have a validate method but i want to get rid of this
standard validate method because i want to start doing validation by using some struts
annotations now as you can see i take input here i've got first name last name email age i
can make that actually look a little bit better by clicking on the display properties properly
i'd actually like to add two more fields here i like to add a website and ask a person
what their phone number is as well so i'm going to add a couple more fields here i'm going to
add in as type string website and i'm also going to add in of type string their phone
number a couple of extra things we'll want to learn about the user and as always you want to
or you want to add the getters and setters there so let getters and setters for phone
and since i've got that i don't have those fields yet for the form so i'm going to open up the
register form and add those fields in too if we want to do validation on them we've got to have
input for them and so that was website and that was the phone number now also personbean.website
and phone we want to include those variables here in the register action properties it's just
just kind of making sure that something prints out there something prints out there
when we ask them for their information so what do you have website and phone
okay and now when we rerun our register.htm file we should see that all of these fields do get
populated and there we go that looks good display the properties properly there we go
and your website phone number now i did comment out the validation here so you'll notice that if i
click submit well it all goes right through by the way if i really wanted to be tight
here i would go in and delete the tostring method and just recreate it again so that we
include all of the new properties that were added anyway just a little ism there but yeah as you can
see we're not doing any validation it just goes right through as soon as we type anything in so
we can put as much junk in here as we want in fact if we do that with age if we put something alpha
alphabetical for age i think that might even cause an exception okay so it doesn't cause an exception
but it does does cause a bit of a problem so there we go we want to actually have proper validation
in our example so what's the easiest way to do it well the easiest way is to go to your
action class find the execute method and then just specify that there are going to be some
validations going on here so i'll type validations in there and you've got to open and close that
bracket so that's what i'm working out right here and i'm going to say that there's going to be a
couple of required strings and so i'll say right off the bat i'm going to need a lot of room here
that we're going to require the last name and first name we are also going to require that the
email be validated so i can paste that in as well so you can see here emails need to be validated
there we go and line that up nicely so we've got the email validator okay that looks good i think
we've got a little bit of problem with organizing imports here so i'm going to do source organize
imports although this isn't going to solve all of my errors it's going to solve a few of them
i'm going to say with the url i'm going to use a url validator so after this
i'll type in that url validator a little formatting just to make this look handsome
okay so we use the url validator on the person's website we got the email validator on the email we
can do a regex validator on the phone number make sure that the phone number is provided correctly
now the red jacks is going to be a little bit more intense as you would imagine with doing
a little bit of red jacks but let's take a look at that it's a simple validator ask
for a phone number it's on the phone number field and there's the regex expression there
i don't think it's too complicated but basically it wants numbers between 0 and 9 and and i think
what only includes dashes or brackets around it just a simple regex for the phone number
and then finally we want to do range for the end we want an int range validator
you need a comma in between each of these regexes there you can see the int range
fields added in there i'm going to do an organize import source
organize imports click control s and save and so now with this execute method i've specified
all of the different requirements that i have for validation i want to make sure that the last name
and first name are there i want the email to use an email validator the website to use the
url validator i'm going to use a little bit of red jacks on the phone number because phone
numbers can get a little bit a little bit ornery there and then finally i want an int
range field validator and i've actually done something pretty cool here notice that i've
said the minimum age is going to be 18 and then i've used that variable so if somebody
actually changes the variable or something and we want to print it out it'll say must be over
and that'll always print whatever that minimum value is right there so get that gets the
validator going that now eliminates the need for that validate method as you can see the validate
method is commented out i'm going to click save i'm going to close all other tabs there
to clean things up a little bit clear that window maybe even restart the server never a bad idea
then i'm going to rerun the register page say run on server and i'll type in i'll just click
submit right away what happens see it kills the name doesn't like the first name last name
if the website is malformed or the phone number is malformed notice it
says hey you must provide a valid phone number
and i don't know why the website isn't working let's go take a look at their register action here
urls personbean.org
and it does look like it's using the url validator let's give this a try so cameron
mckenzie me at me.com say i'm 19
and then your phone number
click submit
and you know what let's just take a look at i think the reason why that
register wasn't working there is this person being website is the key value pair
and then if i go to the person class this private string website
and that's right i never had the setters and getters for website there so okay so i'll
try that one more time everything looked like it was working except that one field
let's run that on the server one more time
and let's see what happens if we don't put in a good website
now we've got the enter a valid url so there you go i just hadn't made sure that i had all the
setters and getters inside of my person class when i added that website field and now it's
actually put them in an ugly spot so i'm going to clean that up too but there you go that's the
idea that's how easy it is to do validation if you start using annotations some of the built-in
annotations that come with struts and you can see them all right here part of the exwork validator
annotations framework and there you go that's all there is to doing annotation based validation with
struts now if you enjoyed this tutorial why don't you head over to the serverside.com i'm
the editor-in-chief over there we've got great set of tutorials on strut spring jsf hibernate
you name it anything to do with enterprise software development we cover it over there
if you're interested in my personal annex follow me on twitter cameronmcnz and subscribe on youtube