0

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:

Project tree(1) Project tree(2)

renanrfranca
  • 71
  • 1
  • 11

1 Answers1

2

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.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255
  • You are also absolutely right, I had no idea. I did try using getters at some point, but I guess I didn't pay attention to the results and thought the issue persisted. I do blame [This guy](https://stackoverflow.com/a/6271781/2870015) for the public fields tho :o). Thank you very much for your time. – renanrfranca Jun 25 '17 at 12:27