Applying an OGNL Expression to a Context Variable

I am currently working with Struts2 (inexperienced developer) and I have searched but could not find how to apply an OGNL expression to a variable stored in a context.

The thing is, I need to get the parameter from the context and capitalize on it. So far, I've tried to do it this way, but unfortunately no luck:

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

      

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

I can print the content #myVar

just fine if I don't add to it .toUpperCase()

.

Also tried this workaround but didn't help:

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

      

So what am I missing? How do I apply an OGNL expression to a variable stored in a Context?

Many thanks

+3


source to share


1 answer


Your variable is most likely not a string, so there is no method in it toUpperCase()

. 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 can be more than one in the parameters myVar

, it will return an array of strings, so if you only want one parameter change your expression to #parameters.myVar[0]

and then it 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()" />

      

+1


source







All Articles