Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to evaluate struts 2 s:if?

Tags:

struts2

i have an object called 'item'. it has a property called 'type'

when i do this:

<s:property value="item.type" />

i get this: Q

ok, so i can read the value, but when i try this:

<s:property value="item.type == 'Q'" /> 

i get an empty string

this give me an empty string:

<s:property value="%{#item.type == 'Q'}" />

i even tried this:

<s:property value="item.type.equals('Q')" />

but i got this string: false

how do i get 'true'?

like image 745
Titi Wangsa bin Damhore Avatar asked Jul 30 '09 13:07

Titi Wangsa bin Damhore


2 Answers

ok, i struggled with this for ever... and eventually found out how to do this. "Q" is a string literal versus 'Q' which is a character. therefore...

<s:if test="%{#item.type == 'Q'}">

will always evaluate to false. instead try...

<s:if test="%{#item.type == \"Q\"}">

i've also seen it done this way...

<s:if test='%{#item.type == ("Q")}'>

(notice the single quotes around the test attribute value.)

OGNL is a thorn in my side. i'm guessing the OP has already figured this out. but hopefully this helps somebody else.

like image 121
Josh Fuson Avatar answered Nov 09 '22 21:11

Josh Fuson


I don't think you can put any EL inside the value property of this tag. You could accomplish that with something like:

<s:if test="%{#item.type == 'Q'}">
  true
</s:if>
<s:else>
  false
</s:else>

More examples of this are here

Note - I use freemarker for the view, so the above may not be exact. In freemarker you could do this with an assign:

<#assign val = (item.type == 'Q')/>
${val}

Or, you could also use the if construct like above:

<#if (item.type == 'Q')>true<#else>false</#if>
like image 22
Brian Yarger Avatar answered Nov 09 '22 23:11

Brian Yarger