<c:foreach> does not work

Asked

Viewed 588 times

0

For some reason, man <c:forEach> doesn’t work, my for-each normal, in which I do the 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 on the console, but on <c:forEach> nothing appears.

  • @Renan Hi, you must have noticed that I have no body, nor head, nor anything there right. I made a kind of single page application, this content is "loadado" in a div container, in my main page I have it here <%@taglib prefix="c" Uri="http://java.sun.com/jsp/jstl/core"%>, but even playing the taglib in my secondary page I can not return the c:foreach

  • really, it seems that I need to have the libraries on the secondary page, I did this but still no result

1 answer

2


Change items="${listaObjetos}" for items="${listaRegistros}".

JSP will search for references to variables in page scopes, request, session, and application (in that order). Your listaObjetos is a local variable of a scriptlet, not a place where JSP will search for variable references.

  • It worked, thank you very much!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.