The Struts logic tags
These tags are used to control the generation of page output by allowing conditions to be tested, loops
to be set up and application flow to be directed to other pages. A comparison will often involve a bean created
with one of the bean tags. A very simple example with a bean containing only a single property:
<bean:define id="string" value="Struts "/>
<logic:equal name="string" value="Struts">
The string contained the word Struts
</logic:equal> |
- Figure 10: logic:equal example
This will result in this output being shown in the browser:
The string contained the word Struts |
Let's make a more realistic--but still simple--example. We'll use the DVD class shown above. The
storing of the DVD instance in session scope is done in a scriptlet to simplify the example.
Normally you'd do this in your Action class:
<%
List actors = new LinkedList();
actors.add("Tom Hulce");
actors.add("Elizabeth Berridge");
DVD dvd = new DVD("Amadeus", 158, actors);
session.setAttribute("dvd", dvd);
%>
<logic:notEqual name="dvd" property="title" value="The Matrix">
This DVD is not The Matrix<br>
</logic:notEqual>
<logic:equal name="dvd" property="title" value="Amadeus">
This DVD is Amadeus<br>
</logic:equal> |
- Figure 11: logic:equal and logic:notEqual example
When run we'll see this output:
This DVD is not The Matrix
This DVD is Amadeus
The comparison tags are these:
|
equal |
notEqual |
|
greaterEqual |
lessEqual |
|
greaterThan |
lessThan |
|
match |
notMatch |
|
present |
notPresent |
- Figure 12: The comparison tags
The first 2*3 comparison tags use this rule (taken from the Struts tag documentation):
"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."
Besides comparing values with bean properties you may also compare with cookie, header, and request parameter
data. For example:
<logic:match header="User-Agent" value="Mozilla">
Hi Mozilla browser
</logic:match>
<logic:notMatch header="User-Agent" value="Mozilla">
You browser is not Mozilla - it is
<bean:header id="agent" name="User-Agent"/>
<bean:write name="agent"/>
</logic:notMatch>
|
- Figure 13: logic:match and logic:notMatch example
The present/notPresent tags are useful if you don't know if a property is available:
<logic:present header="referer">
You came to this page from
<bean:header id="ref" name="referer"/>
<bean:write name="ref"/>
</logic:present>
|
- Figure 14: logic:present example
The iterate tag
Probably the most interesting tag in the logic library is the iterate tag. It is used to iterate
over a collection, and you saw in Figure 5 how it is used:
<logic:iterate id="dvd" name="dvds" property="DVDList">
. . . (process each item in the collection returned by getDVDList)
</logic:iterate>
New on the Java Boutique:
New Review:
Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling
API boasts simplicity, ease-of-integration, a well-rounded feature
set, and it's free!
New Applet:
Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA
sequences into three useful formats.
Elsewhere on internet.com:
WebDeveloper Java
Lots of Java information on webdeveloper.com
WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.
ScriptSearch Java
Hundreds of free Java code files to download.
jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.