c: forEach does not work

0

For some reason my <c:forEach> does not work, my normal for-each normal, in which I System.out.print works normally.

MODEL:

    package model;

public class ModeloX {

    private int id;
    private String desc;
    private float val;

    public ModeloX() {

    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public float getVal() {
        return val;
    }

    public void setVal(float val) {
        this.val = val;
    }


}

JSP:

    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ page import="java.util.*" %>
<%@ page import="model.ModeloX" %>


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">

<%

ModeloX mx = new ModeloX();
List<ModeloX> listaObjetos;
listaObjetos = new ArrayList();

listaObjetos = (ArrayList<ModeloX>)request.getAttribute("listaRegistros");
System.out.println("TESTANDO O PRINT");
for(ModeloX obj : listaObjetos){
    System.out.println("ObjetoID: " + obj.getId() + " Descrição do objeto: " + obj.getDesc() + 
            " Valor do obejto: " + obj.getVal());
}
%>

<h1>Página Inicial!</h1>

<c:forEach items="${listaObjetos}" var="item">
  <c:out value="${item.id}"/>
</c:forEach>

My console:

TESTANDO O PRINT

    ObjetoID: 1 Descrição do objeto: uma descrição Valor do obejto: 40.2
    ObjetoID: 2 Descrição do objeto: Uma descricao qualquer Valor do obejto: 20.2
    ObjetoID: 3 Descrição do objeto: NOVA DESC ATUALIZADA Valor do obejto: 66.66
    ObjetoID: 5 Descrição do objeto: aaa Valor do obejto: 123.2

As you can see, my list is populated I can display the values in the console, however in <c:forEach> nothing appears.

    
asked by anonymous 05.02.2018 / 19:40

1 answer

2

Change% of% by% with%.

The JSP will look for references to variables in page scopes, request, session, and application (in that order). Since its items="${listaObjetos}" is a local variable of a scriptlet, it is not a place where the JSP will search for variable references.

    
05.02.2018 / 20:19