hi i'm cameron mackenzie at cameron mcnz on twitter and i wanted to talk to you about getting
your message bundles and properties files to work with struts now the specifics of this tutorial is
going to show you how to get to work with struts annotations which is a little different than using
the standard struts xml file and it's going to show you how to do it with action classes
specifically i've got another tutorial that will deal with package based and global properties
this one will show you how to solve the problems that a lot of people have when they're working
specifically with message bundles properties files annotations and struts action class
as you can see here i've got a nice struts input form but you can see where it says first name
last name email that's all hard-coded here inside the register.jsp file and what i'd like to do is
instead of putting things inside of a register.jsp file i'd like to have a properties file
and you can map an action class like this a register action class and the results that
happens when that register action class runs you can map that to a properties file and here's all
you have to do so that class is called register action and it's in the com.mcnz dot struts package
i'm going to create a new package under resources with the exact same name so com.mcnz.struts
i'll click finish and in here i'm going to create a properties file with the exact same name
as the action so registeraction.java gets matched with a new file called register
action.properties and what i do inside this properties file is i simply put
key value pairs so i want the first name to say hey put in your first name i want where it says
last name to say put in your last name now this is going to be a little different notice it just
says first name right now last name right now when i use these key value pairs it'll say your
first name and your last name that just allows us to differentiate between what's coming in
by default here originally and what's going to come in after we've updated this now the way
things change a little bit is rather than using name and label we just use key we get rid of
that label there and so we just say key equals key equals key equals and you'll notice that
that key personbeam.firstname maps to the properties file personbean.firstname and we don't
need the label right because that label is coming from that properties file so i'll delete all of
this everything looks really handsome really good we've got my properties file in the exact same
package the exact same name except a different extension and everything is mapped here which
is mapped inside of the register.jsp and so now the theory is when this runs everything will work
and when we look at this page instead of saying first name which was originally hard coded it's
actually going to say your first name now here's the the rub this isn't going to work so i'm going
to restart my server and it's going to partially work and the reason it's not going to work is
normally when i run this register.jsp page i just right click on it and say run as run on server
and everything comes up here notice this has kind of changed already we're
now definitely getting the latest version but it's just saying person being dot first name
which is what's printed here inside of the the key value inside of the s text field it's actually
not pulling up what's in my properties file where it should say your first name
now watch this here's the weird thing if i actually submit this file it actually works
okay so now when i submit the file it's actually pulling that detail it's not email f what is that
nonsense there okay so the reason why it works when i click submit but not when i ask for the jsp
directly is because in order for these keys to to work you have to go through a struts action first
so when i just call up register.jsp that's just calling the jsp file directly it's not
going through any struts component however when you click submit so notice register.jsp
right there no struts but when i click submit the submission is going through the register action
right that's what the submit does goes through the register action the validation
fails it then says redisplay this page and get more input and now struts is redisplaying the
page and because it's going through the struts action it's actually pulling out the keys
so well how do you fix this right we want these to come up the first time somebody calls register.jsp
well the fix is you don't call register jsp directly what you do is you go into your
action class and you can see right now everything goes through execute well we're going to create
a slightly different url when somebody wants to call the register jsp directly they'll call
an action name display so they'll go you know stretch form handling slash display
dot action and then that will end up calling the jsp but that'll go through an action class
so watch this i'm going to create a new method here
and i'll just call it and i'll just call it display it doesn't really matter what i call it
public string display it's a new method it returns input and input is mapped to register.jsp that's
where we get input i'm also going to specify a new action for it so the action for this method is
anybody that calls display the basic act the basic action for this class is register and so execute
when they call this that's the but if someone actually calls slash display anywhere in this
struts project in this namespace it's actually going to call this method now one problem is if i
run this it's going to try and validate and the validate will trigger a null pointer exception
so in fact even if i try and get this to work right now i may have to restart my server in
order to get the latest thing to to work here i'm going to do a little system dot out dot
there we go now it just gives us some feedback but watch if i try and run this
so call the display action right there
all of a sudden i end up getting a problem oh and it looks like it's because in this case
that's not the problem i want in this case it's uh somebody can't spell the word display properly
so let me fix that up there we go display notice a null pointer exception on validate and that's
because even though i'm going through this display action this register action has a validate method
and we're always going to validate before we actually go through the actual action method
right that's that's part of stress so in order to really get this to work i have to skip the
validation right i don't want any validation i just want to go directly to that page
so you can throw in the skip validation tag there that'll tell struts don't validate just go
directly to the page associated with the input return which is register jsp so i come over here
struts form handling now i say display dot action and notice everything is coming up properly now by
the way one of the other ways you could do that potentially you could just create an index.jsp
page or something along those lines and put a url in there so i could do something
throw this i could on my register page i could add this link here and that's the register page
is still going to not work when it first comes up but then if you click on this link
it'll call that display action and then it will go through the action and then it will
render properly so what i've done here so as you can see i've created a little struts url
that we'll call the display action i'll just call the the url display page link and
then i actually create the actual href link here referencing this url i'll say hey please display
now when i run this again look if i run it directly run as run on server
i'm not going to get the key value pairs right because it's a direct reference but if i click
please display which forces it to go through the struts action
you'll notice everything works properly and so there you go those are the ins and outs of using
properties files and message bundles associated with struts actions and there you go that's how
easy it is to deal with message bundles properties files your struts action class and annotations as
you can see there's a couple of little tricks in there that it's pretty easy to get stuck
on but hopefully by seeing me go through all the steps there it irons everything's out for you so
anyways if you enjoyed this tutorial head over to i'm the editor-in-chief over there lots of great
tutorials on strat spring hibernate anything to do with enterprise software development
if you're interested in my personal antics follow me on twitter cameronmcnz and subscribe on youtube