hi i'm cameron mckenzie at cameron mznz on twitter and i want to talk to you about
apache struts interceptors how to add a couple to your struts action classes and i want to show
you how to do it with annotations i think most of the tutorials you see online do it with xml
but i think annotations are a way better way to go so if you're interested in working with
apache struts interceptors and you want to see how to apply them real easily stay tuned
so here's my standard application right here and you can see when i
click on a link like display french properties i just nothing comes out other than what i've
got in my standard output where i do a little system.out.printline but there's a bunch of
interceptors that you can add to your code here's a list of interceptors on struts.apache.org
and one of them is a timer that you can put in there let me see if we can find the timer it's
alphabetical it's highlighted in yellow that helps there's a timer interceptor that you
can wrap around all of your requests and then there's even a logger interceptor that will
print out the name of the action that has been triggered and these are interceptors
that aren't standard there's a bunch of interceptors that are standard but you can
kind of daisy chain them together and make them part of your request response cycle and so that's
exactly what i'm going to do i'm going to add this logger and i'm going to add this timer to my
to my action class my register action class so that i can take advantage of of those couple
of interceptors now how do i do it well it's relatively straightforward all you have to do
at the top of your action class you can do it on a per method level as well is add in this
interceptor refs annotation now that interceptor refs annotation has to be fed and i'm going to
paste in a a long feeding here let me see if i can get it and what it's going to do is it's going
to say hey i want to use a bunch of interceptors here oh boy look at that i told you it was going
to be long i want to use a bunch of interceptors here so the first interceptor that i want to use
is the timer interceptor and then after the timer interceptor is used i want to use the
logger interceptor and then after the logger interceptor well i just want to go back to the
regular set of interceptors that come with pretty much any struts action which is referred to as the
default stack logger and timer aren't part of the default stack that's why i'm putting them
in here and then the default stack is a couple of parameters i've set to enable logging and
make the log level trace and so now when i invoke one of my actions well i'm going to get a bunch of
information about well what action was triggered and then how long the timer says that it took for
the invocation to happen now unfortunately i got a couple of quick configurations i've got to do here
i do not have log4j set up inside of my pom file so i'm going to open up my palm file
and in the dependencies i'm going to have to add a reference to log for j and format this
so it looks handsome but i just added these two here so i've added dependencies for apache
logging log for j i guess i should probably do a maven update on that project just to make sure
that those are downloaded my server's barking at me too i'm going to stop that server for now
so that adds the dependency the other thing i need then once i've done that is in my resources folder
i just need to configure a log 4j2 xml file which will send all of the logging to my console so i
can actually see it so i don't know exactly what's going on here but there's the logging
pattern and that's going to go to standard out and standard out is the one referenced here
and there you go this kind of sets up my log4j logging okay sorry for having to do that but
if i didn't do that wouldn't be able to see the results of this file
taking advantage of those two interceptors so i'm going to do a little publish here i did
add to my maven palm file so i did download a few few files and then i'll try and run this
register.jsp file and now i'm going to clear the window down here that's all cleared and i'm going
to just do this notice the properties aren't good so i can click on this display properties properly
and you'll notice it says hey we're in the display action but you'll also see a bunch of other
information added as well so now it says execution action was performed and it was the display and
specifically called the display method in there it took 143 milliseconds so we weren't getting
that before i can get the french properties here so there's already in french you get that as well
i can trigger an exception now actually i think the exception blows everything up so
that one's not gonna oh it actually did um called the bad call method took 16 milliseconds is my
exception not causing an exception properly well i think it is but it's all being handled
properly because my exception has the proper exception mapping on it so okay there we go
there and i think even if i go do the back button and try and do a submit on this form
you'll see again it even says hey in this case we called the registers execute method and it
took 74 milliseconds as well you can see got rid of some of the validation there just to
make life a little bit easier but there you go that's how easy it is to append some new and
interesting interceptors into your code i'll make that large so you can see that a little bit better
and there we go that's how we include the timer and the logger interceptor and as i mentioned
there's a lot of other interceptors that are available so if you want to daisy chain them
in go right ahead and for that matter you can create your own interceptors if you're really
anxious to do so so there you go that's how easy it is to use interceptors with apache
struts especially if you're using annotations if you enjoyed this tutorial why don't you head over
to the serverside.com i'm the editor-in-chief over there we got lots of great tutorials on
struts enterprise software development string hibernate devops you name it if you're interested
in my personal antics you can follow me on twitter at cameronmcnz and subscribe on youtube