Struts Logic Tags


The frequently used tags in the Logic tag library are for logical comparison between values and for iteration over collection. The important logical comparison tags are: equal , notEqual , greaterEqual , greaterThan , lessEqual and lessThan . The following are the important attributes are common to these tags.

  • value ‚ The constant value against which comparison is made.

  • name ‚ The name of the bean in one of the 4 scopes

  • property ‚ The property of the bean that is compred with the value.

A sample usage of the tags is as follows :

 <logic:equal name=customer property=firstName value=John>    //do whatever when the customer first name is John </logic:equal> 

The above tag searches for a bean named customer in the 4 scopes and checks if its firstName property is equal to John. You can also specify the scope to restrict the search scope by using the scope attribute on these tags.

Another tag attribute is parameter . You have to specify only one: parameter or ( name and property ). As the name suggests, the parameter attribute looks for the specified request parameter and compares it with the value attribute. In the example below, the request parameter named firstName is compared with a value of John .

 <logic:equal parameter=firstName value=John>    //do whatever when the request parameter firstName     //is equal to John </logic:equal> 

There are more comparison tags of interest: empty , notEmpty , present and notPresent . These tags do not compare against a given value, but check if an entity (bean property or request parameter) is empty or present respectively. Hence they don ‚ t need the value attribute. The following snippet checks if a request parameter named firstName is present.

 <logic:present parameter=firstName > //do whatever when the request parameter firstName is present  //(Irrespective of its value) </logic:equal> 

Nested Logic Tags

Consider how you would write the following logical condition using Logic tags:

 if (customer.firstName == John && customer.lastName == Doe       && customer.age == 28)  {    do something.. } 

This can be done by nesting the logic:equal tags as follows:

 <logic:equal name=customer property=firstName value=John>    <logic:equal name=customer property=lastName value=Doe>       <logic:equal name=customer property=age value=28>          //do something.       </logic:equal>    </logic:equal> </logic:equal> 

Nesting of < logic:xxx > tags always results in logical ANDing. There is no convenient way to do an ‚“OR ‚½ test however; that's where the expression language in JSTL comes in handy (introduced in next section). With JSTL, the above AND condition can be written as follows:

 <c:if test=${customer.firstName == John &&                customer.lastName == Doe && customer.age == 28}>       //do something ... </c:if> 

Writing the OR condition is also no different

 <c:if test=${customer.firstName == John                 customer.lastName == Doe  customer.age == 28}>       //do something ... </c:if> 

The c in the c:if stands for JSTL ‚ s core tag library TLD. There are other tag libraries in JSTL such as formatting. Refer to the section ‚“ A crash course on JSTL ‚½ for details.

Iterate Tag

The iterate tag is used to iterate over a collection (or a bean containing collection) in any of the four scopes (page, request, session and application) and execute the body content for every element in the collection. For instance, the following tag iterates over the collection named customers .

 <logic:iterate name=customers>    //execute for every element in the collection </logic:iterate> 

Another alternative is to use a bean and iterate over its property identified by the attribute property . The following tag accesses the company bean from one of the scope and then invokes getCustomers() on it to retrieves a collection and iterates over it.

 <logic:iterate name="company" property="customers">    // Execute for every element in the customers     // collection in company bean </logic:iterate> 

Most of the times a collection is iterated over to display the contents of that collection, perhaps in the table format. This requires the individual element of the collection is exposed as a scripting variable to the inner tags and scriptlets. This is done using the id attribute as follows:

 <logic:iterate  id="customer"  name="company" name="customers">    // Execute for every element in the customers    // collection in company bean.    // Use the scripting variable named customer    <bean:write name="  customer  " property="firstName" /> </logic:iterate> 
Note ‚  

The JSTL tag < c:forEach > performs similar functionality. It is recommended that you switch to these new tags where applicable .




Struts Survival Guide. Basics to Best Practices
Struts Survival Guide: Basics to Best Practices (J2ee Survival Series)
ISBN: 0974848808
EAN: 2147483647
Year: 2004
Pages: 96

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