Home

This Chapter
-Chapter 7: JSTL
-Introducing JSTL
-General-Purpose Actions
-Conditional Actions
-Iterator Actions
-Formatting Actions
-Functions
-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

 

General-Purpose Actions

The following section discusses three general-purpose actions in the core library used for manipulating scoped variables: out, set, remove.

The out Tag

The out tag evaluates an expression and outputs the result to the current JspWriter object. The syntax for out has two forms, with and without a body content:

<c:out value="value" [escapeXml="{true|false}"]
  [default="defaultValue"]/>

<c:out value="value" [escapeXml="{true|false}"]>
  default value
</c:out>

The body content is JSP. The list of the out tag’s attributes is given in Table 7.2.

Attribute Type Description
value*+ Object The expression to be evaluated.
escapeXml+ boolean Indicates whether the characters <, >, &, ', and " in the result will be converted to the corresponding character entity codes, i.e. < to <, etc.
default+ Object The default value

Table 7.2: The out tag’s attributes

For example, the following out tag prints the value of the scoped variable x:

<c:out value="${x}"/>

By default, out encodes the special characters <, >, ', ", and & to their corresponding character entity codes <, >, ', ", and &, respectively. In JSP 2.0, where there is no built-in function in the EL to escape special characters in the value of a scoped variable, you can use out to achieve that, as an alternative to using the escapeXml function. However, if you are sure that a scoped variable does not contain any of the five characters, you can safely use an EL expression:

${x}

Warning

If a string that contains one or more special characters is not XML escaped, its value may not be rendered correctly in the browser. On top of that, unescaped special characters will make your Web site susceptible to cross-site scripting attacks, i.e. someone can post a JavaScript function/expression that will be automatically executed.

The default attribute lets you assign a default value that will be displayed if the EL expression assigned to its value attribute returns null. The default attribute may be assigned a dynamic value. If this dynamic value returns null, the out tag will display an empty string.

For example, in the following out tag, if the variable myVar is not found in the HttpSession object, the value of the application-scoped variable myVar is displayed. If the latter is also not found, an empty string is sent to the output.

<c:out value="${sessionScope.myVar}"
  default="${applicationScope.myVar"/>

The set Tag

You can use the set tag to do the following.

  1. Create a string and a scoped variable that references the string.
  2. Create a scoped variable that references an existing scoped object.
  3. Set the property of a scoped object.

If set is used to create a scoped variable, the variable can be used throughout the same JSP after the occurrence of the tag.

The set tag’s syntax has four forms. The first form is used to create a scoped variable in which the value attribute specifies the string to be created or an existing scoped object.

<c:set value="value"  var="varName"
  [scope="{page|request|session|application}"]/>

where the scope attribute specifies the scope of the scoped variable.

For instance, the following set tag creates the string “The wisest fool” and assigns it to the newly created page-scoped variable foo.

<c:set var="foo" value="The wisest fool"/>

And, the following set tag creates a scoped variable named job that references the request-scoped object position. The variable job has the page scope.

<c:set var="job" value="${requestScope.position}" scope="page"/>

Note

The last example might be a bit confusing because it created a page-scoped variable that references a request-scoped object. This should not be so if you bear in mind that the scoped object itself is not really “inside” the HttpServletRequest object. Instead, a reference (named position) exists that references the object. With the set tag in the last example, you are only creating another scoped variable (job) that references the same object.

The second form is similar to the first form, except that the string to be created or the scoped object to be referenced is passed as the body content.

<c:set var="varName" [scope="{page|request|session|application}"]>
  body content
</c:set>

The second form allows you to have JSP code in the body content.

The third form sets the value of a scoped object’s property. The target attribute specifies the scoped object and the property attribute the scoped object’s property. The value to assign to the property is specified by the value attribute.

<c:set target="target" property="propertyName" value="value"/>

For example, the following set tag assigns the string “Tokyo” to the city property of the scoped object address.

<c:set target="${address}" property="city" value="Tokyo"/>

Note that you must use an EL expression in the target attribute to reference the scoped object.

The fourth form is similar to the third form, but the value to assign is passed as the body content.

<c:set target="target" property="propertyName">
  body content
</c:set>

For example, the following set tag assigns the string “Beijing” to the city property of the scoped object address.

<c:set target="${address}" property="city">Beijing</c:set>

The list of the set tag’s attributes is given in Table 7.3.

Attribute Type Description
value+ Object The string to be created, or the scoped object to reference, or the new property value.
var String The scoped variable to be created.
scope String The scope of the newly created scoped variable.
target+ Object The scoped object whose property will be assigned a new value; this must be a JavaBeans instance or a java.util.Map object.
property+ String The name of the property to be assigned a new value.

Table 7.3: The set tag’s attributes

The remove Tag

You use the remove tag to remove a scoped variable. The syntax is as follows:

<c:remove var="varName"
  [scope="{page|request|session|application}"]/>

Note that the object referenced by the scoped variable is not removed. Therefore, if another scoped variable is also referencing the same object, you can still access the object.

The list of the remove tag’s attributes is given in Table 7.4.

Attribute Type Description
var String The name of the scoped variable to remove.
scope String The scope of the scoped variable to be removed

Table 7.4: The remove tag’s attributes

As an example, the following remove tag removes the page-scoped variable job.

<c:remove var="job" scope="page"/>

Previous
Next