hey i'm cameron mckenzie at cameron mcnz on twitter and i want to talk about strut
form handling i'm going to be creating a little form here that takes the first name last name
email address and age creates a person object calls on a struts action the action
will initialize all these properties in that person javabean and then print out hey you
know we got all this information so that's going to show you how to do some form handling in this
particular tutorial now i will tell you that this tutorial actually follows the processing forms
tutorial over on struts.apache.org so if you're watching this tutorial you actually want to see
some of the text and and kind of read what i'm doing and step through it through text as well you
can head over there and find that tutorial so i've got just a basic struts application started here
i wouldn't even call it a struts application i've just set up the palm file with all of the required
dependencies and i put in the web.xml file that's got the basic struts prepare and execute
filter in there and now i'm going to actually start creating a bit of a struts application
so the goal here is to actually create a little program that can simulate the
registration of a user of a person so i'm going to create a new package called com.mcnz.struts
and i'm going to create a class called i'm going to create a class called
person this is going to represent the person class and a person is going to be
able to register so we're going to have an html form that allows a user to tell us what their
first name is we'll also allow that user to tell us what their last name is their email address is
and their name i'm going to update these so that should be the last name and then
that should be email and then this will be their age and we're even going to
do some razzle dazzle there and make that of type in now of course whenever you've
got these in a pojo you always want to add your getters and setters to that
and of course uh never hurts to actually have a tostring method as well so i'm going to go source
generate a tostring method and add all of those fields in there now if i just do systemout.print
person you'll get to see their first name and last name that just
that just makes a little bit of sense there anyways it's just a nice little person pojo
that i can use to help register a user now really the interesting part here is
this registration form i want to create a little registration form i'm going to call it register
we're going to create the file in which the form is contained register.jsp that's my java server
page and like all java server pages you want to start it off with a reference to the struts
tag library and then also put in a page directive there as well and by by doing so you're going to
make sure that this page is fully supported in terms of java and java web development and web
runtime so there's your taglib that imports the struts tags make those available to the
page and then yeah we've got the little page directive there that just lets java know that
it's going to generate some html so what i want to do in here is just create a simple little form
and i'll start off by just putting in my basic structure for the page i'm not going to do the
head preamble but i will do a little heading here that says hey why don't you register
and then inside here i'm going to have a struts form and so the
struts form again uses that prefix so you say s colon form the action's going to be
registered and that's going to be a register action i haven't actually created that
but i will in just a moment and i want to have a nice kind of nice table that asks
the person to put in their first name their last name their all of those other fields
and of course the first field will be the person's name the second one will be their last name the
third one will be their email address and then the last thing will be their age notice that
these properties all map to the person class and it is called person being here and i'll actually
create a field called person being in the register action class in just a moment you'll see how that
maps forth and these labels that's just the text that's going to be next to these entry entry
fields on the form so we'll say what's your first name what's your last name what's your email name
that will just ask you your email and then your age i guess something like that maybe capitalize
email maybe not the worst thing in the world to do and so that creates the basic registration form
now i wonder if it would allow me to run that i have a feeling that it actually might not so
the first thing so before i try and run that what i'm actually going to do is i'm actually
going to create the action class because watch if i try and run this it may actually bark and
say i can't find the register action so i'll give this a try run as run on server
i really just want to see what the forms would look like but oh okay it didn't do that so
there you go it's actually working even better than i thought so that's what the form looks like
oh you know what we're missing we're missing a submit button gotta have a submit button
otherwise i guess somebody could click the carriage return but
there we go submit button and that should look even better when i take a look at it running
on tomcat there you go the submit button okay but this does go to that action class called
register which means i now need to create a new java class and i'll call it the register
action and this action class is actually going to extend action support
see we can browse for that and find that yeah there's the open symphony x works 2
action support put that in there i get a little complaint against a serialization id
just add a default one if you really want to get rid of that little error
it's just a warning so it's not going to stop it from compiling but you know it does look good
i want to actually declare an instance of that person class so public or maybe make it private
and i'll call it person being here i guess i could have called it person but you see person being
this is the register action this is what's getting called from the register gs jsp there's action
register jsp and then you see person being first name that's what person being maps to it maps to
that field inside of this action class and as i said like always we will generate the appropriate
getter and setter for the person being and of course we also want an execute method
could throw an exception so you want to add that in here
in this case we're not going to be doing a lot of work so we want to return success now
normally what you would do is you would say return success in brackets but this class
because it extends action support has access to a bunch of constants like login success failure
there's five different constants that you could actually use in here input log login none success
that are common strings that you would use in a struts application
now we're not going to do anything here other than forward to the thank you jsp which we'll
create in a minute but you could do like system dot dot print lawn and say the age is
plus person being get age and of course this will print out the person's age
and the reason they'll do that is because before this execute method runs the stretch framework is
going to take this person object name person being and bind all of the fields here that are typed in
to that particular prop to that beans properties and then they become available you can just use
your getters to access them okay so now what do we do well this register action when this runs
i want this to call jsp called thank you.jsp now i haven't quite developed that jsp yet
but i've got to mention that on my action class and so i'm going to add an action annotation
right above the class and then i'm going to inform the class what happens if the result
is the keyword success i get a little error here i gotta organize my import source organize imports
it's that convention class that we want click control s everything all the compile errors go
away and i think this looks good this is the register action okay that maps up here the
action is register and when there's a success we will call on the thank you jsp and i guess
that's a file that we will create right now right next to that register jsp i'm going to create
a new file i'm going to call it thank you jsp and what am i going to do here in this
thank you jsp well i guess i'll just i'm just going to print out information about
the user now it is a jsp so once again you want all of these things that you put here
in your register jsp in here as well including the end tags for html and body so you have
the taglib directive the page directive that always makes sense we can even say
thanks for registering and then finally i don't know i guess we could put a little bit
of data here so we could say your information
submitted was and then do a little s colon property and then value equals
person being now just by saying person being that's actually going to be the full two string
and put a little break there that'll call the tostring method so that we'll call
this method here because we're not calling any property in particular and then all of
that will get printed out onto the page so we don't have to go property by property
we could if we wanted to right but here we're actually just going to print out the whole object
okay so i'll click control s there save that make sure all of my files are saved i'm going to close
that and close that up maybe do a publish on the server never a bad idea after you've
done all this maybe even do a restart only takes a second if you're using tomcat
and then with that done i am going to right click on my register jsp say run as run on server
i get my beautiful form here by the way you can always look at the source code there and you
can see yes he's created all these tables for the form but you can see you know personbean.firstname
is the name of the property you can see the action up here register.actions the method is
post in this case which is interesting but with that in there i can type in cameron mckenzie
me at mcnz.com my age is 29 click submit it goes to the server notice it says down here the age
is 29 that comes from the register action right here okay so you see it actually calling that
execute method and then we can also see here the information submitted was that you're a person
and these are all of your details now you may not like that formatting you might not want to include
that in your public facing website but that does come from the fact that we called the basic object
the person instance and that gives us the tostring method which is that implementation right there
and there you go that's how easy it is to do form handling in struts with that person class
tutorial that i told you all about over on the struts website i'm actually going
to go forward and do the form validation one next so if you're interested in learning more
stay tuned and otherwise if you're interested in my personal antics you can always follow me
on twitter at cameronmcnz i'm also the editor at the serverside.com so head over there for lots
of great tutorials on struts jsf hibernate spring you name it and subscribe on youtube