Struts2, gli struts-tags, il tag <s:if> e il test su un singolo carattere

Si parte dall’errore:

WARNING: Caught an exception while evaluating expression 'valore=='P'' against value stack
java.lang.NumberFormatException: For input string: "P"
	at sun.misc.FloatingDecimal.readJavaFormatString(Unknown Source)

che viene generato quando, all’interno di una jsp si cerca di testare il valore di una proprietà, in questo modo:

<s:if test="valore == 'P'">
   ...
</s:if>

Lo stesso errore non si verifica se il controllo non viene fatto su un singolo carattere ma su più di uno:

<s:if test="valore == 'Pippo'">
   ...
</s:if>

Il motivo dell’errore è l’utilizzo dei singoli apici nel casi di un singolo carattere; in questo caso occorre utilizzare il double quote:

<s:if test="valore == /"P/"">
   ...
</s:if>

In questo modo il problema è risolto.

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>