hi i'm cameron mckenzie at cameron mcnz on twitter and i wanted to show you how
to do a little struts 2 example struts 2.5 in fact and do it inside of eclipse
so as you can see this struts 2.5 and eclipse example is working with the 20 2006
version of eclipse which i think is pretty up to date first thing i'm going to do as i kick off
this project is i'm going to create a basic maven project so i'm going to go in here look for maven
create a basic maven project it's just going to be a simple project i'm going to fill in all of the
particulars of the maven file by hand the group id will be com.mcnz the artifact id just struts
example and of course we're going to be using a war file right here i'm going to click finish it's
going to bark back at me just a little bit because it's going to say hey you know if you've got a
war file you should have a web.xml file as well you can kind of see that in the error there not
to worry we're going to add that web.xml file in shortly but first i want to just deal with some of
the dependencies that we have in this struts 2.5 application in a clip you'll notice that we have
to add the dependency for the struts core we want to add the dependency for the struts convention
which will give us access to annotations and all sorts of other great things and of course we're
going to leverage the server and jsp api it's a it's a java web application why wouldn't you so
those three dependencies need to be added and also i've got jdk 1.8 on this machine here and so just
to enforce jdk 1.8 compliance i'm going to add this build plugin here which says you know we're
going to compile the source and generate target code based on jdk 1.8 now feel free to slap my
wrist for not using jdk one point or jdk 11 which is currently the latest long-term support release
but this should be good for this example oh i don't like the way that that formatted that there
but you know i'm not going to get too worried about it what i am worried about is this little
error that says there's no xml file and that's easily remedied all i have to do is go into this
main web app folder create a new folder called web inf pronounced with all uppercase letters and in
there generate a new file just called web.xml and that'll make one of the errors go away at the very
least you know that we don't have that hey no xml web.xml found but that's not exactly a well-formed
web.xml file in itself so i need to update that and i'm just going to paste in
a web.xml file here you can see it's got the regular overly verbose preamble right there
it's got the name you know struts and eclipse without the config file but this is is interesting
so all of the stuff above is just standard but you'll notice that i've got a filter mapping here
for struts and specifically the stretch prepare and execute filter this is going to capture every
request that comes into this web module and force it to go through the standard struts
flow of input actions output success all of those other interesting things that struts does
so this is really what's needed to make struts a part of your web application now one thing to
mention if you're using prior to struts 2.5 i believe the package name is a little different
and so if and if you are using struts.5 and you don't have that exact package name you might get a
can't find this file or something like that so just be careful make sure you're using
struts 2.5 if you do get that error message it's probably just the package that you're referencing
might not be correct me copy and paste it from a different tutorial that's a little older than
this one shame on you okay so that is now the web.xml file configured and really the
heart of struts configured here as well now i want to write a little application that someone's
going to type their name into and then we're going to say hey welcome cameron or welcome joe
that's going to require well i guess that's going to require
a jsp file or an html file one and i create a new well let me just create a basic html file
keep things simple i'll call it index.html this will create a basic
one for me and as i said i'm going to create this really really simple
i'm going to replace that with just really just an html page that has nothing more than
this form in it and this form just says may i ask your name it's got a input text file where
someone will type their name a submit button where they click submit and when they click submit it's
going to call on the strut action associated with the term hello you'll also notice that
the input here is put into a text field called first name and so i want that first name to get
set on a property of an action being over on the server that's another important property
that i need to think about as well but this will be the input page and this will call on our strut
action class now that does mean that i have to create a new struts action class i'll put in the
package.com.mcnz.struts and i'll call it the hello action because this is
kind of a hello world type of act like application that i'm creating right here
right off the bat there we go we've got the hello action i did
mention that there's going to be a property named first name that's going to
be a field that's being submitted to the server so you want to create
a variable that maps to that property and it also makes sense to
create the associated setters and getters for that property as well
now the thing about a struts action class is it should have a method called execute
and in this execute method you're supposed to perform a bunch of logic
and if the logic is successful you're supposed to return a text string that says success and
that success is that success string is then mapped to some uh file to
that will some some jsp file or resource that will render a result for the client
um also inside this method you can gain access to any of these properties so i think i might even
say i'm going to do a little system.out.print here that says hey you know the value of first name
that was passed in is equal to first name now this is going to represent the processing i mean you
might save something to a database you might send an email you might persist something perform logic
we're going to keep it simple here we're just going to return success but what happens when
we return success from this execute method well normally things like action mappings and where
we go if there's a success or a failure that would get mapped in a struts.xml file but we don't have
to do that anymore when we're using annotations instead you can just put this tag above the hello
action class that says you know when this runs if we have a successful result so that is the
return of this method is the word success then we'll send somebody to the results jsp then you
could have you know something says if it's equal to failure send them to the error page so that's
how this result tag works also this hello action notice it gets mapped to this hello.action here
how does the framework know that when someone asks for the hello action that it should be this
hello action that gets called it's not as simple as the fact that it's called hello action you're
supposed to actually have an action mapping again this is something that would normally go
inside of a struts config file with annotations you can just put it right onto your action class
now am getting barked out a little bit here can't resolve those things so i've got to
organize my imports when i do that you see that it imports the action and result
and then if i do a control s and save all of the errors go away so this is really what your
struts action class looks like it's a fairly simple action class i'll organize some of the
code structure there but you can see it's got a property that maps to all of the properties in
your form that's being submitted and then an execute method to implement some logic
and all of the requisite action and results mapping now i do say that if this runs
successfully which it always will i can't see this throwing an exception we should call on
the result.jsp page and i'd like to say you know hey welcome whatever the person's first name is
on that page so over here in the web app i create a new well i want to create a new we'll call it
result.jsp so this creates the basic jsp file now i call this results notice that in the
i went to look for the strats config file there notice here that the uh annotation is result
singular the file i'm going to is result plural with an s on it i just want a little distinction
there to show you that you know it's not this name that's being used it's this name here so i've got
the results.jsp file and i don't really want to do anything too crazy in it right at the beginning
i need to add a couple of these tags here this is just your page directive that says this is really
a jsp page it's going to generate html there's also this taglib directive that i need and this
is part of the struts tag lib but it's going to be used to help print out this property to the user
so i want to gain access to this property that's inside this action class that action class is
passed into this jsp so i can access some of its data now it should be a well-formed html page
and so that means starting off with your html and body tags and then finishing with your end
body and end html tag i'm also just going to add a little text here in the body that says hey this
is the results page that's just so that when the page comes up we know where we are and then i'm
going to add this remember we've asked somebody to type in their name i'm going to say hey hello
whatever your first name is that's the first name that was
entered into this text field here it's the first name that was assigned to this property here and
it's getting displayed using this struts tag associated with the prefix s that will display
a property that it find in the current scope and specifically the property of first name
now i am getting this little warning right here it says hey you know
we're having a problem with this s property thing i think it's just because i've got these
reverse you want to have your taglib first up there click control s and that goes away
now for the most part i think i've done this application i can right click on this and say
run as run on server i do have a tomcat server already configured here so you can see tomcat9
i'll click finish restart the server i guess it was already started up it'll only take a
second this should bring up my html page that maps to the index.html right here you can see
may i ask your name input it's the first name and then it's got the submit button
and then i'm going to say yeah my name is cameron i'll click submit
it'll call on the action you can see that the action says hey the value of first name is cameron
that was printed out from the action right here and then finally when it gets to the results page
which is this jsp right here hey it's going to print out our first name from that's held inside
that action it then says this is the results page and hello cameron and there you go that's how you
create a very basic struts 2.5 web application without a struts config file in eclipse
and there you go that's how easy it is to do a nice little round trip with struts 2.5 inside of
the eclipse ide now if you enjoyed this tutorial why don't you head over to theserverside.com
we've got lots of great tutorials on all sorts of enterprise java frameworks devops anything
that has to do with enterprise software development in general if you're interested
in my personal antics you can follow me on twitter cameronmcnz and subscribe on youtube