View RSS Feed

My Java Tips

Using Struts2 Tags

Rate this Entry
by , 11-18-2011 at 04:59 PM (1620 Views)
Struts provides HTML tag library for easy creation of user interfaces. In this lesson I will show you what all Struts HTML Tags are available to the JSP for the development of user interfaces.

To use the Struts HTML Tags we have to include the following line in our JSP file:
Java Code:
<%@ taglib uri="/tags/struts-html" prefix="html" %>
above code makes available the tag to the jsp.

Java Code:
<html:message key="thekey"/>
Looks up the message corresponding to the given key in the message resources and displays it.
Java Code:
<html:password property="prop" size="10"/>
Tag creates the password field. The string is stored in the property named prop in the form bean.
Java Code:
<html:text property="text1" size="5"/>
Tag creates the text field. The string is retrieved from and later stored in the property named text1 in the form bean.
Java Code:
Tag creates a submit button with the provided content as the button text.
Java Code:
Tag creates a reset button with the provided content as the button text.
Java Code:
Tag prints all the available error on the page.

<html:file property="fileSelectionBox"/>

Tag creates the file upload element on the form. The property must be of the type org.apache.struts.upload.FormFile.
Java Code:
<html:checkbox property="myCheckBox"/>
Tag creates check box on the form.
Java Code:
<html:hidden property="hiddenfield"/>
Tag creates the hidden html element on the form.
Java Code:
<html:radio value="abc" property="myCheckBox"/>
Tag creates the check box on the form.
Java Code:
<html:select multiple="true" property="selectBox">
Tag creates list box on the form. The property selectBox must be an array of supported data-types, and the user may select several entries. Use to specify the entries.
Java Code:
<html:textarea property="myTextArea" value="Hello Struts" />
Tag creates the text area on the form.
Java Code:
<html:form action="/Address" method="post">
Tag is used to create the HTML Form for posting the data on the server.
Java Code:
Tag generates the base tag.
Java Code:
<BASE ...>
tells the browser to pretend that the current page is located at some URL other than where the browser found it. Any relative reference will be calculated from the URL given by instead of the actual URL. goes in the section.


Tag renders an HTML Element.

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

Finally, in the Languages section on the Hello 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="Start">
  <s:param name="request_locale">en</s:param>
<s:a href="%{url}">English</s:a>
The param tag will add the parameter “request_locale=en” to the Start Action URL, and store it under the name “url”. The tag then adds the “url” reference into the hyperlink.

Creating Wildcard Mappings

Since the Start 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 an Action class later.

<action name="Start" >

As we create the application, we will often want to go directly to a page. To make prototyping easy, we can change the Start entry to a wildcard mapping.
XML Code:
<action name="*" >

If no other mapping matches, the framework will match “Start” to the asterisk, and substitute “Start” 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.
Note: Wildcard mappings let you create your own conventions, so that you can avoid redundant configuration. The first mapping that matches a request wins.

Creating Data Entry Forms

Almost all the applications will use data entry forms, the Struts2 Tags make it easy.

Java Code:
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form action="Logon"> 
  <s:textfield label="User Name" name="username"/>
  <s:password label="Password" name="password" />

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, so you don't have to specify separate lables elements.

Submit "Using Struts2 Tags" to Facebook Submit "Using Struts2 Tags" to Digg Submit "Using Struts2 Tags" to Submit "Using Struts2 Tags" to StumbleUpon Submit "Using Struts2 Tags" to Google