How to display attributes of an object passed in a list in a JSP page

Asked

Viewed 810 times

2

I am working on a Java Web project, using the Spring MVC framework.

I’m having difficulty displaying information that comes from a list where analysts are registered.

The name of the analyst is correctly displayed, since the name is a String attribute of an object in the Analyst class. But when trying to display the CNPJ (String) of the Enterprise object (of the Enterprise class), which in this case is an attribute of an object of the Analyst class, the CNPJ number does not appear.

<c:forEach var="analista" items="${listAnalistas}" varStatus="status">  
    <a>Nome do analista: <c:out value="${analista.nome}" /> </a>
    <a>CNPJ da empresa: <c:out value="${analista.empresa.cnpj}" /></a>
</c:forEach>    

The list is filled in the Controller class:

  @RequestMapping(value = "/")
     public ModelAndView home() {
    List<Analista> listAnalistas = analistaDao.list();
    ModelAndView model = new ModelAndView("/home/home");
    model.addObject("listAnalistas", listAnalistas);

    return model;
}

Is there any way to display this information?

2 answers

2

Gabriel Polo, passes the class of your controller on forEach

<c:forEach var="analista" items="${"classe do seu model".listAnalistas}" varStatus="status">  
    <a>Nome do analista: <c:out value="${analista.nome}" /> </a>
    <a>CNPJ da empresa: <c:out value="${analista.empresa.cnpj}" /></a>
</c:forEach>

0


The problem has been fixed by changing the Hibernate configuration file (Hibernate.cfg.xml).

I had to add the following line:

<property name="enable_lazy_load_no_trans">true</property>

Browser other questions tagged

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