Search code examples
struts2ognl

struts2 ognl retrieve data from session (nested property tags?)


I have a situation where I would like to retrieve data from the session on a jsp using OGNL. The data in my session is stored like this:

/data/abc/-Name (key) -> ABC Inc. (value)

I can easily retrieve this from the session by doing

<s:property value="#session['/data/abc/-Name']"/> 

But unfortunately, the string '/data/abc/' is a dynamic one and is stored in my action under the variable companyFolder.

How do I use this variable to get the data from the session.. something like

<s:property value="#session['%{companyFolder}-Name']"/> // this didnt work
<s:property value="#session['<s:property value="%{companyFolder}"/>-Name']"/> // this didnt work

Solution

  • <s:property value="#session[companyFolder + '-Name']" />
    

    I would likely do this in the action, though, using SessionAware. This makes things easier to test, and avoids executing the JSP to see if things work.