Versions Compared

Key

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

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 added value. Other tags create non-standard, but useful controls.

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.

...

The Code

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>

...

(tick) Any number of parameters can be added to the URI by adding more param etags.

The framework provides an array of tags, ranging from the mundane to the insane. See the Tag Developers Guide for details.

Mapping Code

Wildcard Mappings

Since the Welcome page is simple, we don't need an Action. 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

<action name="Welcome" >

...


  <result>/tutorial/Welcome.jsp</result>
</action>

As we create the application, we will often want to go directly to a page. To make prototyping easy, we can change the Welcome to a wilcard mapping.

Code Block

<action name="*" >
  <result>/tutorial(1).jsp</result>
</action>

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 "Logon" is matched, then the "/Logon.jsp" page is returned instead.

(tick) Wildcard mappings let you create your own conventions, so that you can avoid redundant configuration.

Data Entry Forms

Most applications will use several data entry forms. The Struts tags include a full set of form tags, that make creating input forms easy.

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" showPassword="true"/>
  <s:submit/>
</s:form>
</body>
</html>

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 control type, attaching a HTML label attribute, whenever it can.

Next

Onward to Coding Actions

Prev

Return to Hello World

Next

Onward to Coding Actions

Prev

Return to Hello World