Displaying an ArrayList on a JSP Page via JSTL
Can someone please help me figure out what I'm doing wrong? I have a class with a method that returns an ArrayList and I can't get it to display on my JSP page. Here's my code:
//java class
public class Confirmation {
ArrayList<Criterion> criterion = null;
ArrayList<String> criterionTitles = null;
EvaluationDefinition eval = null;
public ArrayList<String> getCriterionTitles() {
criterion = new ArrayList<Criterion>();
criterionTitles = new ArrayList<String>();
for(int i = 0; i < eval.getGroups().get(0).getCriterionCount(); i++ )
{
criterion.add(eval.getGroups().get(0).getCriterion().get(i));
criterionTitles.add(i, criterion.get(i).getTitle());
}
return criterionTitles;
}
}
//jsp page code...
<jsp:useBean id="criterionTitles" scope="page" class="Confirmation" />
// ERROR MSG HERE: Error reading 'criterionTitles' on type 开发者_C百科Confirmation
<c:forEach var="title" items="${criterionTitles.criterionTitles}">
<c:out value="${title}" />
</c:forEach>
if I run the for loop logic in a servlet and just out.println(criterion.get(i).getTitle() it prints out the titles just fine. It's just when I run the c:foreach loop that I get errors. Thank you.
Error reading 'criterionTitles' on type Confirmation
Calling getCriterionTitles()
has thrown an exception. My bet on an NullPointerException
because eval
is null
. Read the server logs for the full stacktrace, trackback the root cause in the code and fix it accordingly.
Unrelated to the concrete problem, putting classes in the default package is a bad idea. It will only work that way on specific servletcontainer/JVM configurations, but not all. In a normal Java class inside a package it's namely impossible to import
classes from the default package. Some servletcontainers have builtin workarounds for this in order to be "noob-friendly". But you shouldn't rely on that.
精彩评论