Search code examples
javastruts2ognl

Conditional display with OGNl Expressions


In My struts project i have set of records in an array list,these recors have its name and categoryid. and in second arraylist i have records for category(category_id, categoryname).

now i want to list the first list with the category name as subheadings like

ArrayList1:(nameDetailList)
NAME:               CATEGORYID
name1                    1
name2                    2
name3                    1
name4                    5

ArrayList2:(categoryList)
CategoryID          CategoryName
1                     Category-1
2                     Category-2 
3                     Category-3 
4                     Category-4 
5                     Category-5

I need these to be displayed as

Category-1 
    --name1
    --name3
Category-2 
    --name2 
Category-5 
    --name3 

Note: Here i don't want to display the category names that don't have records associated for that. for this i coded below.

<s:iterator id="catIter"  value="categoryList">
       <s:property value="categoryName"/>
  <s:iterator value="nameDetailList.{ ?this.categoryId==#catIter.categoryId}">
 <s:property value="Name"/>
</s:iterator>
 </s:iterator>

it displaying the categories those don't have records asssociated with those also.can anyone tell how to control the display of category name. or is there any other better alternatives for this.


Solution

  • i have updated my codes to

    <s:iterator id="catIter"  value="categoryList" status="catstatu">      
      <s:iterator value="nameDetailList.{ ?this.categoryId==#catIter.categoryId}">
         <s:if test="#catstatus.first==true">
              <s:property value="categoryName"/>
         </s:if>
        <s:property value="Name"/>
      </s:iterator>
    </s:iterator>
    

    Now i'm getting the desired output. but is solution is the best one??