0

I had Struts2 code to show a button based on the result of a date comparison like this (note that this is a simplified example of what I actually have):

<s:iterator value="myList">
    <s:if test="%{getDate().compareTo(getNow()) > 0}">
        // Show the item
    </s:if>
</s:iterator>

I have since converted the display of the result set to use displaytag. How would I do the above using display tag. This is what I've tried.

<display:table name="myList" pagesize="50" id="row">
    <display:column title="Item">
        <s:if test="%{#attr.row.endDt.compareTo(#attr.row.now()) > 0}"><%--does NOT work--%>
    </display:column>
</display:table>

What is the correct syntax of the <s:if> statement?
Or can this not be done using displaytag + Struts2?

nmc
  • 8,724
  • 5
  • 36
  • 68
  • Given that you use `getDate().compareTo(getNow()) > 0` in the first, working expression, have you tried `#attr.row.getDate().compareTo(#attr.row.getNow()) > 0`? It seems more logical to me. – JB Nizet Dec 03 '12 at 22:42
  • @JBNizet I didn't even think of that! But, unfortunately, didn't quite work. – nmc Dec 04 '12 at 15:43

3 Answers3

0

Using the #attr.row as index to reference the element in the list should work:

<display:table name="myList" pagesize="50" id="row">
    <display:column title="Item">
        <s:if test="myList[%{#attr.row_rowNum - 1}].endDt.compareTo(myList[%{#attr.row_rowNum - 1}].now()) > 0}">
           <span>Let's rock</span>
        </s:if>
    </display:column>
</display:table>

Note: is getNow() a function coming from the iterated object ?

This seems strange to me, shouldn't it be a common function, from the baseAction from example ?

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Thanks for the answer but didn't quite work for me. `getnow` isn't actually a property of the bean but is coming from the Action class, from baseAction as you said. – nmc Dec 04 '12 at 15:44
0

Another way would be use a displaytag decorator.

 public String getLink()
 {
    YourObject yourObject = (YourObject) getCurrentRowObject();
    //Do your checks....
    return "<a href="#">your link</a>";

 }
Atropo
  • 12,231
  • 6
  • 49
  • 62
0

There are likely multiple solutions but, this ended up working for me:

<display:table name="myList" pagesize="50" id="row">
    <display:column title="Item">
        <s:set name="endDt" value="#attr.row.endDt"/>
        <s:set name="now" value="now" />
        <s:if test="%{#endDt.compareTo(#now) > 0}">
           <span>w00 h00!</span>
        </s:if>
    </display:column>
</display:table>
nmc
  • 8,724
  • 5
  • 36
  • 68