hi i'm cameron mckenzie at cameron mcnz on twitter and i wanted to show you how to
incorporate application resources files in your struts application specifically struts2 2.5
applications that are using annotations and use those as your i18n properties files i'm also going
to show you how to do package properties as well in the previous tutorial that i did i showed you
how to use action class properties and some of the issues that happen when you interact with those
i think you'll find the application resources properties are a little bit easier to work with
so in my last tutorial i showed people how to use properties for actions you can see i've got a
little page here that looks like it doesn't really register properly but if you trigger it through a
struts action you can see that all of these fields here your first name which maps to the properties
up here your first name all get put onto the form that's all part of the register action properties
file which works with the register action object all fine and dandy all works really really
well but these aren't global or package level properties so i wanted to demonstrate how you can
actually include application resources or package level properties in your your program so right
now that was a special action properties file i've got this package here com.mcnz.struts in resources
i've got com mcnz struts folder if i create a brand new file in here called package.properties
i can actually get struts to use any name value pairs that i put in here
inside any of the resources that action actions inside of this package
are going to trigger so for example let me just throw a
package level greeting in there package.greeting equals this and then i'll go into my
registration.jsp file and inside that i will do something like well that's package greeting
here in the register jsp i will type in ask tax name equals and what package dot greeting
and hopefully when this runs this greeting will come out now i say hopefully now i know that
it actually won't because it won't actually run unless you put that jsp through a struts
action so if i just run the jsp directly it's not going to work so you notice it just says package
greeting just like it says personbean.firstname with these actionlevel properties files and with
these package level properties file they have to go through a struts action first this link here
forces them to go through the register action so if i click on here notice everything works
so that's pretty cool now just looking at that register action what that does is that
calls the display action in that register class and you can see that display action here skips
validation and just sends somebody to the input result which is mapped to the register jsp file
so that's how you can use package level properties but i think it's more interesting
to use application resources people are pretty comfortable with application resource files for
doing internationalization to use them all you have to do is go into your web.xml file and add an
init parameter and say use it stress custom i18n resource and make it application resources and
from that point on you can actually use that application resources file to do name value
pair i18n localization internationalization and just generally you know extracting text
from your jsp files i think it's a much better way of doing it and so you specify application
resources here it could be any name but that's the standard then you create a new file called
applicationresources.properties right in there and then you can do
i don't know put something like global greeting this is the global application resources greeting
that's what ar stands for and all of a sudden you've now got this property here that can be
pulled into your jsp files and in fact it can be pulled into your jsp files a lot easier than the
package greetings so just watch this i'll use the struts text tag say pull in the global
greeting i'll run this register.jsp on the server i may have to start the server but hopefully not
new property looks like the server server's starting for me anyways and notice right there
this is the global ar meeting now the the thing about this properties file is it's not
being forced to go through a struts action in order to work right like the package and then the
action properties i got to push them all through a struts action first that global one didn't
need that so that's uh pretty cool definitely a benefit of using that the other thing i would say
there's a big benefit well i guess you get this with the other properties as
well but this is typically where you do your internationalization so i'm going to copy that
file and i will paste it right into the same folder which will force me to rename it
and i'll call it underscore fr for all of my french canadian friends should probably do
it for india for all the people that watch from india shout out to bangalore over here
anyways application resources here now i can change this to what is french for hello bonjour
there we go and now i've got a french greeting as well as the standard english greeting and if i
wanted to invoke a page using the french language locale this would come up i could actually
force that so i can actually put a link on a page and let me see if i can figure out how to do
this i'm going to go over to the register jsp i'm going to throw this right down at the bottom here
and i'm going to create a url so i'm going to create a url just like i created a url up here
to go to the display action but i'll call it display french page link instead of display
page link and i'm going to set the request locale to french and then with that done i
can actually throw a link onto the page that uses that display french page link right there
and say please display in french and now when i run this particular file
let's go register.jsp run as run on server
okay we've got this link here display in french and this is the one that's currently
being displayed in english i'll say display it in french and notice it now says bonjour because we
have now internationalized this application with a french resource bundle so there you go if you
take a look at this tutorial and the previous tutorial they do give you a good idea of how
to internationalize and localize your application through the use of resource bundles don't forget
that key entry in this web.xml file it also show you how to use those package properties and the
previous tutorial demonstrated how to use those action properties as well for your action beans
and there you go that's how easy it is to incorporate application resources into your struts
programs and also do a little internationalization as well even a look at the package properties
files as well by the way if you enjoyed that tutorial head over to theserverside.com i'm
the editor and chief over there we've got all sorts of great tutorials on enterprise software
development strut spring hibernate jsf and all sorts of devops tutorials if you're interested
in my personal antics you can follow me on twitter at cameronmcnz and subscribe on youtube