...">

How to evaluate struts 2 s: if?

I have an object called "item". it has a type property

when i do this:

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

I understand: Question

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

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

I get an empty string

this gives 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 line: false

how can i get "true"?

+5
source share
4 answers

ok, I struggled with this forever ... and in the end I learned how to do it. "Q" is a string literal compared to "Q", which is a character. so...

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

will always be evaluated as false. try instead ...

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

I also saw how this is done ...

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

(note the single quotes around the value of the test attribute.)

OGNL - . , . , , - .

+14

, EL value . - :

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

:

. freemarker , . freemarker :

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

if, :

<#if (item.type == 'Q')>true<#else>false</#if>
+2
<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>
0
source

It is as simple as:

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

You just need to replace single quotes with double quotes and vice versa

0
source

All Articles