...
In
...
the
...
...
...
lesson,
...
we
...
presented
...
a
...
page
...
with
...
a
...
simple
...
Welcome
...
message.
...
In
...
the
...
Using
...
Tags
...
lesson,
...
we
...
create
...
a
...
Welcome
...
page
...
with
...
links
...
to
...
other
...
actions
...
in
...
the
...
application.
...
Web
...
applications
...
differ
...
from
...
conventional
...
websites
...
in
...
that
...
web
...
applications
...
can
...
create
...
a
...
dynamic
...
response.
...
To
...
make
...
it
...
easier
...
to
...
reference
...
dynamic
...
data
...
from
...
a
...
page,
...
the
...
framework
...
offers
...
a
...
set
...
of
...
tags.
...
Some
...
of
...
the
...
tags
...
mimic
...
standard
...
HTML
...
tag
...
while
...
providing
...
added
...
value.
...
Other
...
tags
...
create
...
non-standard,
...
but
...
useful
...
controls.
...
One
...
use
...
of
...
the
...
Struts
...
Tags
...
is
...
to
...
create
...
links
...
to
...
other
...
web
...
resources,
...
especially
...
to
...
other
...
resources
...
in
...
the
...
local
...
application.
Tip |
---|
While HTML provides a simple |
Linking
A very common use cases in web applications is linking to other pages. Now that we know Struts is up and running, let's add a Welcome page with links to other actions.
Panel |
---|
The Code
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
{tip}While HTML provides a simple {{a}} tag for creating hyperlinks, the HTML tag often requires us to include redundant information. Also the HTML tag cannot easily access dynamic data provided by the framework.{tip} h2. Linking A very common use cases in web applications is linking to other pages. Now that we know Struts is up and running, let's add a Welcome page with links to other actions. {panel}!Welcome.PNG!{panel} h3. The Code {code:html|title=Welcome.jsp|borderStyle=solid} <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Welcome</title> <link href="<s:url value="/css/tutorial.css"/>" rel="stylesheet" type="text/css"/> </head> <body> <h3>Commands</h3> <ul> <li><a href="<s:url action="Register"/>">Register</a></li> <li><a href="<s:url action="Logon"/>">Sign On</a></li> </ul> </body> </html> {code} |
Another
...
common
...
use
...
case
...
is
...
using
...
a
...
link
...
to
...
change
...
locales.
...
On
...
the
...
HelloWorld
...
page,
...
let's
...
add
...
links
...
to
...
change
...
the
...
user's
...
locale
...
and
...
to
...
display
...
a
...
message
...
from
...
the
...
application
...
resources.
Panel |
---|
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
{panel}!HelloWorld.PNG!{panel} {code:html|title=HelloWorld.jsp|borderSytle=solid} <body> <h2><s:property value="message"/></h2> <h3>Languages</h3> <ul> <li> <s:url id="url" action="HelloWorld"> <s:param name="request_locale">en</s:param> </s:url> <s:a href="%{url}">English</s:a> </li> <li> <s:url id="url" action="HelloWorld"> <s:param name="request_locale">es</s:param> </s:url> <s:a href="%{url}">Espanol</s:a> </li> </ul> </body> {code} h3. How the Code Works |
How the Code Works
"%{url}"
...
will
...
be
...
evaluated
...
to
...
the
...
url
...
defined
...
with
...
the
...
s:url
...
tag.
...
We
...
will
...
talk
...
more
...
about
...
expressions
...
when
...
we
...
get
...
to
...
the
...
OGNL
...
tutorial.
...
On
...
the
...
Welcome
...
and
...
HelloWorld
...
pages,
...
we
...
use
...
two
...
different
...
Struts
...
tags
...
to
...
create
...
links.
...
We
...
create
...
- Resource
...
- links
...
- Direct
...
- links,
...
- and
...
- Links
...
- with
...
- parameters.
...
Let's
...
look
...
at
...
each
...
in
...
turn.
...
Resource links
First,
...
in
...
the
...
head
...
element,
...
we
...
use
...
the
...
url
...
tag
...
to
...
inject
...
a
...
page
...
reference
...
into
...
the
...
HTML
...
link
...
tag.
Code Block | ||
---|---|---|
| ||
{code:format=HTML} <link href="<s:url value="/css/tutorial.css"/>" rel="stylesheet" type="text/css"/> {code} |
Note
...
that
...
the
...
reference
...
is
...
absolute.
...
We
...
can
...
move
...
the
...
page
...
around
...
without
...
worrying
...
about
...
resolving
...
relative
...
references.
...
Direct links
In the "Commands"
...
section,
...
we
...
use
...
the
...
url
...
link
...
again,
...
to
...
inject
...
a
...
reference
...
to
...
an
...
Action.
Code Block | ||
---|---|---|
| ||
{code:format=HTML} <li><a href="<s:url action="Register"/>">Register</a></li> {code} |
When
...
the
...
link
...
is
...
rendered,
...
the
...
tag
...
will
...
automatically
...
append
...
the
...
appropriate
...
extension,
...
so
...
that
...
we
...
do
...
not
...
need
...
to
...
embed
...
that
...
fact
...
all
...
over
...
the
...
application.
...
The
...
tag
...
will
...
also
...
URL-encode
...
the
...
link
...
with
...
the
...
Java
...
session
...
ID,
...
if
...
needed,
...
so
...
that
...
the
...
Java
...
session
...
can
...
be
...
retained
...
accross
...
requests.
...
Links with parameters
Finally,
...
in
...
the
...
Languages
...
section
...
on
...
the
...
HelloWorld
...
page,
...
we
...
use
...
the
...
url
...
tag
...
along
...
with
...
the
...
param
...
and
...
a
...
tags
...
to
...
create
...
a
...
link
...
with
...
request
...
parameters.
Code Block | ||
---|---|---|
| ||
{code:format=HTML} <s:url id="url" action="Welcome"> <s:param name="request_locale">en</s:param> </s:url> <s:a href="%{url}">English</s:a> {code} |
The
...
param
...
tag
...
will
...
add
...
the
...
parameter
...
"?request_locale=en"
...
to
...
the
...
Welcome
...
Action
...
URL,
...
and
...
store
...
it
...
under
...
the
...
name
...
"url".
...
The
...
a
...
tag
...
then
...
injects
...
the
...
"url"
...
reference
...
into
...
the
...
hyperlink.
Any number of parameters can be added to the URI by adding more param
etags.
Wildcard Mappings
Since the Welcome page is nothing but links, we don't need an Action class. But, we should still add a mapping, so that we can use use an action URI. If we link only to actions, and never to pages, then it's easy to add a Action class later.
Code Block |
---|
(/) Any number of parameters can be added to the URI by adding more {{param}} etags. h2. Wildcard Mappings Since the Welcome page is nothing but links, we don't need an Action class. But, we should still add a mapping, so that we can use use an action URI. If we link only to actions, and never to pages, then it's easy to add a Action class later. {code} <action name="Welcome" > <result>/tutorial/Welcome.jsp</result> </action> {code} |
As
...
we
...
create
...
the
...
application,
...
we
...
will
...
often
...
want
...
to
...
go
...
directly
...
to
...
a
...
page.
...
To
...
make
...
prototyping
...
easy,
...
we
...
can
...
change
...
the
...
Welcome
...
entry
...
to
...
a
...
wilcard
...
mapping.
The Code
Code Block | |||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
| |
| =
| ||||||||||
} <action name="*" > <result>/tutorial/{1}.jsp</result> </action> {code} h3. How the Code Works If no other mapping matches, the framework will * match "Welcome" to the asterisk, and * substitute "Welcome" for any "\{1}" tokens in the mapping. Likewise, if there is a link to a "Login" action, and nothing else matches, then the |
How the Code Works
If no other mapping matches, the framework will
- match "Welcome" to the asterisk, and
- substitute "Welcome" for any "{1}" tokens in the mapping.
Likewise, if there is a link to a "Login" action, and nothing else matches, then the "/Login.jsp"
...
page
...
is
...
returned
...
instead.
Wildcard mappings let you create your own conventions, so that you can avoid redundant configuration. The first mapping that matches a request wins.
If you are coding along, you can replace the Welcome action in your strut.xml
with the Wildcard version.
Data Entry Forms
Most applications will use several data entry forms. The Struts Tags make creating input forms easy.
Panel |
---|
The Code
Code Block | ||||||||
---|---|---|---|---|---|---|---|---|
| ||||||||
(/) Wildcard mappings let you create your own conventions, so that you can avoid redundant configuration. The first mapping that matches a request wins. If you are coding along, you can replace the Welcome action in your {{strut.xml}} with the Wildcard version. h2. Data Entry Forms Most applications will use several data entry forms. The Struts Tags make creating input forms easy. {panel}!Logon.PNG!{panel} h3. The Code {code:html|title=Logon.jsp|borderStyle=solid} <%@ taglib prefix="s" uri="/struts-tags" %> <html> <head> <title>Login</title> </head> <body> <s:form action="Logon"> <s:textfield label="User Name" name="username"/> <s:password label="Password" name="password" /> <s:submit/> </s:form> </body> </html> {code} h3. How The Code Works * The JSP engine reads the taglib reference at the top of the page and loads the Struts Tags for use with this page under the prefix "s". * The Struts Tags -- textfield, password, and submit -- each emit the appropriate label and control type. h2. What to Remember The hardest part of writing a web application can be coding the pages. The framework makes coding pages easier by providing a set of custom tags. The Struts Tags can access dynamic data provided by the framework. Tags reduce the amount of markup needed to create a page. (on) For about Struts Tags, see the [Tag Developers Guide]. || Next | Onward to [Coding Actions] || || Prev | Return to [Hello World] || |
How The Code Works
- The JSP engine reads the taglib reference at the top of the page and loads the Struts Tags for use with this page under the prefix "s".
- The Struts Tags – textfield, password, and submit – each emit the appropriate label and control type.
What to Remember
The hardest part of writing a web application can be coding the pages. The framework makes coding pages easier by providing a set of custom tags. The Struts Tags can access dynamic data provided by the framework. Tags reduce the amount of markup needed to create a page.
For about Struts Tags, see the Tag Developers Guide.
Next | Onward to Coding Actions |
---|---|
Prev | Return to Hello World |