Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在JSP中使用for循环_Java_Html_Jsp_Loops_For Loop - Fatal编程技术网

Java 在JSP中使用for循环

Java 在JSP中使用for循环,java,html,jsp,loops,for-loop,Java,Html,Jsp,Loops,For Loop,我想循环遍历“节日”的ArrayList,并使用get方法获取它们的信息,打印出它的所有值。由于某种原因,当我使用这段代码时,它总是选择第0个值,而不是递增循环 如果我将这些值硬编码为“get(1)”,它将获得正确的值,因此我的问题显然是语法 <h1>All Festival Information</h1> <jsp:useBean id="allFestivals" type="java.util.ArrayList" scope="session" /

我想循环遍历“节日”的ArrayList,并使用get方法获取它们的信息,打印出它的所有值。由于某种原因,当我使用这段代码时,它总是选择第0个值,而不是递增循环

如果我将这些值硬编码为“get(1)”,它将获得正确的值,因此我的问题显然是语法

<h1>All Festival Information</h1>
    <jsp:useBean id="allFestivals" type="java.util.ArrayList" scope="session" />
    <table border="1">
        <tr>
            <td>Festival Name:</td>
            <td>Location:</td>
            <td>Start Date:</td>
            <td>End Date:</td>
            <td>URL:</td>
        </tr>
        <% for(int i = 0; i < allFestivals.size(); i+=1) { %>
            <tr>      
                <td>${allFestivals.get(i).getFestivalName()}</td>
                <td>${allFestivals.get(i).getLocation()}</td>
                <td>${allFestivals.get(i).getStartDate()}</td>
                <td>${allFestivals.get(i).getEndDate()}</td>
                <td>${allFestivals.get(i).getURL()}</td>  
            </tr>
        <% } %>
    </table> 
所有节日信息
节日名称:
地点:
开始日期:
结束日期:
网址:
${allFestival.get(i).getFestival()名称}
${allFestival.get(i).getLocation()}
${allfestival.get(i).getStartDate()}
${allFestival.get(i).getEndDate()}
${allfestival.get(i).getURL()}
执行此操作

    <% for(int i = 0; i < allFestivals.size(); i+=1) { %>
        <tr>      
            <td><%=allFestivals.get(i).getFestivalName()%></td>
        </tr>
    <% } %>


更好的方法是使用c:foreach-see-link,因为您将scriptlets
与其后继者EL
${}
混合在一起,所以导致了具体的问题。它们不共享相同的变量范围。
allfestival
在scriptlet范围内不可用,
i
在EL范围内不可用


你应该安装(不建议在JSP页面内编写Java代码。请改用JSTL核心标记
。只要停止使用
。您将自动被迫做正确的事情。另请参见OP的特殊情况,
所有节日
在scriptlet范围内不可用。工作正常。不确定为什么要我使用“”因为大多数在线信息都符合你的方法,使用“不客气。也许你依赖的是一个十多年前的资源。如果你想像我一样获得列表的索引,这里是:“我的大学。谈谈旧学校的教学。”“--我现在正在上这门课。现在是2016年,我们正在学习十多年前不好的技巧。令人惊讶的是,一些专业知识仍然存在。。。
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>