Search code examples
jspstruts2tagsstring-comparisonognl

how to compare list elements(type string) and string(in request scope) using struts 2 tags


My List contains("A","B","C","D") elements

<s:iterator value="lis">
  <s:property /><br>
</s:iterator>

and String str="A";

<s:property value="%{#request.str}"/>

I want to compare every element of list(lis) with String s.


Solution

  • With the IteratorStatus object:

    <s:iterator value="lis" status="ctr">
        <s:property /> 
        <s:if test="%{#request.str.equals(lis[#ctr.index])}">
            -> This value from "lis" is equal to the value of "str"
        </s:if>
        <br/>
    </s:iterator>
    

    With the var parameter:

    <s:iterator value="lis" var="currentValue">
        <s:property /> 
        <s:if test="%{#request.str.equals(#currentValue)}">
            -> This value from "lis" is equal to the value of "str"
        </s:if>
        <br/>
    </s:iterator>
    

    With the top keyword:

    <s:iterator value="lis">
        <s:property /> 
        <s:if test="%{#request.str.equals(top)}">
            -> This value from "lis" is equal to the value of "str"
        </s:if>
        <br/>
    </s:iterator>
    

    You may wanna read the short OGNL Language Guide for more details.