2009-05-15 10 views
55

E.g.Come posso verificare se un attributo è impostato (non nullo e non una stringa vuota) con jstl?

<c:if test="${post}"> 
    <h3>${post.title}</h3> 
</c:if> 
+1

anche se 'title' non esiste (. cioè la proprietà non appartiene a questa variabile/fagioli), si vuole prendere il' javax.el.PropertyNotFoundException', vedere la domanda ' L'attributo di controllo esiste in JSP' su http://stackoverflow.com/questions/2522562/checking-attribute-exists-in-jsp –

+0

La tua domanda dovrebbe probabilmente essere riformulata come "verifica se un attributo è ** set **" (non null e non una stringa vuota) –

risposta

103

Utilizzare la parola vuota

<c:if test="${not empty post}"> 
    <h3>${post.title}</h3> 
</c:if> 
+2

Questo è sicuramente il modo migliore. Credo che controlli anche per 'null'. –

+0

Questo è fuorviante. Se l'attributo è un elenco, è impostato ed è vuoto, questo condizionale restituisce true. – stepanian

10

È anche possibile utilizzare '!' invece 'non e':

<c:if test="${!empty post}"> 
    <h3>${post.title}</h3> 
</c:if> 
Problemi correlati