2015-08-17 30 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)%>