How to check if List contains the particular element or not in Struts2?

Jothi picture Jothi · Dec 28, 2011 · Viewed 8k times · Source

I have tried to check if the List contains particular element or not using Struts 2 in <s:if> tag?

<display:table class="noheader-border" id="data" name="lstAttendance" sort="list"
               uid="row" htmlId="sources" export="false">
    <display:column style="width:150px">
        <s:property value="%{#attr.row.worker.workerName}" />
    </display:column>
    <display:column style="width:10px;weight:bold;">:</display:column>
    <s:if test="lstSalaryDefinedWorkerId.contains(%{#attr.row.workerId})">
        ...
    </s:if>
...

Answer

Umesh Awasthi picture Umesh Awasthi · Dec 28, 2011

I guess you can do it using OGNL syntax. For example I have a list and a property in my action class like:

private List test;
private String p = "a1";
// And their getters and setters

public String execute() throws Exception {
    test = new ArrayList();
    test.add("a");
    test.add("b");
    test.add("c");
    test.add("d");
    test.add("e");
    return SUCCESS;
}

All I need to do following in my JSP page

<s:if test="%{p in test}">
  Inside If block
</s:if>
<s:else>
  Inside Else block
</s:else>

For details refer to the Struts 2 OGNL documentation: