You are on page 1of 11

Logic Tags

• The Logic tag library contains tags that are


useful in managing conditional generation
of output text, looping over object
collections for repetitive generation of
output text, and application flow
management.
Logic Tags
• For tags that do value comparisons (equal,
greaterEqual, greaterThan, lessEqual, lessThan,
notEqual), the following rules apply:
– The specified value is examined. If it can be converted
successfully to a double or a long, it is assumed that the
ultimate comparison will be numeric (either floating point or
integer). Otherwise, a String comparison will be performed.
– The variable to be compared to is retrieved, based on the
selector attribute(s) (cookie, header, name, parameter,
property) present on this tag. It will be converted to the
appropriate type for the comparison, as determined above.
– A request time exception will be thrown if the specified variable
cannot be retrieved, or has a null value.
– The specific comparison for this tag will be performed, and the
nested body content of this tag will be evaluated if the
comparison returns a true result.
Logic Tags
• For tags that do substring matching (match,
notMatch), the following rules apply:
– The specified variable is retrieved, based on the
selector attribute(s) (cookie, header, name,
parameter, property) present on this tag. The
variable is converted to a String, if necessary.
– A request time exception will be thrown if the specified
variable cannot be retrieved, or has a null value.
– The specified value is checked for existence as a
substring of the variable, in the position specified by
the location attribute, as follows: at the beginning (if
location is set to start), at the end (if location is set to
end), or anywhere (if location is not specified).
Logic Tags
Tag Name Description
Evaluate the nested body content of this tag if the requested variable is either
empty
null or an empty string.
Evaluate the nested body content of this tag if the requested variable is equal
equal
to the specified value.
forward Forward control to the page specified by the specified ActionForward entry.
Evaluate the nested body content of this tag if requested variable is greater
greaterEqual
than or equal to specified value.
Evaluate the nested body content of this tag if the requested variable is greater
greaterThan
than the specified value.
iterate Repeat the nested body content of this tag over a specified collection.
Evaluate the nested body content of this tag if requested variable is greater
lessEqual
than or equal to specified value.
Evaluate the nested body content of this tag if the requested variable is less
lessThan
than the specified value.
Evaluate the nested body content of this tag if specified value is an appropriate
match
substring of requested variable.
messagesNotPrese Generate the nested body content of this tag if the specified message is not
nt present in this request.
Generate the nested body content of this tag if the specified message is present
messagesPresent
in this request.
Evaluate the nested body content of this tag if the requested variable is neither
notEmpty
null nor an empty string.
Evaluate the nested body content of this tag if the requested variable is not
notEqual
equal to the specified value.
Evaluate the nested body content of tag if specified value not an appropriate
notMatch
substring of requested variable.
Generate the nested body content of this tag if the specified value is not
notPresent
present in this request.
Generate the nested body content of this tag if the specified value is present in
present
this request.
redirect Render an HTTP Redirect
Logic Tags - Example
<html:html>
<head>
<title><bean:message key="imagebrokerlink.title"/></title>
<META name="GENERATOR" content="IBM WebSphere Studio">
</head>
<body>
<html:form action="/ImageLocationForm" >
<center>
<font size=3>
<br>
<b>
<logic:notEqual property="action" name="ImageLocationForm" value="Insert">
<bean:message key="imagelocationdetail.title"/>
</logic:notEqual>
<logic:equal property="action" name="ImageLocationForm" value="Insert">
<bean:message key="imagelocationinsert.title"/>
</logic:equal>

Using the Logic Tag Library

To use the Logic Tag Library in a Struts application, your application's JSPs must
declare their use of the library with a JSP taglib directive:
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>

<logic:present name="searchForm" property="results">

if you chose to use a prefix of "strutslgc",

<strutslgc:present name="searchForm" property="results">


The empty Tag

Example Usage

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

<logic:empty name="bizObj" property="results">


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

you can explicitly specify the scope of the object with the scope attribute.
Example Usage

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

<logic:equal header="User-Agent" value="Mozilla/4.0 (compatible; MSIE 6.0;


Windows NT 5.1)"> Browser is Internet Explorer. </logic:equal>

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

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


</logic:equal>

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


</logic:equal>
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

<logic:iterate id="result" name="results">


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

<logic:iterate id="result" name="searchObj" property="results">


Result: <bean:write name="result"/><br>
</logic:iterate>
The match Tag

<logic:match header="User-Agent" location="end" value="Windows NT 5.1)">


Browser is Windows-based.
</logic:match>

<logic:match parameter="category" location="start" value="Cloth">


Category is Clothes.
</logic:match>

<logic:match name="employee" property="lastName" location="start" value="H“>


Last name starts with "H".
</logic:match>
The messagesNotPresent Tag
<logic:messagesNotPresent>
No errors are present.
</logic:messagesNotPresent>
The present Tag

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

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

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


field. </logic:present>

The redirect Tag

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


paramName="queryObj"/>

You might also like