Search code examples
struts2strutsstruts-1

How to concatenate 2 action class variables in Struts 2?


I have 2 variable in my action class, id1 and id2. Joined by a _, they're used as a map key.

I am not able to retrieve the map value using this code:

<s:property value="%{mymap[id1_id2]}" /> 

How should I retrieve the map value?


Solution

  • The expression id1_id2 in OGNL will assume the presence of a variable named id1_id2, since it's a perfectly legal identifier.

    If you want to concatenate strings, you'd need:

    <s:property value="%{mymap[id1 + '_' + id2]}" />
    

    I'd likely create a separate variable to use as the key:

    <s:set var="mapKey" value="%{id1 + '_' + id2}" />
    <s:property value="%{mymap[#mapKey]}" />
    

    Or more likely, I'd do it somewhere besides the view layer.