Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

In the Hello World 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 , but provided 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 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 framnework.

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.

...

Code Block
html
html
titleWelcome.jsp
borderStylesolid
<%@ 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>

<h3>Languages</h3>
<ul>
    <li>
        <s:url id="url" action="Welcome">
            <s:param name="request_locale">en</s:param>
        </s:url>
        <s:a href="%{url}">English</s:a>
    </li>
    <li>
        <s:url id="url" action="Welcome">
            <s:param name="request_locale">ja</s:param>
        </s:url>
        <s:a href="%{url}">Japanese</s:a>
    </li>
</ul>

</body>
</html>

How the Code Works

On the Welcome page, 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.

First, in the head element, we use the url tag to inject a page reference into the HTML link tag.

...

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
xml
xml
titlestruts.xml
borderStylesolid
<action name="*" >
  <result>/tutorial/(1).jsp</result>
</action>

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 "Logon" action, and nothing else matches, then the "/Logon.jsp" page is returned instead.

(tick) 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 Tags make creating input forms easy.

Panel

The Code

Code Block
html
html
titleLogon.jsp
borderStylesolid
<%@ taglib prefix="s" uri="/struts-tags" %>
<html>
<head>
  <title>Logon</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>

...

  • 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.

Summary

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 fraemwork, and reduce the amount of markup needed to create a page.

(lightbulb) For about Struts Tags, see the Tag Developers Guide.

Next

Onward to Coding Actions

Prev

Return to Hello World