在JSP中使用for循环 [英] Using for loop inside of a JSP

查看:456
本文介绍了在JSP中使用for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想循环访问Festivals的一个 ArrayList ,并通过 get 方法获取他们的信息,并打印出所有的值。由于某些原因,当我使用这个代码时,它会一直选择0值,而不是递增循环。

如果我将这些值硬编码为get 1)它会得到正确的值,所以我的问题显然是语法。

 < h1>所有节日信息< / H1> 
< jsp:useBean id =allFestivalstype =java.util.ArrayListscope =session/>
< table border =1>
< tr>
< td>节日名称:< / td>
< td>位置:< / td>
< td>开始日期:< / td>
< td>结束日期:< / td>
< td>网址:< / 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>


解决方案

a href =https://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202>气馁和老派 scriptlets < %%> 及其后续EL $ {} 。他们不共享相同的变量范围。 scriptlet 范围内 allFestivals 不可用,并且EL范围内 i 不可用。



您应该安装 JSTL (< - click

 <%@ taglib prefix =curi = http://java.sun.com/jsp/jstl/core %> 

然后遍历列表如下:

< c:forEach items =$ {allFestivals}var =festival>
< tr>
< td> $ {festival.festivalName}< / td>
< td> $ {festival.location}< / td>
< td> $ {festival.startDate}< / td>
< td> $ {festival.endDate}< / td>
< td> $ {festival.URL}< / td>
< / tr>
< / c:forEach>

(注意可能的 XSS攻击漏洞,相应地使用< c:out>



不要忘记删除< jsp:useBean> ,因为它没有任何价值在这里,当你使用一个servlet作为模型和视图控制器。这只会导致混乱。另请参阅我们的servlets wiki页面。此外,你可以通过 web.xml 中的以下条目来禁用 scriptlets ,以免意外使用它们:

< jsp-config>
< jsp-property-group>
< url-pattern> *。jsp< / url-pattern>
< / scripting-invalid> true< / scripting-invalid>
< / jsp-property-group>
< / jsp-config>


I want to loop through an ArrayList of "Festivals" and get their information with get methods, printing out all its values. For some reason when I use this code, it will always choose the "0"th value and not increment the loop.

If I hard code the values as "get(1)" it will get the correct values so my issue is clearly with the syntax.

<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> 

解决方案

You concrete problem is caused because you're mixing discouraged and old school scriptlets <% %> with its successor EL ${}. They do not share the same variable scope. The allFestivals is not available in scriptlet scope and the i is not available in EL scope.

You should install JSTL (<-- click the link for instructions) and declare it in top of JSP as follows:

<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

and then iterate over the list as follows:

<c:forEach items="${allFestivals}" var="festival">
    <tr>      
        <td>${festival.festivalName}</td>
        <td>${festival.location}</td>
        <td>${festival.startDate}</td>
        <td>${festival.endDate}</td>
        <td>${festival.URL}</td>  
    </tr>
</c:forEach>

(beware of possible XSS attack holes, use <c:out> accordingly)

Don't forget to remove the <jsp:useBean> as it has no utter value here when you're using a servlet as model-and-view controller. It would only lead to confusion. See also our servlets wiki page. Further you would do yourself a favour to disable scriptlets by the following entry in web.xml so that you won't accidently use them:

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <scripting-invalid>true</scripting-invalid>
    </jsp-property-group>
</jsp-config>

这篇关于在JSP中使用for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆