Chapter 16: The Nested Tag Library


The Logic Tag Library Tags

The following table lists each of the tags in the Logic Tag Library and a short description of each tag's purpose.

Tag

Description

empty

Wraps content that is conditionally processed based on whether the specified object is null or contains an empty (zero-length) String.

equal

Wraps content that is conditionally processed based on whether a specified object's value equals a specified constant value.

forward

Looks up a forward from the Struts configuration file and processes it.

greaterEqual

Wraps content that is conditionally processed based on whether a specified object's value is greater than or equal to a specified constant value.

greaterThan

Wraps content that is conditionally processed based on whether a specified object's value is greater than a specified constant value.

iterate

Wraps content that is repeated for each element of a specified collection.

lessEqual

Wraps content that is conditionally processed based on whether a specified object's value is less than or equal to a specified constant value.

lessThan

Wraps content that is conditionally processed based on whether a specified object's value is less than a specified constant value.

match

Wraps content that is conditionally processed based on whether a specified object's value contains, starts with, or ends with a specified constant value.

messagesNotPresent

Wraps content that is conditionally processed based on whether Struts' org.apache.struts.action.ActionErrors or org.apache.struts.action.ActionMessages object does not have any errors or messages in it, respectively.

messagesPresent

Wraps content that is conditionally processed based on whether Struts' org.apache.struts.action.ActionErrors or org.apache.struts.action.ActionMessages object has any errors or messages in it, respectively.

notEmpty

Wraps content that is conditionally processed based on whether the specified object is non-null and does not contain an empty (zero-length) String.

notEqual

Wraps content that is conditionally processed based on whether a specified object's value is not equal to a specified constant value.

notMatch

Wraps content that is conditionally processed based on whether a specified object's value does not contain, start with, or end with a specified constant value.

notPresent

Wraps content that is conditionally processed based on whether a specified object does not exist.

present

Wraps content that is conditionally processed based on whether a specified object exists.

redirect

Composes a URL to another page and then redirects to it.

The remainder of this chapter discusses each tag in detail, including a complete description of the tag, a table listing each of the tag's attributes, and a usage example for the tag. In the tables that describe each tag's attributes, pay special attention to the Required and Accepts JSP Expression columns. The Required column simply denotes whether the given attribute is required when using the tag. In addition to the required column denoting whether an attribute is required, the rows for required attributes are highlighted in gray so that you can determine at a glance which attributes are required.

If an attribute is required and you do not specify it when using the tag, the tag will throw a javax.servlet.jsp.JspException at run time. Note that you can declare an error page in your JSP with a page directive to capture any JspExceptions that might be thrown, as shown here:

<%@ page errorPage="error.jsp" %>

If an exception occurs, the page specified by the errorPage attribute will be internally redirected to display an error page.

The Accepts JSP Expression column denotes whether the given attribute's value can be specified with a JSP expression. If a JSP expression is used to specify an attribute value, the expression must comprise the complete value, quote (") to quote ("), as shown here.

Correct:

<logic:present name="<%=result%>">

Incorrect:

<logic:present name="<%=result%>-result">

Notice in the incorrect example that "-result" is used as part of the value for the name attribute following the expression. This is invalid because there are extra characters between the end of the expression and the ending quote.

A corrected version of the incorrect example follows:

<logic:present name="<%=result + "-result"%>"/>

The concatenation of "-result" is now part of the expression and the expression comprises the complete value for the attribute.

The empty Tag

The empty tag is used to wrap content that is conditionally processed based on whether the specified object is null or contains an empty (zero-length) String. Additionally, if the object specified implements java.util.Collection or java.util.Map, this tag will call the object's isEmpty( ) method to determine whether the object is empty. If the object is empty, then the wrapped content will be processed.

Attributes

Attribute

Description

Accepts JSP Expression

Required

name

Specifies the name of an object to check as being empty. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object to check as being empty.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object to check as being empty.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

Example Usage

There are two ways you can use the empty tag. The first way, shown here, uses the name attribute to specify the name of an object to check as being empty:

<logic:empty name="results"> Your search yielded no results. </logic:empty>

If the results object is null, contains a zero-length string, or contains a Collection or Map with zero elements, then the content between the starting and ending empty tags will be processed. Remember that you can explicitly specify the scope of the object with the scope attribute.

The second way to use the empty tag is shown here:

<logic:empty name="bizObj" property="results"> Your search yielded no results. </logic:empty>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object to check as being empty. Again, you can explicitly specify the scope of the object with the scope attribute.

The equal Tag

The equal tag is used to wrap content that is conditionally processed based on whether a specified object's value equals a specified constant value. This tag works for both numeric and string comparisons.

There are five ways you can use the equal tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value will be used for the comparison.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose value will be used for the comparison.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose value will be used for the comparison.

  • You can use the name attribute to specify the name of an object whose value will be used for the comparison. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used for the comparison. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose value is used for comparison.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose value is used for comparison.

Yes

No

name

Specifies the name of an object whose value is used for comparison. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object whose value is used for comparison.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose value is used for comparison.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object whose value is used for comparison.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

value

Specifies the constant value to which the value, specified by other attributes of this tag, will be compared.

Yes

Yes

Example Usage

As mentioned, there are five ways you can use the equal tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value is compared against the value specified with the value attribute:

<logic:equal cookie="role" value="Manager"> User is a Manager. </logic:equal>

If the cookie's value and the value attribute's value are equal, the content between the starting and ending equal tags will be processed. Otherwise, it will be ignored.

The second way to use the equal tag is shown here:

<logic:equal header="User-Agent"               value="Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)"> Browser is Internet Explorer. </logic:equal> 

This example compares the incoming request's User-Agent header against the value attribute to determine whether they are equal. If so, the content between the starting and ending equal tags will be processed.

The following is the third way to use the equal tag:

<logic:equal parameter="catId" value="10"> Category Id is 10. </logic:equal>

This example compares a request parameter from the incoming request against the value of the value attribute. If the two values are equal, the content between the starting and ending equal tags will be processed.

The fourth way to use the equal tag is shown here:

<logic:equal name="resultCount" value="0"> Search returned no results. </logic:equal>

This example compares the value of the resultCount object against the value of the value attribute. If the two values are equal, the content between the starting and ending equal tags will be processed. Remember that you can explicitly specify the scope of the resultCount object with the scope attribute.

The following is the fifth and final way to use the equal tag:

<logic:equal name="employee" property="name" value="Bob"> Hello, Bob! </logic:equal>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be compared against that of the value attribute. Again, you can explicitly specify the scope of the object with the scope attribute.

The forward Tag

The forward tag is used to look up a forward from the Struts configuration file and process it. Based on the way that the forward is defined in the configuration file, this tag will either forward to the URL specified by the forward or perform a redirect to it. If the forward has its redirect attribute set to true, as shown in the following example, this tag will perform a redirect:

<forward name="google" path="http://www.google.com/" redirect="true"/>

A redirect sends a response to the user's browser instructing it to load another page. A forward, on the other hand, simply "forwards" control (on the server side) to another URL without having the browser make another request. Note that only URLs residing in the same application can be forwarded to, whereas any URL can be redirected to.

Attribute

Attribute

Description

Accepts JSP Expression

Required

name

Specifies the name of the forward to process.

Yes

Yes

Example Usage

The following snippet shows how to use the forward tag:

<logic:forward name="search"/>

As you can see, this tag is very straightforward. All you have to do is specify the name of the forward to process.

The greaterEqual Tag

The greaterEqual tag is used to wrap content that is conditionally processed based on whether a specified object's value is greater than or equal to a specified constant value. Note that this tag works for both numeric and string comparisons.

There are five ways you can use the greaterEqual tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value will be used for the comparison.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose value will be used for the comparison.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose value will be used for the comparison.

  • You can use the name attribute to specify the name of an object whose value will be used for the comparison. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used for the comparison. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose value is used for comparison.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose value is used for comparison.

Yes

No

name

Specifies the name of an object whose value is used for comparison. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object whose value is used for comparison.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose value is used for comparison.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object whose value is used for comparison.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

value

Specifies the constant value to which the value, specified by other attributes of this tag, will be compared.

Yes

Yes

Example Usage

As mentioned, there are five ways you can use the greaterEqual tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value is compared against the value specified with the value attribute:

<logic:greaterEqual cookie="size" value="0"> Size is valid. </logic:greaterEqual>

If the cookie's value is greater than or equal to the value attribute's value, the content between the starting and ending greaterEqual tags will be processed. Otherwise, it will be ignored.

The following is the second way to use the greaterEqual tag:

<logic:greaterEqual header="Content-Length" value="100000"> Page has large amount of content. </logic:greaterEqual>

This example compares the incoming request's Content-Length header against the value attribute to determine whether the content length is greater than or equal to 100,000. If so, the content between the starting and ending greaterEqual tags will be processed.

The third way to use the greaterEqual tag is shown here:

<logic:greaterEqual parameter="catId" value="10"> Category Id is greater than or equal to 10. </logic:greaterEqual>

This example compares a request parameter from the incoming request against the value of the value attribute. If the parameter is greater than or equal to the value, the content between the starting and ending greaterEqual tags will be processed.

The fourth way to use the greaterEqual tag is shown here:

<logic:greaterEqual name="resultCount" value="1"> Search returned at least 1 result. </logic:greaterEqual>

This example compares the value of the resultCount object against the value of the value attribute. If the resultCount object's value is greater than or equal to the value of the value attribute, the content between the starting and ending greaterEqual tags will be processed. Remember that you can explicitly specify the scope of the resultCount object with the scope attribute.

The following is the fifth and final way to use the greaterEqual tag:

<logic:greaterEqual name="employee" property="age" value="21"> Employee is old enough to drink. </logic:greaterEqual>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be compared against that of the value attribute. Again, you can explicitly specify the scope of the object with the scope attribute.

The greaterThan Tag

The greaterThan tag is used to wrap content that is conditionally processed based on whether a specified object's value is greater than a specified constant value. This tag works for both numeric and string comparisons.

There are five ways you can use the greaterThan tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value will be used for the comparison.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose value will be used for the comparison.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose value will be used for the comparison.

  • You can use the name attribute to specify the name of an object whose value will be used for the comparison. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used for the comparison. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose value is used for comparison.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose value is used for comparison.

Yes

No

name

Specifies the name of an object whose value is used for comparison. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object whose value is used for comparison.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose value is used for comparison.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object whose value is used for comparison.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

value

Specifies the constant value to which the value, specified by other attributes of this tag, will be compared.

Yes

Yes

Example Usage

As mentioned, there are five ways you can use the greaterThan tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value is compared against the value specified with the value attribute:

<logic:greaterThan cookie="size" value="0"> Size is valid. </logic:greaterThan>

If the cookie's value is greater than the value attribute's value, the content between the starting and ending greaterThan tags will be processed. Otherwise, it will be ignored.

The second way to use the greaterThan tag is shown here:

<logic:greaterThan header="Content-Length" value="100000"> Page has large amount of content. </logic:greaterThan>

This example compares the incoming request's Content-Length header against the value attribute to determine whether the content length is greater than 100,000. If so, the content between the starting and ending greaterThan tags will be processed.

The following is the third way to use the greaterThan tag:

<logic:greaterThan parameter="catId" value="10"> Category Id is greater than 10. </logic:greaterThan>

This example compares a request parameter from the incoming request against the value of the value attribute. If the parameter is greater than the value, the content between the starting and ending greaterThan tags will be processed.

The fourth way to use the greaterThan tag is shown here:

<logic:greaterThan name="resultCount" value="1"> Search returned more than 1 result. </logic:greaterThan>

This example compares the value of the resultCount object against the value of the value attribute. If the resultCount object's value is greater than the value of the value attribute, the content between the starting and ending greaterThan tags will be processed. Remember that you can explicitly specify the scope of the resultCount object with the scope attribute.

The following is the fifth and final way to use the greaterThan tag:

<logic:greaterThan name="employee" property="age" value="20"> Employee is old enough to drink. </logic:greaterThan>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be compared against that of the value attribute. Again, you can explicitly specify the scope of the object with the scope attribute.

The iterate Tag

The iterate tag is used to wrap content that is repeated for each element of a specified collection. The collection can be any of the following:

  • An array of Java objects or primitives (e.g., int, long, and so on)

  • An implementation of java.util.Collection

  • An implementation of java.util.Enumeration

  • An implementation of java.util.Iterator

  • An implementation of java.util.Map

Additionally, this tag sets two JSP variables on each iteration through the specified collection's elements. The first variable, whose name is specified by the id attribute, stores a reference to the current element of the collection. The second variable, whose name is specified by the indexId attribute, stores the numeric index for the current element. Note that each element will be stored as type Object unless specified otherwise with use of the type attribute. Also note that java.util.Map-based collections house java.util.Map.Entry elements, which contain the key and value for the given element.

There are three ways you can specify which collection to iterate over, as listed here and shown later in the section "Example Usage":

  • You can use the collection attribute to specify an expression that evaluates to a collection object.

  • You can use the name attribute to specify the name of a collection object that can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return a collection object. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Note that if none of the aforementioned ways is used to specify the collection that should be iterated over, a JspException will be thrown by this tag at run time.

Also note that if the collection you are iterating over can contain null values, the loop will still be performed but no page scope attribute will be stored for elements that are null.

Attributes

Attribute

Description

Accepts JSP Expression

Required

collection

Specifies an expression that evaluates to an array, java.util.Collection, java.util.Enumeration, java.util.Iterator, or java.util.Map.

Yes

No

id

Specifies the name for the page-scoped JSP variable that will hold a reference to the current element of the collection on each iteration.

No

Yes

indexed

Specifies the name for the page-scoped JSP variable that will hold the current index of the collection on each iteration.

No

No

length

Specifies the maximum number of entries from the collection that will be iterated over. If not specified, all entries in the collection will be iterated over. This attribute can specify an integer value or the name of an Integer object (in any scope).

Yes

No

name

Specifies the name of the object that contains the collection that will be iterated over. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return the collection that will be iterated over.

Yes

No

offset

Specifies the zero-relative index to start the collection iteration at. If not specified, iteration will start at 0 (the beginning). This attribute can specify an integer value or the name of an Integer object (in any scope).

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return the collection that will be iterated over.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

type

Specifies the fully qualified class type (e.g., java.lang.Integer) to expose each collection element as, with the id attribute. A ClassCastException will be thrown at run time if any of the collection's elements are not assignment compatible with this type. If not specified, no type conversions will be performed and each element will be exposed as type Object.

Yes

No

Example Usage

As mentioned, there are three basic ways you can use the iterate tag. The first way, shown here, uses the collection attribute to specify the collection to iterate over:

<logic:iterate  collection="<%=results%>"> Result: <%=result%><br> </logic:iterate>

This example assumes you have defined a collection named "results" and then iterates over each result, printing it. Notice that the collection attribute was specified with an expression. This attribute only accepts expressions. Anything else will result in a JspException being thrown at run time.

The second way to use the iterate tag is shown here:

<logic:iterate  name="results"> Result: <bean:write name="result"/><br> </logic:iterate> 

This example is similar to the first example; however, it differs in that the collection to be iterated over is specified with the name attribute. Remember that you can explicitly specify the scope of the object, defined by the name attribute, with the scope attribute.

The following is the third and final way to use the iterate tag:

<logic:iterate  name="searchObj" property="results"> Result: <bean:write name="result"/><br> </logic:iterate>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return a collection to be iterated over. Again, you can explicitly specify the scope of the object with the scope attribute.

The lessEqual Tag

The lessEqual tag is used to wrap content that is conditionally processed based on whether a specified object's value is less than or equal to a specified constant value. This tag works for both numeric and string comparisons.

There are five ways you can use the lessEqual tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value will be used for the comparison.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose value will be used for the comparison.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose value will be used for the comparison.

  • You can use the name attribute to specify the name of an object whose value will be used for the comparison. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used for the comparison. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose value is used for comparison.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose value is used for comparison.

Yes

No

name

Specifies the name of an object whose value is used for comparison. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object whose value is used for comparison.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose value is used for comparison.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object whose value is used for comparison.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

value

Specifies the constant value to which the value, specified by other attributes of this tag, will be compared.

Yes

Yes

Example Usage

As mentioned, there are five ways you can use the lessEqual tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value is compared against the value specified with the value attribute:

<logic:lessEqual cookie="size" value="0"> Size is invalid. </logic:lessEqual>

If the cookie's value is less than or equal to the value attribute's value, the content between the starting and ending lessEqual tags will be processed. Otherwise, it will be ignored.

The following is the second way to use the lessEqual tag:

<logic:lessEqual header="Content-Length" value="500"> Page has small amount of content. </logic:lessEqual>

This example compares the incoming request's Content-Length header against the value attribute to determine whether the content length is less than or equal to 500. If so, the content between the starting and ending lessEqual tags will be processed.

The third way to use the lessEqual tag is shown here:

<logic:lessEqual parameter="catId" value="10"> Category Id is less than or equal to 10. </logic:lessEqual>

This example compares a request parameter from the incoming request against the value of the value attribute. If the parameter is less than or equal to the value, the content between the starting and ending lessEqual tags will be processed.

The fourth way to use the lessEqual tag is shown here:

<logic:lessEqual name="resultCount" value="19"> Search returned less than 20 results. </logic:lessEqual>

This example compares the value of the resultCount object against the value of the value attribute. If the resultCount object's value is less than or equal to the value of the value attribute, the content between the starting and ending lessEqual tags will be processed. Remember that you can explicitly specify the scope of the resultCount object with the scope attribute.

The following is the fifth and final way to use the lessEqual tag:

<logic:lessEqual name="employee" property="age" value="20"> Employee is not old enough to drink. </logic:lessEqual>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be compared against that of the value attribute. Again, you can explicitly specify the scope of the object with the scope attribute.

The lessThan Tag

The lessThan tag is used to wrap content that is conditionally processed based on whether a specified object's value is less than a specified constant value. This tag works for both numeric and string comparisons.

There are five ways you can use the lessThan tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value will be used for the comparison.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose value will be used for the comparison.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose value will be used for the comparison.

  • You can use the name attribute to specify the name of an object whose value will be used for the comparison. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used for the comparison. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose value is used for comparison.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose value is used for comparison.

Yes

No

name

Specifies the name of an object whose value is used for comparison. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object whose value is used for comparison.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose value is used for comparison.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object whose value is used for comparison.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

value

Specifies the constant value to which the value, specified by other attributes of this tag, will be compared.

Yes

Yes

Example Usage

As mentioned, there are five ways you can use the lessThan tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value is compared against the value specified with the value attribute:

<logic:lessThan cookie="size" value="1"> Size is invalid. </logic:lessThan>

If the cookie's value is less than the value attribute's value, the content between the starting and ending lessThan tags will be processed. Otherwise, it will be ignored.

The second way to use the lessThan tag is shown here:

<logic:lessThan header="Content-Length" value="500"> Page has small amount of content. </logic:lessThan> 

This example compares the incoming request's Content-Length header against the value attribute to determine whether the content length is less than 500. If so, the content between the starting and ending lessThan tags will be processed.

The following is the third way to use the lessThan tag:

<logic:lessThan parameter="catId" value="10"> Category Id is less than 10. </logic:lessThan>

This example compares a request parameter from the incoming request against the value of the value attribute. If the parameter is less than the value, the content between the starting and ending lessThan tags will be processed.

The following is the fourth way to use the lessThan tag:

<logic:lessThan name="resultCount" value="1"> Search returned no results. </logic:lessThan>

This example compares the value of the resultCount object against the value of the value attribute. If the resultCount object's value is less than the value of the value attribute, the content between the starting and ending lessThan tags will be processed. Remember that you can explicitly specify the scope of the resultCount object with the scope attribute.

The fifth and final way to use the lessThan tag is shown here:

<logic:lessThan name="employee" property="age" value="21"> Employee is not old enough to drink. </logic:lessThan>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be compared against that of the value attribute. Again, you can explicitly specify the scope of the object with the scope attribute.

The match Tag

The match tag is used to wrap content that is conditionally processed based on whether a specified object's value contains, starts with, or ends with a specified constant value. This tag essentially provides a convenient interface to the String object's indexOf( ), startsWith( ), and endsWith( ) methods. If the location attribute of this tag is not specified, an indexOf( ) match will be performed to see if the specified object's value contains the value specified by the value attribute. If the location attribute is specified as "start", a startsWith( ) match will be performed to see if the specified object's value starts with the value specified by the value attribute. Finally, if the location attribute is specified as "end", an endsWith( ) match will be performed to see if the specified object's value ends with the value specified by the value attribute. Note that all of the matches are case sensitive.

There are five ways you can use the match tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value will be used to match against.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose value will be used to match against.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose value will be used to match against.

  • You can use the name attribute to specify the name of an object whose value will be used to match against. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used to match against. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose value is matched against.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose value is matched against.

Yes

No

location

Accepts start or end to specify where the match should occur. If not specified, the match can occur at any location.

Yes

No

name

Specifies the name of an object whose value is matched against. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object whose value is matched against.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose value is matched against.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object whose value is matched against.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

value

Specifies the constant value that other attributes of this tag should contain, start with, or end with.

Yes

Yes

Example Usage

As mentioned, there are five ways you can use the match tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value is used to match the value specified with the value attribute:

<logic:match cookie="roles" value="Manager"> User is a Manager. </logic:match>

If the cookie's value contains the value specified by the value attribute, the content between the starting and ending match tags will be processed. Otherwise, it will be ignored.

The second way to use the match tag is shown here:

<logic:match header="User-Agent" location="end" value="Windows NT 5.1)"> Browser is Windows-based. </logic:match>

This example checks to see if the incoming request's User-Agent header ends with the value specified by the value attribute. If so, the content between the starting and ending match tags will be processed.

The third way to use the match tag is shown here:

<logic:match parameter="category" location="start" value="Cloth"> Category is Clothes. </logic:match>

This example checks to see if the parameter from the incoming request starts with the value of the value attribute. If so, the content between the starting and ending match tags will be processed.

The following is the fourth way to use the match tag:

<logic:match name="lastName" location="start" value="H"> Last name starts with "H". </logic:match>

This example checks to see if the value of the lastName object starts with the value of the value attribute. If so, the content between the starting and ending match tags will be processed. Remember that you can explicitly specify the scope of the lastName object with the scope attribute.

The fifth and final way to use the match tag is shown here:

<logic:match name="employee"              property="lastName"              location="start"              value="H"> Last name starts with "H". </logic:match>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used to match that of the value attribute. Again, you can explicitly specify the scope of the object with the scope attribute.

The messagesNotPresent Tag

The messagesNotPresent tag is used to wrap content that is conditionally processed based on whether Struts' org.apache.struts.action.ActionErrors or org.apache.struts.action.ActionMessages objects do not have any errors or messages in them, respectively. Additionally, you can optionally specify a particular property to check for the presence of errors or messages with the property attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

message

Accepts true or false to specify whether the presence of messages is checked for. If set to false, the presence of errors will be checked for. Note that if this attribute is set to true, the name attribute will be ignored.

Defaults to false.

Yes

No

name

Specifies the key to use to look up an org.apache.struts.action.ActionErrors or org.apache.struts.action.ActionMessages object that will be checked for the presence of errors or messages, respectively.

Yes

No

property

Specifies the name of a particular property to check for the presence of errors or messages. If not specified, the presence of any messages or errors will be checked for.

Yes

No

Example Usage

The following example illustrates the basic usage of the messagesNotPresent tag:

<logic:messagesNotPresent> No errors are present. </logic:messagesNotPresent>

This example simply checks if no errors are present. The following example checks if no errors are present for a specific property named "username":

<logic:messagesNotPresent property="username"> No errors are present for username property. </logic:messagesNotPresent> 

The messagesPresent Tag

The messagesPresent tag is used to wrap content that is conditionally processed based on whether Struts' org.apache.struts.action.ActionErrors or org.apache.struts.action

.ActionMessages object has any errors or messages in it, respectively. Additionally, you can optionally specify a particular property to check for the presence of errors or messages with the property attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

message

Accepts true to false to specify whether the presence of messages is checked for. If set to false, the presence of errors will be checked for. Note that if this attribute it set to true, the name attribute will be ignored. Defaults to false.

Yes

No

name

Specifies the key to use to look up an org.apache.struts.action.ActionErrors or org.apache.struts.action.ActionMessages object that will be checked for the presence of errors or messages, respectively.

Yes

No

property

Specifies the name of a particular property to check for the presence of errors or messages. If not specified, the presence of any messages or errors will be checked for.

Yes

No

Example Usage

The following example illustrates the basic usage of the messagesPresent tag:

<logic:messagesPresent> Errors are present. </logic:messagesPresent>

This example simply checks if any errors are present. The following example checks if there are any errors present for a specific property named "username":

<logic:messagesPresent property="username"> Errors are present for username property. </logic:messagesPresent>

The notEmpty Tag

The notEmpty tag is used to wrap content that is conditionally processed based on whether the specified object is non-null and does not contain an empty (zero-length) String. Additionally, if the object specified implements java.util.Collection or java.util.Map, this tag will call the object's isEmpty( ) method to determine if the object is not empty. If the object is not empty, then the wrapped content will be processed.

Attributes

Attribute

Description

Accepts JSP Expression

Required

name

Specifies the name of an object to check as being not empty. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object to check as being not empty.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object to check as being not empty.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

Example Usage

There are two ways you can use the notEmpty tag. The first way, shown here, uses the name attribute to specify the name of an object to check as being not empty:

<logic:notEmpty name="results"> Your search returned results! </logic:notEmpty>

If the results object is non-null, does not contain a zero-length string, and does not contain a Collection or Map with zero elements, then the content between the starting and ending empty tags will be processed. Remember that you can explicitly specify the scope of the object with the scope attribute.

The second way to use the notEmpty tag is shown here:

<logic:notEmpty name="bizObj" property="results"> Your search returned results! </logic:empty>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object to check as being not empty. Again, you can explicitly specify the scope of the object with the scope attribute.

The notEqual Tag

The notEqual tag is used to wrap content that is conditionally processed based on whether a specified object's value is not equal to a specified constant value. This tag works for both numeric and string comparisons.

There are five ways you can use the notEqual tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value will be used for the comparison.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose value will be used for the comparison.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose value will be used for the comparison.

  • You can use the name attribute to specify the name of an object whose value will be used for the comparison. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used for the comparison. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose value is used for comparison.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose value is used for comparison.

Yes

No

name

Specifies the name of an object whose value is used for comparison. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object whose value is used for comparison.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose value is used for comparison.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object whose value is used for comparison.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

value

Specifies the constant value that will be compared.

Yes

Yes

Example Usage

As mentioned, there are five ways you can use the notEqual tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value is compared against the value specified with the value attribute:

<logic:notEqual cookie="role" value="Manager"> User is NOT a Manager. </logic:notEqual>

If the cookie's value and the value attribute's value are not equal, the content between the starting and ending notEqual tags will be processed. Otherwise, it will be ignored.

The following is the second way to use the notEqual tag:

<logic:notEqual header="Content-Length" value="1000"> Content length is not 1,000. </logic:notEqual>

This example compares the incoming request's Content-Length header against the value attribute to determine whether they are equal. If they are not equal, the content between the starting and ending notEqual tags will be processed.

The third way to use the notEqual tag is shown here:

<logic:notEqual parameter="catId" value="10"> Category Id is NOT 10. </logic:notEqual>

This example compares a request parameter from the incoming request against the value of the value attribute. If the two values are not equal, the content between the starting and ending notEqual tags will be processed.

The following is the fourth way to use the notEqual tag:

<logic:notEqual name="resultCount" value="0"> Search returned results. </logic:notEqual>

This example compares the value of the resultCount object against the value of the value attribute. If the two values are not equal, the content between the starting and ending notEqual tags will be processed. Remember that you can explicitly specify the scope of the resultCount object with the scope attribute.

The fifth and final way to use the notEqual tag is shown here:

<logic:notEqual name="employee" property="gender" value="male"> Employee is a female. </logic:notEqual>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be compared against that of the value attribute. Again, you can explicitly specify the scope of the object with the scope attribute.

The notMatch Tag

The notMatch tag is used to wrap content that is conditionally processed based on whether a specified object's value does not contain, start with, or end with a specified constant value. This tag essentially provides a convenient interface to the String object's indexOf( ), startsWith( ), and endsWith( ) methods. If the location attribute of this tag is not specified, an indexOf( ) match will be performed to see if the specified object's value does not contain the value specified by the value attribute. If the location attribute is specified as "start", a startsWith( ) match will be performed to see if the specified object's value does not start with the value specified by the value attribute. Finally, if the location attribute is specified as "end", an endsWith( ) match will be performed to see if the specified object's value does not end with the value specified by the value attribute. Note that all of the matches are case sensitive.

There are five ways you can use the notMatch tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value will be used to match against.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose value will be used to match against.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose value will be used to match against.

  • You can use the name attribute to specify the name of an object whose value will be used to match against. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used to match against. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose value is matched against.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose value is matched against.

Yes

No

location

Accepts start or end to specify where the match should occur. If not specified, the match can occur at any location.

Yes

No

name

Specifies the name of an object whose value is matched against. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object whose value is matched against.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose value is matched against.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object whose value is matched against.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

value

Specifies the constant value that attributes of this tag should contain, start with, or end with.

Yes

Yes

Example Usage

As mentioned, there are five ways you can use the notMatch tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose value is used to match the value specified with the value attribute:

<logic:notMatch cookie="roles" value="Manager"> User is not a Manager. </logic:notMatch>

If the cookie's value does not contain the value specified by the value attribute, the content between the starting and ending notMatch tags will be processed. Otherwise, it will be ignored.

The second way to use the notMatch tag is shown here:

<logic:notMatch header="User-Agent" location="end" value=""Windows NT 5.1)"> Browser is not Windows XP-based. </logic:notMatch>

This example checks to see if the incoming request's User-Agent header does not end with the value specified by the value attribute. If so, the content between the starting and ending notMatch tags will be processed.

The following is the third way to use the notMatch tag:

<logic:notMatch parameter="category" location="start" value="Cloth"> Category is not Clothes. </logic:notMatch>

This example checks to see if the parameter from the incoming request does not start with the value of the value attribute. If so, the content between the starting and ending notMatch tags will be processed.

The following is the fourth way to use the notMatch tag:

<logic:notMatch name="lastName" location="start" value="H"> Last name does not start with "H". </logic:notMatch>

This example checks to see if the value of the lastName object does not start with the value of the value attribute. If so, the content between the starting and ending notMatch tags will be processed. Remember that you can explicitly specify the scope of the lastName object with the scope attribute.

The fifth and final way to use the notMatch tag is shown here:

<logic:notMatch name="employee"                 property="lastName"                 location="start"                 value="H"> Last name starts with "H". </logic:notMatch>

In this example, the name and property attributes are used in tandem to specify the name of an object and its field whose getter method will be called to return an object whose value will be used to match that of the value attribute. Again, you can explicitly specify the scope of the object with the scope attribute.

The notPresent Tag

The notPresent tag is used to wrap content that is conditionally processed based on whether a specified object does not exist. That is, this tag looks up the specified object, and if it is not found, the wrapped content will be processed. If the specified object is found, the content will be skipped.

In addition to the standard existence checks this tag offers, it provides a mechanism for interacting with the J2EE security system to determine if there is an authenticated user with a given name or role. For more information on security in Struts applications, refer to Chapter 19.

There are seven ways you can use the notPresent tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose existence will be checked for.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose existence will be checked for.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose existence will be checked for.

  • You can use the name attribute to specify the name of an object whose existence will be checked for. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and one of its fields whose existence will be checked for. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the role attribute to specify a list of roles to check the currently authenticated user against. If the current user is in one of the roles, the existence check passes.

  • You can use the user attribute to specify a name to compare against the currently authenticated user. If the name matches the user, the existence check passes.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose existence is checked for.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose existence is checked for.

Yes

No

name

Specifies the name of an object whose existence is checked for. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its existence checked for.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose existence is checked for.

Yes

No

property

Specifies the field of the object specified by the name attribute whose existence is checked for.

Yes

No

role

Specifies the list of roles to check the currently authenticated user against. Multiple roles are delimited by commas (i.e., "role1,role2,role3").

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

user

Specifies a name to compare against the currently authenticated user.

Yes

No

Example Usage

As mentioned, there are seven ways you can use the notPresent tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose existence is checked for:

<logic:notPresent cookie="role"> No role cookie exists. </logic:notPresent>

If the incoming request does not have a cookie named "role" in it, the content between the starting and ending notPresent tags will be processed. Otherwise, it will be ignored.

The second way to use the notPresent tag is shown here:

<logic:notPresent header="Host"> Host header was not specified. </logic:notPresent>

This example checks the incoming request for a header named "Host". If the header does not exist, the content between the starting and ending notPresent tags will be processed.

The following is the third way to use the notPresent tag:

<logic:notPresent parameter="catId"> Category Id was not specified. </logic:notPresent>

This example checks the incoming request for a parameter name "catId". If the parameter does not exist, the content between the starting and ending notPresent tags will be processed.

The fourth way to use the notPresent tag is shown here:

<logic:notPresent name="results"> Results object exists. </logic:notPresent>

This example looks up an object named "results" to see if it exists. If it does not, the content between the starting and ending notPresent tags will be processed. Remember that you can explicitly specify the scope of the results object with the scope attribute.

The following is the fifth way to use the notPresent tag:

<logic:notPresent name="employee" property="name"> Employee object does not have a name field. </logic:notPresent>

In this example, the name and property attributes are used in tandem to specify the name of an object and one of its fields that is checked for existence. Again, you can explicitly specify the scope of the object with the scope attribute.

The following is the sixth way to use the notPresent tag:

<logic:notPresent role="manager"> James is not part of the manager group. </logic:notPresent> 

This example checks if the currently authenticated user is associated with the "manager" role. If not, the content between the starting and ending notPresent tags will be processed.

The seventh and final way to use the notPresent tag is shown here:

<logic:notPresent user="jholmes"> James Holmes is not currently logged in. </logic:notPresent>

This example checks if there is currently an authenticated user and if that user's name is "jholmes." If not, the content between the starting and ending notPresent tags will be processed. Otherwise, the content will be skipped.

The present Tag

The present tag is used to wrap content that is conditionally processed based on whether a specified object exists. That is, this tag looks up the specified object, and if it is found

(regardless of its value), the wrapped content will be processed. If the specified object is not found, the content will be skipped.

In addition to the standard existence checks this tag offers, it provides a mechanism for interacting with the J2EE security system to determine if there is an authenticated user with a given name or role. For more information on security in Struts applications, refer to Chapter 19.

There are seven ways you can use the present tag, as listed here and shown later in the section "Example Usage":

  • You can use the cookie attribute to specify the name of an HTTP cookie from the incoming request whose existence will be checked for.

  • You can use the header attribute to specify the name of an HTTP header from the incoming request whose existence will be checked for.

  • You can use the parameter attribute to specify the name of a parameter from the incoming request whose existence will be checked for.

  • You can use the name attribute to specify the name of an object whose existence will be checked for. This object can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the name and property attributes in tandem to specify the name of an object and one of its fields whose existence will be checked for. Again, the object specified by the name attribute can be in any scope or limited to a specific scope with the scope attribute.

  • You can use the role attribute to specify a list of roles to check the currently authenticated user against. If the current user is in one of the roles, the existence check passes.

  • You can use the user attribute to specify a name to compare against the currently authenticated user. If the name matches the user, the existence check passes.

Attributes

Attribute

Description

Accepts JSP Expression

Required

cookie

Specifies the name of the HTTP cookie from the incoming request whose existence is checked for.

Yes

No

header

Specifies the name of the HTTP header from the incoming request whose existence is checked for.

Yes

No

name

Specifies the name of an object whose existence is checked for. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its existence checked for.

Yes

No

parameter

Specifies the name of the parameter from the incoming request whose existence is checked for.

Yes

No

property

Specifies the field of the object specified by the name attribute whose existence is checked for.

Yes

No

role

Specifies the list of roles to check the currently authenticated user against. Multiple roles are delimited by commas (i.e., "role1,role2,role3").

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

user

Specifies a name to compare against the currently authenticated user.

Yes

No

Example Usage

As mentioned, there are seven ways you can use the present tag. The first way, shown here, uses the cookie attribute to specify the name of an HTTP cookie from the incoming request whose existence is checked for:

<logic:present cookie="role"> Role cookie exists. </logic:present>

If the incoming request has a cookie named "role" in it, the content between the starting and ending present tags will be processed. Otherwise, it will be ignored.

The following is the second way to use the present tag:

<logic:present header="Host"> Host header was specified. </logic:present> 

This example checks the incoming request for a header named "Host". If the header exists, the content between the starting and ending present tags will be processed.

The third way to use the present tag is shown here:

<logic:present parameter="catId"> Category Id was specified. </logic:present>

This example checks the incoming request for a parameter name "catId". If the parameter exists, the content between the starting and ending present tags will be processed.

The following is the fourth way to use the present tag:

<logic:present name="results"> Results object exists. </logic:present>

This example looks up an object named "results" to see if it exists. If it does, the content between the starting and ending present tags will be processed. Remember that you can explicitly specify the scope of the results object with the scope attribute.

The fifth way to use the present tag is shown here:

<logic:present name="employee" property="name"> Employee object has a name field. </logic:present>

In this example, the name and property attributes are used in tandem to specify the name of an object and one of its fields that is checked for existence. Again, you can explicitly specify the scope of the object with the scope attribute.

The sixth way to use the present tag is shown here:

<logic:present role="manager"> James is part of the manager group. </logic:present>

This example checks if the currently authenticated user is associated with the "manager" role. If so, the content between the starting and ending present tags will be processed.

The seventh and final way to use the present tag is shown here:

<logic:present user="jholmes"> James Holmes is currently logged in. </logic:present>

This example checks if there is currently an authenticated user and if that user's name is

"jholmes." If so, the content between the starting and ending present tags will be processed. Otherwise, the content will be skipped.

The redirect Tag

The redirect tag is used to compose a URL to another page and then redirect to it. The URL is composed by specifying a base URL and optionally specifying an anchor and/or query string parameters to add to the URL.

There are four ways to specify the base URL:

  • You can use the action attribute to specify the name of an action from the Struts configuration file whose URL will be used.

  • You can use the forward attribute to specify the name of a forward from the Struts configuration file whose URL will be used.

  • You can use the href attribute to specify an absolute URL, including protocol (e.g., http://www.yahoo.com/).

  • You can use the page attribute to specify an application-relative URL.

In addition to specifying the base URL, you have two options for specifying query string parameters to add to the base URL:

  • You can use the paramId attribute in conjunction with the paramName attribute, and optionally the paramProperty attribute, to specify a single parameter.

  • You can use the name attribute, either alone or in tandem with the property attribute, to specify a java.util.Map object that will be used to add several parameters.

Attributes

Attribute

Description

Accepts JSP Expression

Required

action

Specifies the name of an action from the Action Mappings Configuration section of the Struts configuration file, which contains the URL that will be used as the base of the URL generated by this tag.

Yes

No

anchor

Specifies the anchor (e.g., "#bottom") to be added to the URL generated by this tag. This value should be specified without the leading hash (#) character.

Yes

No

forward

Specifies the name of a forward, from the Global Forwards Configuration section of the Struts configuration file, which contains the URL that will be used as the base of the URL generated by this tag.

Yes

No

href

Specifies the absolute URL (including protocol, such as http://) that will be used as the base of the URL generated by this tag.

Yes

No

name

Specifies the name of the java.util.Map object whose elements are added as query string parameters to the URL generated by this tag. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return the java.util.Map object whose elements are added as query string parameters to the URL generated by this tag.

Yes

No

page

Specifies the application-relative URL (starts with a leading slash, /) that will be used as the base of the URL generated by this tag.

Yes

No

paramId

Specifies the name of a single parameter to add to the URL generated by this tag.

Yes

No

paramName

Specifies the name of an object whose value will be used as the value for the parameter specified with the paramId attribute. If the property attribute is also specified, one of the fields of the object defined by this attribute will have its getter method called to return an object whose value will be used as the value for the parameter specified with the paramId attribute.

Yes

No

paramProperty

Specifies the field of the object specified by the name attribute whose getter method will be called to return an object whose value will be used as the value for the parameter specified with the paramId attribute.

Yes

No

paramScope

Specifies the scope (application, page, request, or session) to look in for the object specified by the paramName attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

property

Specifies the field of the object specified by the name attribute whose getter method will be called to return the java.util.Map object whose elements are added as query string parameters to the URL generated by this tag.

Yes

No

scope

Specifies the scope (application, page, request, or session) to look in for the object specified by the name attribute. If not specified, each scope will be searched, in this order: page, request, session, and then application.

Yes

No

transaction

Accepts true or false to specify whether the current transaction token will be included in the URL generated by this tag. Defaults to false.

Yes

No

useLocalEncoding

Accepts true or false to specify whether the character encoding of the URL parameters should be performed using the encoding type of the HTTP response object. If this attribute is set to false, the encoding type defaults to UTF-8. Defaults to false.

Yes

No

Example Usage

There are a few different ways to use the redirect tag. The first way, shown here, uses the href attribute to specify an absolute URL to redirect to:

<logic:redirect href="http://www.yahoo.com/"/>

Upon this tag's execution, an HTTP redirect response will be sent to the requesting browser. The browser will then request the specified URL.

The following example adds to the first example by specifying a single query string parameter to add to the base URL specified by the href attribute:

<logic:redirect href="http://www.yahoo.com/"                 param                 paramName="queryObj"/>

This example takes the base URL specified by the href attribute and appends the query string parameter specified by the paramId and paramName attributes and composes a URL that the tag then redirects to.

Another way to use the redirect tag is shown here:

<logic:redirect page="/search.jsp" name="params"/> 

This example uses the page attribute to specify an application-relative base URL and the name attribute to specify a java.util.Map object whose entries are added to the URL as query string parameters.



Struts. The Complete Reference
Struts: The Complete Reference, 2nd Edition
ISBN: 0072263865
EAN: 2147483647
Year: 2004
Pages: 165
Authors: James Holmes

Similar book on Amazon
Struts 2 in Action
Struts 2 in Action
Murach's Java Servlets and JSP, 2nd Edition
Murach's Java Servlets and JSP, 2nd Edition
Jakarta Struts For Dummies
Jakarta Struts For Dummies
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam
Head First Servlets and JSP: Passing the Sun Certified Web Component Developer Exam

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net