Issue
I'm getting the error above while trying to populate a xml file with info from a simple ArrayList of the class RankingResult. After searching around I found out that most people with this error made typos in the xml, but that doesn't seems to be the case here (I'll feel real stupid if it is).
I already have a really similar thing going on and working perfectly (controller redirects to a xml sending an ArrayList of objects which is then printed by , so I'm completely lost here.
Here's some code:
The "ranking.jsp" xml
<?xml version="1.0" encoding="UTF-8"?>
<%@page contentType="application/xml" pageEncoding="UTF-8"%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<data>
<c:forEach items="${results}" var="result">
<tr>
<td>${result.genero}</td>
<td><c:out value="${result.quantidade}"/></td>
</tr>
</c:forEach>
</data>
Controller doPost()
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String ator = request.getParameter("ator");
String diretor = request.getParameter("diretor");
ArrayList<RankingResult> results = null;
try{
BuscaDAO b2DAO = new BuscaDAO();
results = b2DAO.busca2(ator, diretor);
} catch(DAOException | SQLException ex) {
Logger.getLogger(Busca1.class.getName()).log(Level.SEVERE, null, ex);
}
request.setAttribute("results", results);
request.getRequestDispatcher("/WEB-INF/xml/ranking.jsp").forward(request, response);
}
Debugging confirms that the "results" ArrayList is correctly populated.
The RankingResult class:
public class RankingResult {
public final String genero;
public final int quantidade;
public RankingResult(String genero, int quantidade){
this.genero = genero;
this.quantidade = quantidade;
}
}
Project tree:
Solution
The message is absolutely right. There is no property name genero in your class. You have a public field named genero. But the JSP EL works on Java Bean properties. You need a
public String getGenero() {
return this.genero;
}
method in your RankingResult
class.
Using public fields is bad practice in general, and won't work with the JSP EL, which is designed around the Java Beans conventions.
Answered By - JB Nizet
Answer Checked By - Pedro (JavaFixing Volunteer)