View RSS Feed

My Java Tips

Using tags in Struts

Rate this Entry
by , 11-15-2011 at 07:26 PM (1203 Views)
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 Struts 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. 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.


One use of the Struts Tags is to create links to other web resources, especially to other resources in the local application.

A very common use cases in web applications is linking to other pages. Let's add a Welcome page with links to other actions.

Welcome.jsp
Java Code:
<%@ taglib prefix="s" uri="/struts-tags" %>
<link href="http://www.java-tips.org/blog/wp-admin/%3Cs:url%20value=" />" rel="stylesheet"
          type="text/css"/&gt;
<h3>Commands</h3>
<ul>
	<li><a href="http://www.java-tips.org/blog/wp-admin/%3Cs:url%20action="></a>"&gt;Register</li>
	<li><a href="http://www.java-tips.org/blog/wp-admin/%3Cs:url%20action="></a>"&gt;Sign On</li>
</ul>
Another common use case is using a link to change locales. Let's add links to change the user's locale and to display a message from the application resources.

HelloWorld.jsp
Java Code:
<h2><s:property value="message"></s:property></h2>
<h3>Languages</h3>
<ul>
	<li>
        <s:url id="url" action="HelloWorld">
            <s:param name="request_locale">en</s:param>
        </s:url>
        <s:a>English</s:a></li>
	<li>
        <s:url id="url" action="HelloWorld">
            <s:param name="request_locale">es</s:param>
        </s:url>
        <s:a>Espanol</s:a></li>
</ul>



Now let see how the code works.

"%{url}" will be evaluated to the url defined with the s:url tag. On the Welcome and HelloWorld pages, we use two different Struts tags to create links. We create

1. Resource links

First, in the head element, we use the url tag to inject a page reference into the HTML link tag.
Java Code:
<link href="http://www.java-tips.org/blog/wp-admin/%3Cs:url%20value=" />"
  rel="stylesheet" type="text/css"/&gt;
Note that the reference is absolute. We can move the page around without worrying about resolving relative references.

2. Direct links

In the "Commands" section, we use the url link again, to inject a reference to an Action.
Java Code:
	<li><a href="http://www.java-tips.org/blog/wp-admin/%3Cs:url%20action="></a>"&gt;Register</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 across requests.

3. 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.
Java Code:
<s:url id="url" action="Welcome">
  <s:param name="request_locale">en</s:param>
</s:url>
<s:a>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.

Submit "Using tags in Struts" to Facebook Submit "Using tags in Struts" to Digg Submit "Using tags in Struts" to del.icio.us Submit "Using tags in Struts" to StumbleUpon Submit "Using tags in Struts" to Google

Categories
Struts

Comments