You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 7 Next »

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

Welcome.jsp
<%@ 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>

On the Welcome page, we use two different Struts tags to create links.

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

<link href="<s:url value="/css/tutorial.css"/>" 
  rel="stylesheet" type="text/css"/>

Note that the reference is absolute. We can move the page around without worrying about resolving relative references.

In the "Commands" section, we use the url link again, to inject a reference to an Action.

<li><a href="<s:url action="Register"/>">Register</a></li>

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, we use the url tag along with the param and a tags to create a link with request parameters.

<s:url id="url" action="Welcome">
  <s:param name="request_locale">en</s:param>
</s:url>
<s:a href="%{url}">English</s:a>

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.

(tick) 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.

<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 entry to a wilcard mapping.

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

Data Entry Forms

Most applications will use several data entry forms. The Struts tags make creating input forms easy.

The Code

Logon.jsp
<%@ 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>

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.

Next

Onward to Coding Actions

Prev

Return to Hello World

  • No labels