Using Struts Logic Tags That Perform Conditional Display Based on a Value


This section provides information about the following tags:

  • <logic:equal> ” Evaluates its contents if the variable is equal to a value

  • <logic:greaterEqual> ” Evaluates its contents if the variable is greater than or equal to a value

  • <logic:greaterThan> ” Evaluates its contents if the variable is greater than a value

  • <logic:lessEqual> ” Evaluates its contents if the variable is less than or equal to a value

  • <logic:lessThan> ” Evaluates its contents if the variable is less than a value

  • <logic:notEqual> ” Evaluates its contents if the variable is not equal to a value

If you point your browser to the URL

 http://myAppServer/StrutsTaglibs/logic.jsp 

you bring up the main page that links to all the sample code for the Struts tag chapters. This section uses the Logic Servlet page at /StrutsTaglibs/LogicCompare.do . The rendered page is shown in Figure 14.2

Figure 14.2. Logic Compare page at /StrutsTaglibs/LogicCompare .do.

graphics/14fig02.jpg

This page demonstrates a typical use of each of the tags. The source is shown in Listing 14.1

Listing 14.1 JSP File Demonstrating Use of Struts Logic Tags That Compare Values and Conditionally Display ( LogicCompare.jsp )
 <%@ page language="java" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html:html> <head> <title>Logic Compare sample code</title> </head> <body bgcolor="white"> <h3>Logic Compare sample code</h3> <p>This page provides examples of the following Struts LOGIC tags:<br> <ul>   <li>&lt;logic:equal&gt;</li>   <li>&lt;logic:lessEqual&gt;</li>   <li>&lt;logic:lessThan&gt;</li>   <li>&lt;logic:greaterEqual&gt;</li>   <li>&lt;logic:greaterThan&gt;</li>   <li>&lt;logic:notEqual&gt;</li> </ul> <%-- Variables used on this page --%> <% request.setAttribute("numberStringValue", "090210"); request.setAttribute("numberIntValue", new Integer(90210)); request.setAttribute("numberDoubleValue", new Double(90210.0)); request.setAttribute("nonnumberStringValue", "90210a"); %> <table border="1" width="100%">   <%--   The following section shows equal and not equal.   --%>   <tr><td colspan="5" align="center">equal/notEqual   </td> </TR> <tr>   <td align="left" >value   </td>   <td>     String Value<BR>(090210)   </td>   <td>     Int Value<BR>(90210)   </td>   <td>     Float Value<BR>(90210.0)   </td>   <td>     NonNumber (90210a)   </td> </tr> <tr>   <td>     90210   </td>   <td>     <logic:equal name="numberStringValue" value="90210" scope="request">     equal     </logic:equal>     <logic:notEqual name="numberStringValue" value="90210" scope="request">     notEqual     </logic:notEqual>   </td>   <td>     <logic:equal name="numberIntValue" value="90210" scope="request">     equal     </logic:equal>     <logic:notEqual name="numberIntValue" value="90210" scope="request">     notEqual</logic:notEqual>   </td>   <td>     <logic:equal name="numberDoubleValue" value="90210" scope="request">     equal     </logic:equal>     <logic:notEqual name="numberDoubleValue" value="90210" scope="request">     notEqual     </logic:notEqual>   </td>   <td>     <logic:equal name="nonnumberStringValue" value="90210" scope="request">     equal     </logic:equal>     <logic:notEqual name="nonnumberStringValue" value="90210" scope="request">     notEqual     </logic:notEqual>   </td> </tr> <tr>   <td>     90210.0   </td>   <td>     <logic:equal name="numberStringValue" value="90210.0" scope="request">     equal     </logic:equal>     <logic:notEqual name="numberStringValue" value="90210.0" scope="request">     notEqual     </logic:notEqual>   </td>   <td>     <logic:equal name="numberIntValue" value="90210.0" scope="request">     equal     </logic:equal>     <logic:notEqual name="numberIntValue" value="90210.0" scope="request">     notEqual     </logic:notEqual>   </td>   <td>     <logic:equal name="numberDoubleValue" value="90210.0" scope="request">     equal     </logic:equal>     <logic:notEqual name="numberDoubleValue" value="90210.0" scope="request">     notEqual     </logic:notEqual>   </td>   <td>     <logic:equal name="nonnumberStringValue" value="90210.0" scope="request">     equal     </logic:equal>     <logic:notEqual name="nonnumberStringValue" value="90210.0" scope="request">     notEqual     </logic:notEqual>   </td> </tr> <tr>   <td>     90210a   </td>   <td>     <logic:equal name="numberStringValue" value="90210a" scope="request">     equal     </logic:equal>     <logic:notEqual name="numberStringValue" value="90210a" scope="request">     notEqual     </logic:notEqual>   </td>   <td>     <logic:equal name="numberIntValue" value="90210a" scope="request">     equal     </logic:equal>     <logic:notEqual name="numberIntValue" value="90210a" scope="request">     notEqual     </logic:notEqual>   </td>   <td>     <logic:equal name="numberDoubleValue" value="90210a" scope="request">     equal     </logic:equal>     <logic:notEqual name="numberDoubleValue" value="90210a" scope="request">     notEqual     </logic:notEqual>   </td>   <td>     <logic:equal name="nonnumberStringValue" value="90210a" scope="request">     equal     </logic:equal>     <logic:notEqual name="nonnumberStringValue" value="90210a" scope="request">     notEqual     </logic:notEqual>   </td> </tr> </table> <table border="1" width="100%">   <%--   The following section shows less than and greater than.   --%>   <tr><td colspan="5" align="center">lessThan/greaterThan   </td> </TR> <tr>   <td align="left" >value   </td>   <td>     String Value<BR>(090210)   </td>   <td>     Int Value<BR>(90210)   </td>   <td>     Float Value<BR>(90210.0)   </td>   <td>     NonNumber (90210a)   </td> </tr> <tr>   <td>     90210   </td>   <td>     <logic:lessThan name="numberStringValue" value="90210" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="numberStringValue" value="90210" scope="request">     greaterThan     </logic:greaterThan>&nbsp   </td>   <td>     <logic:lessThan name="numberIntValue" value="90210" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="numberIntValue" value="90210" scope="request">     greaterThan     </logic:greaterThan>&nbsp   </td>   <td>     <logic:lessThan name="numberDoubleValue" value="90210" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="numberDoubleValue" value="90210" scope="request">     greaterThan     </logic:greaterThan>&nbsp   </td>   <td>     <logic:lessThan name="nonnumberStringValue" value="90210" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="nonnumberStringValue" value="90210" scope="request">     greaterThan     </logic:greaterThan>&nbsp   </td> </tr> <tr>   <td>     90210.0   </td>   <td>     <logic:lessThan name="numberStringValue" value="90210.0" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="numberStringValue" value="90210.0" scope="request">     greaterThan     </logic:greaterThan>&nbsp   </td>   <td>     <logic:lessThan name="numberIntValue" value="90210.0" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="numberIntValue" value="90210.0" scope="request">     greaterThan     </logic:greaterThan>&nbsp   </td>   <td>     <logic:lessThan name="numberDoubleValue" value="90210.0" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="numberDoubleValue" value="90210.0" scope="request">     greaterThan     </logic:greaterThan>&nbsp   </td>   <td>     <logic:lessThan name="nonnumberStringValue" value="90210.0" scope="request">     lessThan     </logic:lessThan>   <logic:greaterThan name="nonnumberStringValue" value="90210.0" scope="request">     greaterThan     </logic:greaterThan>&nbsp   </td> </tr> <tr>   <td>     90210a   </td>   <td>     <logic:lessThan name="numberStringValue" value="90210a" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="numberStringValue" value="90210a" scope="request">     greaterThan     </logic:greaterThan>&nbsp;   </td>   <td>     <logic:lessThan name="numberIntValue" value="90210a" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="numberIntValue" value="90210a" scope="request">     greaterThan     </logic:greaterThan>&nbsp   </td>   <td>     <logic:lessThan name="numberDoubleValue" value="90210a" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="numberDoubleValue" value="90210a" scope="request">     greaterThan     </logic:greaterThan>&nbsp;   </td>   <td>     <logic:lessThan name="nonnumberStringValue" value="90210a" scope="request">     lessThan     </logic:lessThan>     <logic:greaterThan name="nonnumberStringValue" value="90210a" scope="request">     greaterThan     </logic:greaterThan>&nbsp;   </td> </tr> </table> <table border="1" width="100%">   <%--   The following section shows less than and greater than.   --%>   <tr><td colspan="5" align="center">lessEqual/greaterEqual   </td> </TR> <tr>   <td align="left" >value   </td>   <td>     String Value<BR>(090210)   </td>   <td>     Int Value<BR>(90210)   </td>   <td>     Float Value<BR>(90210.0)   </td>   <td>     NonNumber (90210a)   </td> </tr> <tr>   <td>     90210   </td>   <td>     <logic:lessEqual name="numberStringValue" value="90210" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="numberStringValue" value="90210" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp   </td>   <td>     <logic:lessEqual name="numberIntValue" value="90210" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="numberIntValue" value="90210" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp;   </td>   <td>     <logic:lessEqual name="numberDoubleValue" value="90210" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="numberDoubleValue" value="90210" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp   </td>   <td>     <logic:lessEqual name="nonnumberStringValue" value="90210" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="nonnumberStringValue" value="90210" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp   </td> </tr> <tr>   <td>     90210.0   </td>   <td>     <logic:lessEqual name="numberStringValue" value="90210.0" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="numberStringValue" value="90210.0" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp   </td>   <td>     <logic:lessEqual name="numberIntValue" value="90210.0" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="numberIntValue" value="90210.0" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp   </td>   <td>     <logic:lessEqual name="numberDoubleValue" value="90210.0" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="numberDoubleValue" value="90210.0" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp   </td>   <td>     <logic:lessEqual name="nonnumberStringValue" value="90210.0" scope="request">     lessEqual     </logic:lessEqual>   <logic:greaterEqual name="nonnumberStringValue" value="90210.0" scope="request">     greaterEqual   </logic:greaterEqual>&nbsp   </td> </tr> <tr>   <td>     90210a   </td>   <td>     <logic:lessEqual name="numberStringValue" value="90210a" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="numberStringValue" value="90210a" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp;   </td>   <td>     <logic:lessEqual name="numberIntValue" value="90210a" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="numberIntValue" value="90210a" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp;   </td>   <td>     <logic:lessEqual name="numberDoubleValue" value="90210a" scope="request">     lessEqual     </logic:lessEqual>     <logic:greaterEqual name="numberDoubleValue" value="90210a" scope="request">     greaterEqual     </logic:greaterEqual>&nbsp;   </td>   <td>     <logic:lessEqual name="nonnumberStringValue" value="90210a" scope="request">     lessEqual     </logic:lessEqual>   <logic:greaterEqual name="nonnumberStringValue" value="90210a" scope="request">     greaterEqual   </logic:greaterEqual>&nbsp;   </td> </tr> </table> </body> </html:html> 

All these tags follow essentially the same syntax. They perform a comparison between two values (one a constant, one a variable) and evaluate their contents if the test is true.

PERFORMING COMPARISONS BETWEEN NUMBERS AND STRINGS

One subtlety regarding the comparison tags: If both values can be successfully converted to numbers, the comparisons are done numerically rather than as literal strings. For example, "90210.0" , "0090210" , and 90210 " all compare as equal, and "0090211" compares as greater than "90210" , although the same zero-led string compares as less than "90210a" because one of the strings is nonnumeric.

All of these tags take their variable value from one of the following parameters:

  • cookie ” Looks for a cookie in the request with the specified name

  • header ” Looks for a request header value with the specified name

  • name ” Looks for a bean or bean property, if the property attribute is also set, of the specified scope (or any scope if the scope attribute isn't set)

  • parameter ” Looks for a request parameter (that is, a form value) of the specified name

If the specified variable doesn't exist or is null, the value is compared against a zero-length string.

The <logic:equal> and <logic:notEqual> Tags

The equal tag evaluates its contents if the variable equals the value, whereas the notEqual tag evaluates if the two differ . It's worth mentioning again that the comparison is numerical if both values can be converted to a double or a long.

The <logic:lessThan> and <logic:greaterThan> Tags

The lessThan and greaterThan tags evaluate their contents if the variable is (respectively) less or greater than the constant.

The <logic:lessEqual> and <logic:greaterEqual> Tags

These tags similar to the lessThan and greaterThan tags, but the tests here are less than or equal and greater than or equal.



Struts Kick Start
Struts Kick Start
ISBN: 0672324725
EAN: 2147483647
Year: 2002
Pages: 177

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