简体   繁体   中英

Collection is not in scope of jsp file (Spring MVC, ModelAndView)

Usually i can find solution provided by StackOverflow community in older questions. This time my brain stopped working, i guess...

All I'm trying to do is show a list of customers which I'm taking out of MySQL database. Everything is going fine, but when i'm passing the List from controller to view I can't use it in jsp file, as it appears to be out of scope(?).

If you could suggest any solutions / links / tutorials i would be really thankful

CustomerDAO:

public List<Customer> getAllCustomers()
    {
        EntityManagerFactory entityManagerFactory = Persistence.createEntityManagerFactory("library-manager");
        EntityManager entityManager = entityManagerFactory.createEntityManager();
        
        TypedQuery<Customer> query = entityManager.createQuery("select e from Customer e", Customer.class);
        List<Customer> customers = query.getResultList();
        
        entityManager.close();      
        entityManagerFactory.close();
        
        return customers;
    }

CustomersController:

@RequestMapping(value = "/getAllCustomers")
    public ModelAndView getAllCustomers(){
        
        List<Customer> customers = customerDAO.getAllCustomers();
        
        return new ModelAndView("getAllCustomers", "customersList", customers);
    }

At this point I have no clue how to access this List in my jsp file (getAllCustomers.jsp). When I try to access it in Java block <% %>, then I get simple errors like:

"customers cannot be resolved to a variable"

getAllCustomers.jsp:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="db_objects.Customer" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<h2>List of all customers:</h2>
<%
for(Customer customer : customers){
    out.println(customer.getCustomerFirstName);
    out.println(customer.getCustomerLastName);
    out.println(customer.getCustomerContactNumber);
}
%>
</body>
</html>

I hope you will find some patience to help me a little bit here :)

edit

I got some information that my question may be the same as this one

I'm not sure if it's automatic system or not, but my question is not how to use for loop but how to pass a collection to jsp via ModelAndView.

edit2

I just figured it out what @RamPrakash meant. I will try his solution tomorrow. But until then, maybe someone from different timezone could answer if using scriptlet instead of JSTL may cause that problem? Thanks in advance!

In your jsp you are referring to the customers list by customers . This however is only a valid identifier in the context of the getAllCustomers method of your controller.

Try customersList instead and consult the JavaDoc of the ModelAndView

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM