How to iterate Set elements using Struts 2 tags

前端 未结 3 1378
小蘑菇
小蘑菇 2021-01-14 13:56

I have two Sets

set1  contains(\"A\",\"B\",\"C\",\"D\") elements
set2  contains(\"E\",\"F\",\"D\",\"G\") elements

I want to co

相关标签:
3条回答
  • 2021-01-14 14:13

    Rearranged to better list the several ways:

    1. With two Iterators:

      <s:iterator value="setOne" var="first" status="ctr1">
          <s:iterator value="setTwo" var="second" status="ctr2">
              <s:if test="%{#first == #second}">
                  Common value detected : [<s:property value="%{#first}"/>]
                  <br/> setOne position : [<s:property value="%{#ctr1.index}"/>] 
                  <br/> setTwo position : [<s:property value="%{#ctr2.index}"/>]
                  <br/>
              </s:if>
          </s:iterator>
      </s:iterator>
      
    2. With a single Iterator and Set.contains(Object):

      <s:iterator value="setOne" var="first" status="ctr1">
          <s:if test="%{setTwo.contains('#first')}">
              Common value detected : [<s:property value="%{#first}"/>] <br/>
          </s:if>
      </s:iterator>
      
    3. With a single Iterator and pure OGNL:

      <s:iterator value="setOne" var="first" status="ctr1">
          <s:if test="%{#first in setTwo}">
              Common value detected : [<s:property value="%{#first}"/>] <br/>
          </s:if>
      </s:iterator>
      
    4. Without Iterators, using OGNL List Selection only (thanks to @AleksandrM for the code):

      <s:property value="setOne.{? #this in setTwo}"/>
      
    0 讨论(0)
  • 2021-01-14 14:21

    Try the code below:

    <s:iterator var="val1" value="set1">
     <s:iterator var="val2" value="set2">
      <s:if test="#val1 == #val2">
       <s:property /><br>
      </s:if>
     </s:iterator>
    </s:iterator>
    
    0 讨论(0)
  • 2021-01-14 14:22

    Try this.i think it will work for you. and if not then do some trial & error on this.

    <s:iterator value="set1" var="first">
      <s:iterator value="set2" var="second">
    
       <s:set name="valueofset1" value="%{#set1.index}" />  
       <s:set name="valueOfSet2" value="%{#set2.index}" />  
    
         <s:if test="valueofset1 == valueOfSet2">
          //Do what ever you want
         </s:if>
    
      </s:iterator>
    </s:iterator>
    
    0 讨论(0)
提交回复
热议问题