繁体   English   中英

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

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

这是我的servlet代码。 在这里, productsArrayList<Products>的对象,我将其发送到JSP文件。 requestHttpServletRequest类型的对象。

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

在我的JSP中,我尝试接收此消息。

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

它告诉我这个错误

类型不匹配:无法从Object转换为ArrayList

然后我尝试了这个

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

然后我得到了这个警告

在此行发现多个注释:-类型安全性:未经检查的从Object到ArrayList的转换-类型不匹配:无法从Object转换为ArrayList

以下是执行此操作的现代方式(环境友好!):

<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>

JSP的文件扩展名必须为.jspx

页面中不需要Java代码。

暂无
暂无

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

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