Home

This Chapter
-Chapter 3: The HTML Tag Library
-The html Tag
-The base Tag
-The form Tag
-The text, password, hidden, textarea Tags
-The submit and cancel Tags
-The reset Tag
-The button Tag
-The checkbox Tag
-The multibox Tag
-The radio Tag
-The select Tag
-The option Tag
-The options Tag
-The optionsCollection Tag
-The link Tag
-The img Tag
-The rewrite Tag
-The frame Tag
-The image Tag
-The xhtml Tag
-Summary

Table of Contents
-Introduction
-Chapter 1: Model 2 and Struts
-Chapter 2: Input Validation with Action Forms
-Chapter 3: The HTML Tag Library
-Chapter 4: Input Validation and Data Conversion
-Chapter 5: The Validator Plugin
-Chapter 6: The Expression Language
-Chapter 7: JSTL
-Chapter 8: The Bean Tag Library
-Chapter 9: The Logic Tag Library
-Chapter 10: Struts-EL, Nested, selectLabel
-Chapter 11: Message Handling and Internationalization
-Chapter 12: The Tiles Framework
-Chapter 13: Securing Struts Applications
-Chapter 14: The Config Object
-Chapter 15: The Persistence Layer
-Chapter 16: Object Caching
-Chapter 17: File Upload and File Download
-Chapter 18: Paging and Sorting
-Chapter 19: Preventing Double Submits
-Chapter 20: Early HttpSession Invalidation
-Chapter 21: Decorating Request Objects
-Chapter 22: How Struts Works

Previous
Next

 

The text, password, hidden, textarea Tags

The text, password, hidden, and textarea tags are rendered as <input type="text">, <input type="password">, <input type="hidden">, and <textarea>, respectively. Common attributes to these tags are given in Table 3.5.

Attribute Description Allowed Value(s)
indexed Indicates whether the value assigned to the name attribute is indexed. true or false
name The scoped variable containing the property specified by the property attribute. If the name attribute is not present, the value of the enclosing form tag’s name attribute will be used. string
property* The property of the action form mapped to the enclosing form tag that will be associated with the rendered HTML input field, unless overridden by the value attribute. string
value A constant that will become the value of the rendered HTML input field. string

Table 3.5: Attributes common to the text, password, hidden, and textarea tags

Attribute Description Allowed Value(s)
redisplay Specifies whether the previous value of this password tag will be redisplayed. true or false

Table 3.6: An additional attribute of the password tag

In addition, the password tag has the redisplay attribute (given in Table 3.6) and the hidden tag has the write attribute (given in Table 3.7). If the value of the write attribute is true, the value of the hidden tag is sent to the HTTP response. For instance, <html:hidden property="language" value="en"/> will be rendered as the following input field.

<input type="hidden" name="language" value="en">en
Attribute Description Allowed Value(s)
write Indicates whether the value of this hidden tag is sent to the browser true or false

Table 3.7: Attribute specific to the hidden tag

The use of these tags is demonstrated in the app03a application. In particular, you can see the tags in the displayLoginForm.jsp page. This page is presented in Listing 3.1.

Listing 3.1: Using the text, password, hidden, and textarea tags

<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html>
<title>Login Page</title>
</head>
<body>
<html:form action="/login">
<html:hidden property="language" value="en"/>
<table>
<tr>
  <td>User Id:</td>
  <td><html:text property="userId"/></td>
</tr>
<tr>
  <td>Password:</td>
  <td><html:password property="password"/></td>
</tr>
<tr>
  <td>Comment:</td>
  <td><html:textarea cols="20" rows="4" property="comment"/></td>
</tr>
<tr>
  <td colspan="2"><html:submit> Ok </html:submit></td>
</tr>
</table>
</html:form>
</body>
</html>

The displayLoginForm.jsp page contains a form mapped to an action form (of type app03a.form.LoginForm). This action form has four properties: userId, password, language, and comment. Each property is mapped to a field in the HTML form. The language property is mapped to a hidden tag, the userId property to a text tag, the password property to a password tag, and the comment property to a textarea tag.

Now direct your browser to the following URL and you will see something similar to Figure 3.1 without all the field values.

http://localhost:8080/app03a/displayLoginForm.do

Figure 3.1: The text, password, hidden, and textarea tags in action

Formatting the Value of A Text Area

Pay special attention to the text area in Figure 3.1. A text area can take a multiline text value. Each line is terminated by a carriage return character sequence. This character sequence is also sent to the server when the form containing the text area is submitted. For the value entered into the text area in Figure 3.1, Struts will receive the following value.

line 1<cr>line 2<cr>line 3<cr>line 4

where <cr> represents a carriage return character sequence. What will you see if you try to redisplay the multiline value? It depends on how you do it. Figure 3.2 below shows the page that gets displayed when the form in Figure 3.1 is submitted.

Figure 3.2: Redisplaying a multiline value

The displayLoginInfo.jsp page in Listing 3.2 is responsible for displaying the values entered into the HTML form in Figure 3.1.

Listing 3.2: The displayLoginInfo.jsp page

<%@ taglib uri="http://java.sun.com/jstl/core" prefix="c" %>
<%@ taglib uri="/tags/struts-html" prefix="html" %>
<html>
<head>
<title>Login Info</title>
</head>
<body>
<b>Login Info:</b>
<br/>
<br/>User Id: <c:out value="${loginInfo.userId}"/>
<br/>Password: <c:out value="${loginInfo.password}"/>
<br/>Language: <c:out value="${loginInfo.language}"/>
<br/>Comment: <c:out value="${loginInfo.comment}"/>
<br/>
<br/>Comment:
<html:textarea cols="40" rows="4" name="loginInfo"
  property="comment" readonly="true"/>
</body>
</html>

As you can see, the comment property, which contains a multiline value, is displayed twice, by using the JSTL out tag and the HTML Tag Library’s textarea tag. When displayed with the out tag, the property loses the original format. This is because the carriage return sequences are not rendered by the browser. As a result, the comment is not displayed as multiline text. By contrast, the textarea tag displays it in the correct format.

Now, what if you don’t want to display a multiline value in a text area but still want to retain its format?

There are several ways.

  1. Perform string manipulation on the comment property in the corresponding action class, replacing all carriage returns with <br/>. This approach is not preferable because text formatting should be done in the presentation layer, i.e. in JSPs only.
  2. Use scriptlet on the JSP to manipulate the string. However, having scriptlet in JSPs is not considered good practice.
  3. Employ the replace function in JSTL 1.1 (See Chapter 7). This is the most favorable approach, unfortunately JSTL 1.1 requires JSP 2.0.
  4. Write your own custom tag that functions similarly to the replace function. This seems to be a good solution if you are using JSP 1.2. Be warned, though, since writing custom tags requires relatively more efforts than simply using scriptlet, many will succumb to scriptlet. However, writing one is a good investment since you can reuse the tag in other applications.

Previous
Next