Question

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'?

Was it helpful?

Solution

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.

OTHER TIPS

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>
<s:set name="prop" value="%{'val'}"/>

<s:if test="%{#prop == 'val'}">
  <s:property value="%{#prop}" />
</s:if>

<s:elseif test="%{#prop=='val1'}">
  <s:property value="%{#prop}" />
</s:elseif>

<s:else>
  ...
</s:else>

It is as simple as:

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

Just need to replace single quotes with double quotes and vice-versa

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top