-1

I have a hashmap in an action class. I am trying to check if a key is not present to display a particular text on screen. However, it is not working. below is the code:

<s:elseif test="%{#map[getCurrentYear()]==null && #map.key.equalsIgnoreCase(cdKey)}">
    <h4>
        2
        <s:text name="properties.msg.header" />
        <s:property value="currentYear"      />
    </h4>
</s:elseif>

I am not able to get what is wrong with the code.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
JJN
  • 69
  • 1
  • 9
  • What is `map`? What is `getCurrentYear()`? What is `#map.key`? What is `cdKey`? Show your code. – Aleksandr M Jul 26 '16 at 19:25
  • @Andrea I have a field defined in Action class with getter and setter. private String cdKey='cdKey' and private String cyQKey ='cyQKey' . I have one Hashmap - map which is storing these keys. I am trying to check if cyQKey is not present in the map and if cdKey is present display a message.

    – JJN Jul 29 '16 at 19:50

2 Answers2

0

If map is an action attribute with a getMap() getter, you don't have to use #; also getCurrentYear() becomes currentYear in OGNL.

This

<s:elseif test="%{map[currentYear]==null && map.key.equalsIgnoreCase('cdKey')}">

should be enough, but note that this would be way more simple (and elegant) if done serverside, in a

public boolean isValueEqualToCdKey(){ ... } 

method, with

<s:elseif test="isValueEqualToCdKey" > 
Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
  • Hi, The above solution didn't work for me. I have the map with getter and setter methods private Map> map=new LinkedHashMap>(); removing #didnt work – JJN Jul 26 '16 at 14:50
  • First of all, separate the tests. Put (before the s:if / s:else) an s:property with the year test, and another with the key equality test, and see which one fails – Andrea Ligios Jul 26 '16 at 15:24
  • I added s:if and else if as mentionsed but it didnt work AA Abc ABCDE – JJN Jul 26 '16 at 15:51
  • So none of the tests work. Post more code (cdKey, ecc) or try doing it serverside that is safer and more elegant – Andrea Ligios Jul 26 '16 at 16:01
0

If List> map=new LinkedHashMap>(); is called in jsp like

<s:property value="#map.[#key]"/>
Ravi Thapa
  • 79
  • 1
  • 7