This section provides information about the following tags:
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.
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><logic:equal></li> <li><logic:lessEqual></li> <li><logic:lessThan></li> <li><logic:greaterEqual></li> <li><logic:greaterThan></li> <li><logic:notEqual></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>  </td> <td> <logic:lessThan name="numberIntValue" value="90210" scope="request"> lessThan </logic:lessThan> <logic:greaterThan name="numberIntValue" value="90210" scope="request"> greaterThan </logic:greaterThan>  </td> <td> <logic:lessThan name="numberDoubleValue" value="90210" scope="request"> lessThan </logic:lessThan> <logic:greaterThan name="numberDoubleValue" value="90210" scope="request"> greaterThan </logic:greaterThan>  </td> <td> <logic:lessThan name="nonnumberStringValue" value="90210" scope="request"> lessThan </logic:lessThan> <logic:greaterThan name="nonnumberStringValue" value="90210" scope="request"> greaterThan </logic:greaterThan>  </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>  </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>  </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>  </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>  </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> </td> <td> <logic:lessThan name="numberIntValue" value="90210a" scope="request"> lessThan </logic:lessThan> <logic:greaterThan name="numberIntValue" value="90210a" scope="request"> greaterThan </logic:greaterThan>  </td> <td> <logic:lessThan name="numberDoubleValue" value="90210a" scope="request"> lessThan </logic:lessThan> <logic:greaterThan name="numberDoubleValue" value="90210a" scope="request"> greaterThan </logic:greaterThan> </td> <td> <logic:lessThan name="nonnumberStringValue" value="90210a" scope="request"> lessThan </logic:lessThan> <logic:greaterThan name="nonnumberStringValue" value="90210a" scope="request"> greaterThan </logic:greaterThan> </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>  </td> <td> <logic:lessEqual name="numberIntValue" value="90210" scope="request"> lessEqual </logic:lessEqual> <logic:greaterEqual name="numberIntValue" value="90210" scope="request"> greaterEqual </logic:greaterEqual> </td> <td> <logic:lessEqual name="numberDoubleValue" value="90210" scope="request"> lessEqual </logic:lessEqual> <logic:greaterEqual name="numberDoubleValue" value="90210" scope="request"> greaterEqual </logic:greaterEqual>  </td> <td> <logic:lessEqual name="nonnumberStringValue" value="90210" scope="request"> lessEqual </logic:lessEqual> <logic:greaterEqual name="nonnumberStringValue" value="90210" scope="request"> greaterEqual </logic:greaterEqual>  </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>  </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>  </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>  </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>  </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> </td> <td> <logic:lessEqual name="numberIntValue" value="90210a" scope="request"> lessEqual </logic:lessEqual> <logic:greaterEqual name="numberIntValue" value="90210a" scope="request"> greaterEqual </logic:greaterEqual> </td> <td> <logic:lessEqual name="numberDoubleValue" value="90210a" scope="request"> lessEqual </logic:lessEqual> <logic:greaterEqual name="numberDoubleValue" value="90210a" scope="request"> greaterEqual </logic:greaterEqual> </td> <td> <logic:lessEqual name="nonnumberStringValue" value="90210a" scope="request"> lessEqual </logic:lessEqual> <logic:greaterEqual name="nonnumberStringValue" value="90210a" scope="request"> greaterEqual </logic:greaterEqual> </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.
All of these tags take their variable value from one of the following parameters:
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> TagsThe 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> TagsThe lessThan and greaterThan tags evaluate their contents if the variable is (respectively) less or greater than the constant. The <logic:lessEqual> and <logic:greaterEqual> TagsThese tags similar to the lessThan and greaterThan tags, but the tests here are less than or equal and greater than or equal. |