java - Using for loop inside of a JSP -
i want loop through arraylist of "festivals" , information get methods, printing out values. reason when use code, choose "0"th value , not increment loop.
if hard code values "get(1)" correct values issue 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 = 0; < 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 caused because you're mixing discouraged , old school scriptlets <% %>
successor el ${}
. not share same variable scope. allfestivals
not available in scriptlet scope , i
not available in el scope.
you should install jstl (<-- click link instructions) , declare in top of jsp follows:
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
and iterate on list 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 remove <jsp:usebean>
has no utter value here when you're using servlet model-and-view controller. lead confusion. see our servlets wiki page. further favour disable scriptlets following entry in web.xml
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>
Comments
Post a Comment