Domanda

Ho un oggetto chiamato 'item'. ha una proprietà chiamata 'tipo'

quando lo faccio:

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

ottengo questo: Q

ok, quindi posso leggere il valore, ma quando provo questo:

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

Ricevo una stringa vuota

questo mi dà una stringa vuota:

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

Ho anche provato questo:

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

ma ho questa stringa: false

come posso ottenere "vero"?

È stato utile?

Soluzione

ok, ho lottato per sempre con questo ... e alla fine ho scoperto come farlo. & Quot; Q " è una stringa letterale rispetto a "Q" che è un carattere. quindi ...

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

valuterà sempre come falso. invece prova ...

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

L'ho visto anche fatto in questo modo ...

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

(notare le virgolette singole attorno al valore dell'attributo test.)

OGNL è una spina nel fianco. Immagino che l'OP lo abbia già capito. ma spero che questo aiuti qualcun altro.

Altri suggerimenti

Non credo che tu possa inserire alcun EL all'interno della proprietà value di questo tag. Potresti farlo con qualcosa del tipo:

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

Altri esempi sono qui

Nota: utilizzo il freemarker per la vista, quindi quanto sopra potrebbe non essere esatto. Nel freemarker potresti farlo con un compito:

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

Oppure puoi anche usare il costrutto if come sopra:

<#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>

È semplice come:

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

Devo solo sostituire le virgolette singole con virgolette doppie e viceversa

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top