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