Java 集合不在jsp文件(Spring MVC、ModelAndView)的范围内

Java 集合不在jsp文件(Spring MVC、ModelAndView)的范围内,java,jsp,model-view-controller,modelandview,Java,Jsp,Model View Controller,Modelandview,通常我可以在旧问题中找到StackOverflow社区提供的解决方案。这一次我的大脑停止工作了,我想 我要做的就是显示一个从MySQL数据库中取出的客户列表。一切都很顺利,但当我将列表从控制器传递到视图时,我无法在jsp文件中使用它,因为它似乎超出了范围(?) 如果你能提出任何解决方案/链接/教程,我将非常感谢 客户道: public List<Customer> getAllCustomers() { EntityManagerFactory entityM

通常我可以在旧问题中找到StackOverflow社区提供的解决方案。这一次我的大脑停止工作了,我想

我要做的就是显示一个从MySQL数据库中取出的客户列表。一切都很顺利,但当我将列表从控制器传递到视图时,我无法在jsp文件中使用它,因为它似乎超出了范围(?)

如果你能提出任何解决方案/链接/教程,我将非常感谢

客户道:

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;
    }
@RequestMapping(value = "/getAllCustomers")
    public ModelAndView getAllCustomers(){
        
        List<Customer> customers = customerDAO.getAllCustomers();
        
        return new ModelAndView("getAllCustomers", "customersList", customers);
    }
<%@ 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>
公共列表getAllCustomers()
{
EntityManagerFactory EntityManagerFactory=Persistence.createEntityManagerFactory(“库管理器”);
EntityManager EntityManager=EntityManager工厂。createEntityManager();
TypedQuery query=entityManager.createQuery(“从客户e中选择e”,客户.class);
List customers=query.getResultList();
entityManager.close();
entityManagerFactory.close();
返回客户;
}
客户控制器:

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;
    }
@RequestMapping(value = "/getAllCustomers")
    public ModelAndView getAllCustomers(){
        
        List<Customer> customers = customerDAO.getAllCustomers();
        
        return new ModelAndView("getAllCustomers", "customersList", customers);
    }
<%@ 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>
@RequestMapping(value=“/getAllCustomers”)
公共模型和视图getAllCustomers(){
List customers=customerDAO.getAllCustomers();
返回新模型和视图(“getAllCustomers”、“CustomerList”、customers);
}
此时,我不知道如何在我的jsp文件(getAllCustomers.jsp)中访问此列表。 当我尝试在Java块中访问它时,会出现如下简单错误:

“无法将客户解析为变量”

getAllCustomers.jsp:

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;
    }
@RequestMapping(value = "/getAllCustomers")
    public ModelAndView getAllCustomers(){
        
        List<Customer> customers = customerDAO.getAllCustomers();
        
        return new ModelAndView("getAllCustomers", "customersList", customers);
    }
<%@ 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>

在此处插入标题
所有客户名单:
我希望你能找到一些耐心来帮助我:)

编辑 我得到的一些信息可能与我的问题相同

我不确定它是否是自动系统,但我的问题不是如何使用for循环,而是如何通过ModelAndView将集合传递给jsp

编辑2 我刚刚明白了@RamPrakash的意思。我明天会试试他的解决办法。 但在那之前,也许来自不同时区的人可以回答,使用scriptlet而不是JSTL是否会导致这个问题?
提前谢谢

在您的
jsp
中,您通过
customers
引用客户列表。但是,这只是控制器的
getAllCustomers
方法上下文中的有效标识符


请尝试
customersList
,然后咨询网站的JavaDoc

试试这个@RamPrakash,非常感谢!明天我将尝试使用JSTL,我希望它会有所帮助。我会让你知道的。谢谢@hotzst。我尝试了你的解决方案(我的错误很明显,我不知道我怎么会没有注意到。)由于某种原因,它仍然不起作用。当我使用“CustomerList”时,我得到了相同的错误。我不确定,但看起来ModelAndView出现了一些错误,看起来jsp“不知道”正在将“CustomerList”传递给它。明天我会尝试一下,非常感谢你们的帮助:)那个解决方案对我帮助不大,而RamPrakesh在主帖子的评论中提出的解决方案对我帮助不大。