Applying OGNL expression to a context variable

989 Views Asked by At

I'm currently working with Struts2 (inexperienced developer) and I've been searching but I couldn't find how to apply an OGNL expression to a variable stored in context.

The thing is that I need to retrieve a parameter from Context and uppercase it. By now, I've tried to do it this way but sadly with no luck:

<s:property value="#myVar.toUpperCase()" />

As this works with variables stored in the ValueStack (notation without #), I don't really understand why this won't work with anything stored in Context..

I'm able to print the #myVarcontent just fine if I don't append the .toUpperCase() to it.

Also tried this workaround but didn't help:

<s:property value="<s:property value="#myVar"/>.toUpperCase()"/>

So what's the thing I'm missing? How can I apply an OGNL expression to a variable stored in Context?

1

There are 1 best solutions below

6
On BEST ANSWER

Your variable most probably isn't a String so there isn't toUpperCase() method in it. The solution is to call toString() before calling toUpperCase().

<s:property value="#myVar.toString().toUpperCase()" />

Update

Actually your problem is here <s:set var="myVar" value="%{#parameters.myVar}"/> since there could be more then one myVar in parameters it will return an array of Strings, so if you want just a single parameter change your expression to #parameters.myVar[0] and then toUpperCase() will work.

<s:set var="myVar" value="%{#parameters.myVar[0]}"/>
<s:property value="#myVar.toUpperCase()" />

OR

<s:set var="myVars" value="%{#parameters.myVar}"/>
<s:property value="#myVars[0].toUpperCase()" />