2015-08-17 39 views
0

這是我目前的情況。如果我像下面的代碼,它永遠不會顯示,因爲它沒有價值。顯示jsp的值

<% int z = 0; 
    int count = 3 
     do { %><% z++;} <c:if test="${not empty array[z]}"> 
         <td width="50%" height="15px">${array[z]}</td> 
        </c:if> while (z < count);%> 

我試着這樣的代碼(硬編碼,用0代替z),它會顯示記錄。

<% int z = 0; 
    int count = 3 
     do { %><% z++;} <c:if test="${not empty array[0]}"> 
         <td width="50%" height="15px">${array[0]}</td> 
        </c:if> while (0 < count);%> 

也許你們可以指導我如何讓它讀取z值,但不能讀取z值。對不起,我的英語不好。

+0

這是否甚至編譯?你將進入scriptlet模式並在那裏添加標籤,由於標籤不是Java代碼,所以不應該編譯。 – Thomas

回答

0

要麼使用C:對變量z設置或將其設置爲的pageContext

<% 
    int z = 0; 
    int count = 3 
    do { 
     z++; 
     pageContext.setAttribute("z", z); 
     %> 
     <c:if test="${not empty array[z]}"> 
     <td width="50%" height="15px">${array[z]}</td> 
     </c:if> 
     <%}while (z < count); 
%> 
+0

你的方式解決了我的問題,但現在面臨循環來到z = 1它得到空。我會檢查我的jsp非常感謝。 – FruitLai

0

假設你得到你的標籤正確嘗試做在Java代碼測試和渲染單元唯一的,如果這是真的:

<% 
int z = 0; 
int count = 3; 
do { 
    z++; 
    if(array[z] != null) { //you might have to do additional checks here 
    %> 
    <td width="50%" height="15px"><%=array[z]%></td> 
    <% 
    } 
} 
while (z < count); 
%> 

或者你可以嘗試使用${not empty array[<%=z%>]}等,但我不知道因爲我們很少使用scriptlet。

作爲第二種選擇嘗試使用<c:forEach>這是更清潔IMHO:

<c:forEach var="z" begin="1" end="${count}"> 
    <c:if test="${not empty array[z]}"> 
     <td width="50%" height="15px">${array[z]}</td> 
    </c:if> 
</c:forEach> 
+0

我試着用$ {not empty array [<%=z%>]}和$ {not empty array [<=z%>]}發佈這個......兩個都不行..... – FruitLai

+0

@FruitLai嗯,這就是我不知道'確定。請看看我發佈的代碼的第二個替代方案,因爲這應該會更容易理解,並使代碼更簡潔(如果它符合您的需求)。 – Thomas

0

嘗試以下方法:

<% int z = 0; 
    int count = 3 
    do { 
     if (!array.isEmpty()) { 
     %> 
     <td width="50%" height="15px"><%=array[z]%></td> 
<%while(z < count)%>