简体   繁体   English

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

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

Usually i can find solution provided by StackOverflow community in older questions.通常我可以在较旧的问题中找到 StackOverflow 社区提供的解决方案。 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.我想要做的就是显示我从 MySQL 数据库中取出的客户列表。 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(?).一切正常,但是当我将List从控制器传递给视图时,我无法在 jsp 文件中使用它,因为它似乎超出了范围(?)。

If you could suggest any solutions / links / tutorials i would be really thankful如果您能提出任何解决方案/链接/教程,我将不胜感激

CustomerDAO:客户DAO:

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).在这一点上,我不知道如何在我的 jsp 文件 (getAllCustomers.jsp) 中访问这个列表。 When I try to access it in Java block <% %>, then I get simple errors like:当我尝试在 Java 块 <% %> 中访问它时,我会收到一些简单的错误,例如:

"customers cannot be resolved to a variable" “客户无法解析为变量”

getAllCustomers.jsp: 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.我不确定它是否是自动系统,但我的问题不是如何使用 for 循环,而是如何通过 ModelAndView 将集合传递给 jsp。

edit2编辑2

I just figured it out what @RamPrakash meant.我刚刚弄清楚@RamPrakash 的意思。 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?但在那之前,也许来自不同时区的人可以回答是否使用 scriptlet 而不是 JSTL 可能会导致该问题? Thanks in advance!提前致谢!

In your jsp you are referring to the customers list by customers .在你的jsp你是指由客户名单customers This however is only a valid identifier in the context of the getAllCustomers method of your controller.然而,这只是控制器getAllCustomers方法上下文中的有效标识符。

Try customersList instead and consult the JavaDoc of the ModelAndView尝试customersList代替并查阅ModelAndView的 JavaDoc

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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