hi i'm cameron mckenzie at cameron mcnz on twitter and i wanted to talk to you a little bit about
struts and specifically talking about adding some custom tags to your struts applications a
previous tutorial i created a simple little hello world struts application and i wanted
to show you how i could improve it a little bit by adding in some form tags from strats
so in my previous tutorial i created this handsome little hello world struts app you can see you can
type in world there and it then says hello world back to you and if you type in your own name it'll
actually say hello camiron if you type in your own name so it takes a little bit of input then it's
just kind of a cool little hello world application it kicks off with an html file it calls on a hello
world action class it then goes to a results jsp and all of this is controlled through the use of
a struts config file stress.xml that is in that resources folder so just a nice little
a nice little struts application now one thing that's a bit peculiar is i kick it
all off with an html file nothing wrong with that nothing wrong with that at all however
you know there are these special struts form tags that are available to us and we can't use
those if we use a regular html file but they are available to you if you create a jsp so that's
what i'm going to do i'm going to create a little index.jsp file and recreate this index.html page
but do it with a jsp and use those struts form tags now a couple of things you need
if you're going to start off at jsp and use the struts form tags and it's these two lines that
are actually in the results jsp already it's the taglib directive which says we're going to use
the struts tag and then just the page directive that says this is a page that's going to create
and generate html so that's how you kick it off now i want to recreate the index.html
file here as a jsp so i'm going to copy all of that content that already existed there
push it in but i'm going to make a couple of changes so for example notice this form tag
here where the action is hello.action i'm going to replace that with a new form tag and this form
tag is from the strutstag library i'm going to close that with an end tag there and you can see
here we only have to say action equals hello we don't just say hello dot action i guess that's
not a huge time saver but it is pretty cool also we've got this regular submit button and you know
and struts provides its own custom tag for the input submit
and so there we go we don't have to say input type equals submit we just say it's a strut submit
element and then provide the value and of course there's one for text fields as well and i'm going
to throw that down here and you'll notice it's got a couple of things so it's got the name property
so that would be the same as the name property from the regular input file it's got the value
property we just want the word world in there to start off a user can overwrite that we don't need
the type equals text because this tag says it's a text field so that's not necessary but one thing
that is interesting before we had a prompt here please tell me your name i can actually get rid
of that put that in for the label property click control s and now if i run this whole thing again
well not again it's actually the first time because it's the jsp
you notice it now says please tell me your name it's got world in there it's the jsp that's being
called this time i can click the submit button i can also view the source and the source is kind
of interesting you'll notice in here they've actually filled in a bunch of fields for us
they've done the you know the the kind of route of the application to the hello world they've
put the method equal to post in before i think we defaulted it to get so that's an interesting
change that they've made again the id there in the form looks like they've actually created a table
in here for all of this data so i guess that's needed in order to do the labeling so definitely
a much nicer structure to this page and then you can actually see here you know there's the label
attribute and then well you know the tag just gets turned into regular html tag so there's nothing
crazy there but it is a little different it's got its own id field value name input type text
so kind of interesting also got a div around the submit button as well for some styling and so
that's what happens behind the scenes when you start using these custom tags now click submit
there we go hello world i'll type my name in cameron submit and it works that way as well so
you know you can see it's just kind of a cool way to implement your pages and take
advantage of the struts custom tags one other tag i like to always demonstrate
is i always like to set up a url so let's say i wanted to have a link in my page you can use the
struts url tag and i'll say hey i want a an action where somebody calls the hello action so that
action is very similar to what we set up in the form it's we're going to call this link the hello
link and if somebody clicks on it they're going to send the world to the server as a parameter so
this will kind of act the same way as if somebody clicks submit with the text world
in one of the input files so that's the way you can set up a url in this case it's called
hello link and uh you can add a parameter to it world and then that isn't the actual url you have
to actually go into your code and add that in now the cool thing here is you don't have to put this
as part of a form you can just throw it into your code anywhere you want to it doesn't have to be
inside that form tag because this link is going to automatically append world as a parameter to it
and of course i just have to reference the link's name so use a little bit of a you know a little
bit of an expression here i want that link in that anchor tag to be the hello link and notice
that hello link maps to that variable name there we'll just say do the hello world because the
parameter world is going to be passed in and now i can run this run as run on server
and notice i get a link here if you actually look in the bottom left hand corner here when i hover
over it you'll notice it says hello dot action question mark first name equals world that's how
the server knows to send the parameter world up see if i type junk in there this is still going
to say hello world because this isn't getting processed i'll click on do the hello world and
notice it now says hello world so there you go that's a couple of ways that you can incorporate
various struts form tags into your applications and there you go that's how you can add some
custom tags to your jsps specifically those struts custom tags to accelerate some of your form-based
development now if you enjoyed that tutorial head over to theserverside.com i'm the editor-in-chief
over there we've got lots of great tutorials on enterprise software development if you're
interested in my personal antics you can follow me on twitter cameronmcnz and subscribe on youtube