4

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>
...
informatik01
  • 16,038
  • 10
  • 74
  • 104
Jothi
  • 14,720
  • 22
  • 68
  • 93

1 Answers1

7

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:

Dave Newton
  • 158,873
  • 26
  • 254
  • 302
Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204