简体   繁体   English

发送ArrayList <Product> 从Servlet接收并在JSP中接收

[英]Send ArrayList<Product> from Servlet and receive in JSP

This is my servlet code. 这是我的servlet代码。 Here, products is an object of ArrayList<Products> and I am sending this to a JSP file. 在这里, productsArrayList<Products>的对象,我将其发送到JSP文件。 request is an object of type HttpServletRequest . requestHttpServletRequest类型的对象。

request.setAttribute("listOfProducts", products);
request.getRequestDispatcher("UpdateProduct.jsp").forward(request, response);

In my JSP, I tried to receive this. 在我的JSP中,我尝试接收此消息。

ArrayList<Products> product = request.getAttribute("listOfProducts");

It shows me this error 它告诉我这个错误

Type mismatch: cannot convert from Object to ArrayList 类型不匹配:无法从Object转换为ArrayList

Then I tried this 然后我尝试了这个

ArrayList<Products> product = (ArrayList<Products>) request.getAttribute("listOfProducts");

Then I got this warning 然后我得到了这个警告

Multiple annotations found at this line: - Type safety: Unchecked cast from Object to ArrayList - Type mismatch: cannot convert from Object to ArrayList 在此行发现多个注释:-类型安全性:未经检查的从Object到ArrayList的转换-类型不匹配:无法从Object转换为ArrayList

The following is the modern (environmentally friendly!) way to do this: 以下是执行此操作的现代方式(环境友好!):

<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:jsp="http://java.sun.com/JSP/Page"
      xmlns:c="http://java.sun.com/jsp/jstl/core">
<head>
    ...
</head>
<body>
    ...
    <c:forEach items="${listOfProducts}" var="product">
        <tr><td>${product.id} </td>
            <td>${product.name} </td>
            <td>${product.whatever} </td></tr>
    </c:forEach>
    ...
</body>
</html>

The file extension of your JSP must be .jspx . JSP的文件扩展名必须为.jspx

No java code is required in the page. 页面中不需要Java代码。

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

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