Using the Struts Logic Tags to Test for Absence or Presence of Values


This section provides information about the following tag:

  • <logic:empty> ” Evaluates its contents if the variable is null or an empty string

  • <logic:messagesPresent> ” Evaluates its contents if the message specified is available as an error or message

  • <logic:messagesNotPresent> ” Evaluates its contents if the message specified isn't available as an error or message

  • <logic:notEmpty> ” Evaluates its contents if the variable isn't null or an empty string

  • <logic:notPresent> ” Checks for the presence of the specified role, user , cookie, header, or bean and evaluates its contents if not found

  • <logic:present> ” Checks for the presence of the specified role, user, cookie, header, or bean and evaluates its contents if found

This section uses the Logic Presence page at /StrutsTaglibs/LogicPresence.do . The rendered page is shown in Figure 14.5.

Figure 14.5. Logic Presence page at /StrutsTaglibs/LogicPresence.do .

graphics/14fig05.jpg

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

Listing 14.4 JSP File Demonstrating Use of Struts Logic Tags Testing for the Presence of Values ( LogicPresence.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" %> <%@ page import="java.util.HashMap" %> <%@ page import="org.apache.struts.action.Action" %> <%@ page import="org.apache.struts.action.ActionError" %> <%@ page import="org.apache.struts.action.ActionErrors" %> <html:html> <head> <title>Logic Presence sample code</title> </head> <body bgcolor="white"> <h3>Logic Presence sample code</h3> <p>This page provides examples of the following Struts LOGIC tags:<br> <ul> <li>&lt;logic:empty&gt;</li> <li>&lt;logic:messagesPresent&gt;</li> <li>&lt;logic:messagesNotPresent&gt;</li> <li>&lt;logic:notEmpty&gt;</li> <li>&lt;logic:notPresent&gt;</li> <li>&lt;logic:present&gt;</li> </ul> <%-- Variables used on this page --%> <% request.setAttribute("favoriteTeam", "RedSox"); ActionErrors errors = new ActionErrors(); errors.add("totallylost", new ActionError("application.totally.lost")); request.setAttribute(Action.ERROR_KEY, errors); request.setAttribute("goodThingsAboutTheYankees", ""); %> <%-- The following section shows empty and notEmpty. --%> <logic:empty name="goodThingsAboutTheYankees">    You must be a Red Sox fan!<P> </logic:empty> <logic:notEmpty name="goodThingsAboutTheYankees">    New Yorker, eh? </logic:notEmpty> <P> <%-- The following section shows present and notPresent. --%> <logic:present name="noSuchAttribute" property="noSuchProperty">    Wow, someone defined it? </logic:present> <logic:notPresent name="noSuchAttribute" property="noSuchProperty">    Much more likely! </logic:notPresent> <P> <logic:notPresent name="request" property="noSuchProperty">    Request doesn't have such a property... </logic:notPresent> <P> <logic:present header="user-agent">    Yep, we got a user-agent header. </logic:present> <logic:notPresent header="user-agent">    Odd, user-agent is a standard header property. </logic:notPresent> <P> <%-- The following section shows messagesPresent and messagesNotPresent. --%> <logic:messagesPresent property="noSuchMessage">    Should never see this. </logic:messagesPresent> <logic:messagesNotPresent property="noSuchMessage">    You should see this instead. </logic:messagesNotPresent> <P> <logic:messagesPresent>    Yes, there are errors. </logic:messagesPresent><P> <logic:messagesPresent property="totallylost">    We're totally lost, so we should see this. </logic:messagesPresent> <logic:messagesNotPresent property="totallylost">       We're totally lost, so we shouldn't see this. </logic:messagesNotPresent> </body> </html:html> 

The logic:empty and logic:notEmpty Tags

If you want to check a bean or bean property to see whether it's null or a blank string or, in the case of a collection, whether the size of the collection is zero, you can use the empty and notEmpty tags.

The only interesting thing to note is that you can't really test for null on a bean itself because getAttribute returns null for both not present and null value, both of which cause an exception to be thrown. So, testing for null values is really useful only on properties of beans.

The logic:present and logic:notPresent Tags

If you want to check to see whether a bean, header, cookie, role, user, or parameter exists, you use present and notPresent . These tags evaluate based on the simple presence (or lack) of the specified value. In the case of a role or user, these tags evaluate whether the role or username is equal to the specified value.

The logic:messagesPresent and logic:messagesNotPresent Tags

The <html:errors> tag enables you to print out errors from an Action , but the messagesPresent tag enables you to check for their existence.

With no attributes, the messagesPresent tag evaluates (or doesn't evaluate) its contents if there are any ActionError s in the current request scope. With a property attribute, messagesPresent checks for the existence of an error message for that specific property.

If the messages attribute is set to true, the tag checks the Action.MESSAGE_KEY bean value (where messages ”as opposed to errors ”are stored) instead of the normal Action.ERROR_KEY bean value. Alternatively, the name attribute enables you to explicitly specify the name of the bean.



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