Package org.apache.struts.taglib.logic

The "struts-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.

See:
          Description

Class Summary
CompareTagBase Abstract base class for comparison tags.
ConditionalTagBase Abstract base class for the various conditional evaluation tags.
EmptyTag Evalute the nested body content of this tag if the specified value is empty for this request.
EqualTag Evaluate the nested body content of this tag if the specified variable and value are equal.
ForwardTag Perform a forward or redirect to a page that is looked up in the configuration information associated with our application.
GreaterEqualTag Evaluate the nested body content of this tag if the specified variable is greater than or equal to the specified value.
GreaterThanTag Evaluate the nested body content of this tag if the specified variable is greater than the specified value.
IterateTag Custom tag that iterates the elements of a collection, which can be either an attribute or the property of an attribute.
IterateTei Implementation of TagExtraInfo for the iterate tag, identifying the scripting object(s) to be made visible.
LessEqualTag Evaluate the nested body content of this tag if the specified variable is less than or equal to the specified value.
LessThanTag Evaluate the nested body content of this tag if the specified variable is less than the specified value.
MatchTag Evalute the nested body content of this tag if the specified value is a substring of the specified variable.
MessagesNotPresentTag Evalute the nested body content of this tag if the specified value is not present for this request.
MessagesPresentTag Evalute to true if an ActionMessages class or a class that can be converted to an ActionMessages class is in request scope under the specified key and there is at least one message in the class or for the property specified.
NotEmptyTag Evalute the nested body content of this tag if the specified value is not empty for this request.
NotEqualTag Evaluate the nested body content of this tag if the specified variable and value are not equal.
NotMatchTag Evalute the nested body content of this tag if the specified value is not a substring of the specified variable.
NotPresentTag Evalute the nested body content of this tag if the specified value is not present for this request.
PresentTag Evalute the nested body content of this tag if the specified value is present for this request.
RedirectTag Generate a URL-encoded redirect to the specified URI.
 

Package org.apache.struts.taglib.logic Description

The "struts-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.

[Introduction] [Logic Functionality] [Logic Properties] [Logic Examples]


Introduction

The Logic library supplies tags that are useful for manipulating presentation logic without the use of scriptlets.


Logic Tag UML

Logic Tag Functionality

The functionality of the logic tags can be divided into four groups:

Value Comparisons - The purpose of these tags is to print out the body of the tag if the comparison evaluates to true.

Substring Matching - The purpose of these tags is to match substrings inside of other Strings

Presentation Location - The purpose of these tags is to change the location of the presentation page

Collection Utilities -The purpose of these tags is to handle Collections

Logic Tag Properties

Each of the four groups of logic tags have a common set of attributes associated with them. :

Value Comparisons (equal, notEqual, greaterEqual, lessEqual, greaterThan, lessThan, present, notPresent)

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 float or double, then a number comparison is performed on the value given and the value of the comparison attribute. Otherwise a String comparison is performed. You have to specify one of the comparison attributes: cookie, header, parameter , property or name. For each of the examples, the tag " someComparisonTag"can be replaced by any of the value comparison tags.

Substring Matching (match, notMatch)

The substring matching tags take all the same arguments as the value comparison tags. You compare the String specified by value to any of the comparison values you give it, specified by cookie, header, parameter, property or name. Note that in the examples, matchTag corresponds either the match or notMatch tag. Matching tags also have an additional location attribute added:

Presentation Location (forward, redirect)

The redirect tag is resposible for sending a re-direct to the client's browser, complete with URL-rewriting if it's supported by the container. 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):

The forward tag is responsible for either redirecting or forwarding to a specified global action forward. To define a global ActionForward, see The Action Mappings Configuration File . You can specify whether the forward re-directs or forwards when executed in the config file. The forward tag has one attribute:

Collection Utilities (iterate)

The iterate tag is responsible for executing its body content once for every element inside of the specified Collection. There is one required attribute:

The other attributes allow for more flexibility on which Collection to iterate and how to do it:

<logic:iterate id="myCollectionElement" collection="<%=
            vector %>">
Do something with myCollectionElement </logic:iterate>

Logic Examples

Value Comparisons

Logic Equivalence Tags (equal, notEqual, greaterEqual, lessEqual, lessThan, greaterThan)

You can compare these tags to the "==", "!=" ,">=", "<=", "<", and ">"logic operators in most languages. Their usage is fairly straightforward for numbers. For an example, we'll create a small "Guess That Number" game that uses request parameters from a form input to play. The number will be hardcoded as "7", because this is just an example. Note that this is actually putting application logic inside of jsp pages, and isn't the recommended development method for Struts. It's just an easy way to show how these tags are used:

The first step is to develop the form that will call on the processing jsp page. This form will use the "GET" method so that you can see the request parameter in the URL. The POST method can also be used with no problem or changes.

[numberGuess.jsp]

<form action="numberProcess.jsp" method="GET">
Please Enter a Number From 1-10: <input type="text" name="number" /><br />
<center>
<input type="submit" name="Guess Number" />
</center>
</form>
The next step is to create the processing page. It uses the struts-logic taglib. For information on how to set this tag library up in your application to use, see The Web Application Deployment Descriptor

[numberProcess.jsp]

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!-- Is the number guess right? -->
<logic:equal parameter="number" value="7">
You guessed right! You win a high speed blender!
</logic:equal>
<!-- If the number guessed was wrong -->
<logic:notEqual parameter="number" value="7">
<!-- Less Than -->
<logic:lessThan parameter="number" value="7">
A little higher...
</logic:lessThan>
<!-- Greater Than -->
<logic:greaterThan parameter="number" value="7">
A little lower...
</logic:greaterThan>
</logic:notEqual>

Basically, the numberProcess.jsp page uses the equal tag to check if the guess is 7, and if so, prints out a congratulatory message. If the number isn't equal, specified by the use of the <logic:notEqual> tag, it uses the greaterThan and lessThan tags to check if the number is higher or lower than 7, and prints out a hint. As said before, this is a horribly designed small application, with no validity checks on the number input, but shows the basic usage of the logic equal tags

For String comparisons, the equal tags use the java.lang.String.compareTo() method. See the javadocs on the compareTo() method for more information, located here .

Match and Present Tags (match, notMatch, present, notPresent)

You use the match tags in conjunction with the present tags in order to do substring matches. For an example using this we'll use headers, specifically the "Referer" header. The HTTP referer header gives the URL of the document that refers to the requested URL. We'll use this to check if the user is coming from a link specified by a Google search, and offer a personalized greeting, frightening users that find our site through the search engine with our amazing intimate knowledge of their browsing habits:

[sneaky.jsp]

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>


<!-- Check to see if the "Referer" header is present -->
<logic:present header="Referer">
<logic:match header="Referer" value="google.com">
I see you found our site through Google... interesting.
</logic:match>
<logic:notMatch header="Referer" value="google.com">
Welcome to the site, we're secretly logging what site you came from,
because we're shady...
</logic:notMatch>
</logic:present>

<!-- If the header is not present -->
<logic:notPresent header="Referer">
Hi, welcome to our site. Please fill out our
<a href="nonExistantForm.jsp">Form</a> and
tell us where you're coming from.
</logic:notPresent>

Note: Another interesting usage of these tags and headers would be to use the "User-Agent" header to display browser-specific javascript.

Collection Utilities (iterate)

For an example of using the <logic:iterate> tag, we'll use one of the previous examples given, in it's entirety. This example uses the <bean:write> tag from the Bean Tag Library, see the User's Guide on the bean tag library for more information on it's usage:

[iterate.jsp]

<%@ page language="java" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<%
java.util.ArrayList list = new java.util.ArrayList();
list.add("First");
list.add("Second");
list.add("Third");
list.add("Fourth");
list.add("Fifth");
pageContext.setAttribute("list", list, PageContext.PAGE_SCOPE);
%>

<logic:iterate id="myCollectionElement" name="list">
Element Value: <bean:write name="myCollectionElement" /><br />
</logic:iterate>



Copyright © 2000-2008 Apache Software Foundation. All Rights Reserved.