У меня есть JSP, который отображается при ошибках проверки в моем приложении. В Servlet я установил ArrayList<String>
ошибок в запросе и пытаюсь распечатать их в JSP со следующим кодом. Я знаю, что есть 1 ошибка в ArrayList
, потому что я печатаю ее на консоль сервера, но единственное, что печатается, это «-». Я правильно использую цикл forEach
?JSP forEach loop
<c:forEach var="error" items="${errors}">
<h1>-${error}</h1>
<br>
</c:forEach>
Вот часть кода из doPost в Servlet:
ArrayList<String> errors = dataValidator.getErrors();
if (errors.isEmpty()) {
String cost = dataValidator.getCost();
request.setAttribute("cost", cost);
RequestDispatcher resultsDispatcher = request.getSession().getServletContext().getRequestDispatcher("/results.jsp");
try {
resultsDispatcher.forward(request, response);
} catch (ServletException se) {
System.out.println("Servlet Exception: " + se.toString());
} catch (IOException ioe) {
System.out.println("IO Exception: " + ioe.toString());
}
} else {
request.setAttribute("errors", errors);
RequestDispatcher errorDispatcher = request.getSession().getServletContext().getRequestDispatcher("/errors.jsp");
try {
errorDispatcher.forward(request, response);
} catch (ServletException se) {
System.out.println("Servlet Exception: " + se.toString());
} catch (IOException ioe) {
System.out.println("IO Exception: " + ioe.toString());
}
}
FYI: Вы можете просто вызвать 'request.getRequestDispatcher()'. Вам не нужно связываться с объектами контекста сеанса и сервлета (и вы, вероятно, не должны в любом случае). –