hi i'm cameron mckenzie at cameron mcnz on twitter and i wanted to talk to you about strut struts
2.5 actually and i wanted to get you started with struts 2.5 by showing you how you can put together
a nice little hello world application by using struts struts 2.5 and a struts config file
so starting off i just want to show you a version of the eclipse ide that i'm using for this hello
world struts example you see it's 20 2006 which is fairly current because it's only september
2020 as i do this and as i start off this hello world struts example what i'm going to do is i'm
going to create a new maven project and this new maven projects are going to be a basic
simple maven project we're going to be creating a war file so i'm going to change that to war
as far as the group id goes i'll call it com.mcnz and then the artifact id i'll just call it struts
hello world i think that looks pretty handsome there click finish that creates the basic project
but there will be a couple of annoyances uh inside of the palm file so i'm going to open it up
and you notice in this pom file says hey you've got a war file here but there's no web.xml file
and you know what i'm going to deal with that a little bit later
i will be creating a web.xml file but i'm in this palm file in order to work with struts we
need struts so i'm going to add in a couple of dependencies i'm going to add in the struts core
i'm going to add in the struts convention plug it's going to give me access to annotations and
all sorts of great stuff although i might not use them in this example this example i'm going to use
pure struts xml and of course we're leveraging the servlet api so you got to have a reference
to the servlet api in there as well now my local machine it has jdk 1.8 installed
on it and in order to make sure everything gets compiled and built using a 1.8 jdk i just need to
add this little maven plug-in that will force that now i know you should slap me on the knuckles for
not using the latest long-term support lts release of java which is 1.11 which is 11.
um but i think a lot of people doing struts might be working on 1.8 so i'm going to leave it at that
now my palm's still a little upset because it says hey you've added a bunch of dependencies
but you haven't updated your project so i'm going to go maven update project this isn't
going to solve my problem with the not having a web.xml file but it's always a good idea to
update your project now as i said it is still barking at me for not having a web.xml file and
you know i'm not someone that likes to leave the pom file hanging so i'm going to go over
here into the web app folder create a new folder in here and this folder is going to be called web
inf now i don't know if you notice the subtlety in my voice but i pronounced that with all uppercase
letters that's the convention and while the folder is pronounced with uppercase letters the web.xml
file is pronounced with all the lowercase letters right there now i'm just going to copy and paste a
the web.xml file that i got from my github account and you can get it from my github account too
and it basically has the required preamble and a display name like you don't want to be typing that
out all on your own you don't want to be typing that out all on your own and for that matter all
of the stress configuration happens right in here we configure a filter and this filter is mapped to
the everything url pattern and what this does is this means you know the web.xml file the whole web
project is going to make sure that every single request that comes in is going to go through this
struts filter and that struts filter is the gateway to struts so that means every request
response cycle is going to get handled by struts in this application now one thing i should point
out this package has changed from version two of struts to version 2.5 which i'm using right here
if you for some reason aren't copying my palm and you're using 2.4 or something
earlier in here maybe you copied and pasted a web.xml file from the internet that references
the older package you might get an error that says hey i can't find this particular file
if that's the case make sure the package name is correct here that's probably the problem if you
do run into that okay i'm going to click control s there looks like at this point
i've got my basic project set up my palm file is no longer complaining about the lack of a web.xml
file and so i'd say it's time to go in and maybe add an html file we want to have some input here
so i'm going to create a new file i'm going to call it index dot i'll call it index.html i'll
make this just a basic html file and in this html file i just want to basically say to the user hey
um what what's your name and so it's going to say hey may i ask your name and then
this is the input page let's see if i can run this input page what happens if i try and say run
run on server can we actually just see what this page looks like
okay there we go may i ask your name and it's going to default to world the user
will click submit and then hopefully when the submit runs it'll go to the server the server
will do some processing and then it'll print out hello and in this case world because somebody
typed in world you can change that and put your own name in there and then it'll say hello cameron
or whatever i mean we've got the ability to do that but that's kind of where we're going and
this is the html page that embodies that code now there's a couple of interesting things happening
here notice that first of all it goes to the hello action and we'll call that the hello
yeah i'll leave that as the hello action that's fine
and so we've got an action that we call the action will be configured on the server
it'll be a cloud the action will be a class that's created on the server
we've got a field called first name here so we've got that first name field the first name field
will actually map to a property in the action over on the server so whatever somebody types in here
will get sent to the the server and can be processed and i guess we've got an input button
here so i guess those are the key things we've got this input field called first name uppercase n in
name there what's calling an action and i guess if we're calling an action and that action should
be able to handle that first name well i guess we should probably code an action class that can
handle a property called first name so i'm going to go over to my java class java project here and
i'm going to create a new class i'm going to put in the column.mcnc dot struts package and what is
the name going to be well we'll call it the hello world action maybe something handsome like that
and i think that's about it it's just a pojo right doesn't you don't have to inherit from
anything just have to have that class created and bob's your uncle now we do have this field called
first name and so for every field that you have on an input field you want to
have a private variable i'm going to make this a string call it first name and then
i'll add the requisite setters and getters so generate the getters and setters for first name
and so that maps all the properties from the html form and that's the form object there to
this action bean and the other thing all of these action beings are supposed to have an
execute method you can kind of play around with the name of the method but that's the convention
so you want an execute method in here and in this execute method well you can play around with the
variable i don't think i'm going to play around with it too much i'm just maybe going to print
it out to the console and then when you're done these methods you're supposed to return a string
like success or failure or you know step two of the wizard something like that so i'm just
gonna return the string success right that's uh that just will say that this was a successful
processing it's just a hello world but yeah in here you kind of do all of your processing
and for that matter you delegate to java beans pojos ejbs what other components you know session
facades that will actually do the heavy lifting for you um that's kind of the idea of that section
there and then finally when we have success this word success is supposed to be mapped to
another jsp that will display the results and so you know you usually have results jsp
and i'll create a new file i'll call it results.jsp and when there's success you end up
going to the success jsp the results jsp and here all i want to do is just i just want to go out and
say uh hey you know um hello and if somebody's typed in i want to say hello world um if they
typed in cameron i want to say hello cameron and so here's some of the stuff going on in this page
we had a a tag lib for the special struts tag library it's got the prefix s so you can see
that tag being used here this is a tag that just looks for any property inside of your associated
action clash or associated java beam we've got one called first name here and you can see here
we try and get that first name property so that's what this tag library is all about
trying to be able to extract properties from your java bean from your action class
we're going to say hello based on what that property was this is just requisite stuff for
a jsp just make sure that the entire framework knows that we're actually working with a jsp
that's going to generate html and then i guess the rest of the stuff here well that's all just
boring regular html right that's really the crazy stuff here now one thing i haven't talked about
is how this index.html binds itself to the action class you can see it says hello action here but
a web server on its own is not going to have any idea what hello.action is and we want to map that
to this hello world action and then this hello world action when it says success we want to map
that to the results jsp and all of that is done through the use of a struts configuration file
we can actually use annotations i've got another tutorial that shows you how to do a very similar
hello world application with annotations in this example i'm going to use the struts config file
and so the struts config file is just going to be called struts.xml and the convention is to throw
it in this resources folder here so i'm going to create a new file i'm going to call it struts.xml
and in here i'm going to put all of the mapping now here you got the
preamble that's required here's the meat that's interesting though um i'm going to have a basic
namespace here for my package that's just a basic default namespace that all of the good information
is going to be wrapped up in and then inside you can see that i've got this action named hello
and you can see that that's this one here right hello dot action that's how we map the entry in
the form to the action class itself so going back to the stress.xml file you can see that that hello
action is really mapped to this hello world action class com.mcnz.struts.helloworldaction
the method to call in that class to run this action is execute that's the basic convention
and if we have a result that returns the string success and we do have that in our
code return success then we'll forward someone to the results.jsp page which is this page right here
which should display hello world if they haven't typed their name in and then just hello whatever
their name is if they do and so that's how we tie all of those pieces together the input field
often known as the controller the model often known as the action class i guess that could be
the controller too probably your model is really the the java beans and your domain classes and
then finally i guess you can consider this a part of the view okay and that's all of the pieces
together so i'm going to give this a try see if i can run this delete that there and then i'll run
this again so right click run as run on server accept the default for my tomcat let everything
deploy and then we get this page that starts up that says may i ask your name i'm going to leave
it as world click submit program runs you notice that down here it says the value of first name is
world that was just printed out from the hello world action right that's what we typed in here
in some of our logic we returned success which forwards someone to the jsp page which kind of
types at the top it was the results page and then we say hello whatever was typed in and of
course we had left it typed in world so now we say hello world and there you go that's a basic round
trip with a hello world struts application and there you go that's how you put together nice
little struts hello world application using struts 2.5 and the struts config file now by the way i've
got another tutorial that deals with struts 2.5 fully with annotation configuration so if you
want to see how you can do a zero config struts 2.5 application check out that tutorial as well
now if you enjoyed this tutorial why don't you head over to theserverside.com we've
got lots of great tutorials on struts other enterprise frameworks like spring and hibernate
and of course anything to do with enterprise software development we cover over there if you're
interested in my personal antics you can follow me on twitter at cameronmcnz and subscribe on youtube