2011-06-06 116 views
1

我需要使用JSTL選擇語句來實現一個switch case,我有不同的選擇樹。 任何人都知道下面的代碼不起作用的原因?JSTL c:選擇問題

在此先感謝。

<c:choose> 
    <c:when test="${iUserInfo.identification_card_type}==0"> 
     <option selected="selected">Carta di Identità</option> 
     <option>Passaporto</option> 
     <option>Patente di Guida</option> 
    </c:when> 
    <c:when test="${iUserInfo.identification_card_type}==1"> 
     <option>Carta di Identità</option> 
     <option selected="selected">Passaporto</option> 
     <option>Patente di Guida</option> 
    </c:when> 
    <c:when test="${iUserInfo.identification_card_type}==2"> 
     <option>Carta di Identità</option> 
     <option>Passaporto</option> 
     <option selected="selected">Patente di Guida</option> 
    </c:when> 
    <c:otherwise> 
     <option>Scegli...</option> 
     <option>Carta di Identità</option> 
     <option>Passaporto</option> 
     <option>Patente di Guida</option> 
    </c:otherwise> 
</c:choose> 
+0

如果打印出'$ {iUserInfo.identification_card_type}'這個表達式,你會得到一個結果嗎?據我所知,'== 1'應該足夠了。 – reporter 2011-06-06 12:36:31

+0

我試過了,它仍然不起作用,我認爲這是不夠的.. :( – Kevin 2011-06-06 12:43:37

回答

5

因爲你沒有評估內部${}整個表達。因此解決這個問題:

<c:when test="${iUserInfo.identification_card_type == 0}"> 
... 
<c:when test="${iUserInfo.identification_card_type == 1}"> 
... 
<c:when test="${iUserInfo.identification_card_type == 2}"> 
+0

謝謝,我修改它,因爲你建議,但它沒有工作 – Kevin 2011-06-06 12:42:41

+0

然後它不會返回一個'int'在如果它實際返回'String',那麼你需要相應地修正它,以便它返回'int';或者如果你不能由於某些(愚蠢的)商業限制而改變'0','1', '2'等表達爲'0','1','2'等 – BalusC 2011-06-06 12:44:37

+0

iUserInfo.identification_card_type是一個很長的值,我也用'1'和'2',' 3'但它仍然不起作用 – Kevin 2011-06-06 12:50:48

0

的Ciao,

我通常使用這種測試:

test="${iUserInfo.identification_card_type eq 0}" 

而不是正常的比較,你應該使用JSTL的那些,像GT(大於) st(小於)和eq,即等於。 繼續前進,jstl和servlet他們搖滾!