Wednesday, April 1, 2009

Struts 2 OGNL variable evaluation

< s: set name="startIndex" value="%{#parameters['start'][0]}"/ > < s: property value="@java.lang.Integer@parseInt(#startIndex)+4"/ >
Here the first statement grab the query string value using parameter object and assign it as a string, the '%{}' syntax is used for this purpose. The second statement convert the string to int and add the two integers.
IMPORTANT: for the parseInt to work you should enable the constant in struts.xml < constant name="struts.ognl.allowStaticMethodAccess" value="true" / >

4 Comments:

Don't Trust Anyone said...

dont use #

Anonymous said...

Thanks a ton for this trick. I was struggling with this. I tried several ways to convert string to integer but none had worked. Thnx.!!
- Jugz!

Anonymous said...

Thanks, You save the whole world. By the way, where did you learn this, I read some books, they never and ever tell me.

Unknown said...

You are just genious I wasted 24 hours in this small issue


thanks a lot