8.5 Logic Tags

The Logic tag library contains tags that are useful for managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management. Table 8-5 lists the tags available within the Logic tag library.

Table 8-5. Custom tags within the Logic tag library

Tag name

Description

empty

Evaluate the nested body content of this tag if the requested variable is either null or an empty string.

equal

Evaluate the nested body content of this tag if the requested variable is equal to the specified value.

forward

Forward control to the page specified by the ActionForward entry.

greaterEqual

Evaluate the nested body content of this tag if the requested variable is greater than or equal to the specified value.

greaterThan

Evaluate the nested body content of this tag if the requested variable is greater than the specified value.

iterate

Repeat the nested body content of this tag over a specified collection.

lessEqual

Evaluate the nested body content of this tag if the requested variable is less than or equal to the specified value.

lessThan

Evaluate the nested body content of this tag if the requested variable is less than the specified value.

match

Evaluate the nested body content of this tag if the specified value is an appropriate substring of the requested variable.

messagesNotPresent

Generate the nested body content of this tag if the specified message is not present in this request.

messagesPresent

Generate the nested body content of this tag if the specified message is present in this request.

notEmpty

Evaluate the nested body content of this tag if the requested variable is neither null nor an empty string.

notEqual

Evaluate the nested body content of this tag if the requested variable is not equal to the specified value.

notMatch

Evaluate the nested body content of this tag if the specified value is not an appropriate substring of the requested variable.

notPresent

Generate the nested body content of this tag if the specified value is not present in this request.

present

Generate the nested body content of this tag if the specified value is present in this request.

redirect

Render an HTTP redirect.

The tags within the Logic tag library can be divided into four separate categories based on how they are used:

·         Value comparison

·         Substring matching

·         Redirecting and forwarding

·         Collection utilities

This division into categories is done for the purpose of explanation. The tags are not packaged or arranged into these categories; they all belong to the Logic package.

8.5.1 Value Comparison

The value-comparison tags print out the body of the tag if and only if the comparison evaluates to true. There are several different types of comparison tags that you can use, depending on your specific needs.

Each of the value-comparison tags takes a value and compares it to the value of a comparison attribute. If the value given can be successfully converted to a number, a number comparison is performed; otherwise, a string comparison is performed.

The comparison tags share the common attributes listed in Table 8-6.

Table 8-6. Common attributes of the comparison tags

Name

Description

name

The name of a bean to use to compare against the value attribute. If the property attribute is used, the value is compared against the property of the bean, instead of the bean itself.

parameter

The name of a request parameter to compare the value attribute against.

property

The variable to be compared is the property (of the bean specified by the name attribute) specified by this attribute. The property reference can be simple, nested, and/or indexed.

scope

The scope within which to search for the bean named by the name attribute. All scopes will be searched if not specified.

value

The constant value to which the variable, specified by another attribute(s) of this tag, will be compared.

A few examples will help solidify how these comparison tags can be used. To check whether a particular request parameter is present, you can use the Logic present tag:

<logic:present parameter="id">
  <!-- Print out the request parameter id value -->
</logic:present>

To check whether a collection is empty before iterating over it, you can use the notEmpty tag:

<logic:notEmpty name="userSummary" property="addresses">
  <!-- Iterate and print out the user's addresses -->
</logic:notEmpty>

Finally, here's how to compare a number value against a property within an ActionForm:

<logic:lessThan property="age" value="21">  
  <!-- Display a message about the user's age -->
</logic:lessThan>

8.5.2 Substring Matching

The substring-matching tags take all the same arguments as the value-comparison tags. You compare the string specified by the value attribute to any of the comparison values you give it, specified by cookie, header, parameter, property, or name. Matching tags also have an additional location attribute that informs the tag where to start matching from (either "start" or "end").

In this example, the matchTag is being used to determine whether the request parameter action begins with the string "processLogin":

<logic:matchTag parameter="action" value="processLogin" location="start">
  Processing Login....
</logic:matchTag>

If the location attribute is not specified, a match between the variable and the value may occur at any position within the variable string.

8.5.3 Redirecting and Forwarding

The forward and redirect tags within the Logic tag library might have been better suited to the HTML tag library. However, the fact that they are in the Logic tag library doesn't make them any less valuable. In fact, combined with one of the other Logic tags, these two tags become extremely useful.

The redirect tag is responsible for sending a redirect to the client's browser, complete with URL-rewriting if the container supports it. Its attributes are consistent with the Struts HTML link tag. The base URL is calculated based on which of the following attributes you specify (you must specify exactly one of them):

forward

Use the value of this attribute as the name of a global ActionForward to be looked up, and use the context-relative URI found there.

href

Use the value of this attribute unchanged.

page

Use the value of this attribute as a context-relative URI, and generate a server-relative URI by including the context path.

The forward tag is responsible for either redirecting or forwarding to a specified global ActionForward. The forward tag has one attribute, name, which is the logical name of the ActionForward from the configuration file.

8.5.4 Collection Utilities

One of the most useful and most widely used tags within the Struts tag libraries is the iterate tag. The iterate tag is responsible for executing its body content once for every element inside the specified collection. It has one required attribute:

id

The name of a page-scope JSP bean that will contain the current element during an iteration.

An example is the best way to understand how to use the iterate tag:

<logic:iterate id="address" name="userSummary" property="addresses">
  <!-- Print out the address obejct in a table -->
</logic:iterate>

Here, the iterate tag will get the collection of addresses by calling the getAddresses( ) method on the userSummary bean. During each iteration, an individual address will be assigned to the address variable. This variable can be used inside the body of the iterate tag as if you had assigned it directly. During the next iteration, the next address object will be assigned to the address variable. This continues until the entire collection of addresses has been traversed.

The iterate tag is very flexible in terms of where it gets the collection to iterate over. The attributes that control how the iterate tag performs this behavior are listed in Table 8-7.

Table 8-7. Attributes of the iterate tag

Name

Description

collection

A runtime expression that evaluates to a collection (conforming to the requirements listed above) to be iterated over.

id

The name of a page-scope JSP bean that will contain the current element of the collection on each iteration, if it is not null.

indexed

The name of a page-scope JSP bean that will contain the current index of the collection on each iteration.

length

The maximum number of entries (from the underlying collection) to be iterated through on this page. This can be either an integer that directly expresses the desired value, or the name of a JSP bean (in any scope) of type java.lang.Integer that defines the desired value. If not present, there is no limit on the number of iterations performed.

name

The name of the JSP bean containing the collection to be iterated over (if property is not specified), or the JSP bean whose property getter returns the collection to be iterated over (if property is specified).

offset

The zero-relative index of the starting point at which entries from the underlying collection will be iterated through. This can be either an integer that directly expresses the desired value, or the name of a JSP bean (in any scope) of type java.lang.Integer that defines the desired value. If not present, zero is assumed (meaning that the collection will be iterated from the beginning).

property

The name of the property of the JSP bean specified by name whose getter returns the collection to be iterated.

scope

The bean scope within which to search for the bean named by the name property, or "any scope" if not specified.

type

The fully qualified class name of the element to be exposed through the JSP bean named from the id attribute. If not present, no type conversions will be performed. The elements of the collection must be assignment-compatible with this class, or a request-time ClassCastException will occur.

8.5.5 Messages and Errors

The messagesPresent and messagesNotPresent tags evaluate the body content, depending on whether an ActionMessages or ActionErrors object is present in the request scope. Table 8-8 lists the attributes of these two tags.

Table 8-8. Attributes of the messagesPresent and messagesNotPresent tags

Name

Description

name

The parameter key to retrieve the message from request scope.

property

The name of the property for which messages should be retrieved. If not specified, all messages (regardless of property) are retrieved.

message

By default the tag will retrieve the request-scope bean it will iterate over from the Action.ERROR_KEY constant string, but if this attribute is set to true, the request-scope bean will be retrieved from the Action.MESSAGE_KEY constant string. Also, if this is set to true, any value assigned to the name attribute will be ignored.