[英]Send ArrayList<Product> from Servlet and receive in JSP
這是我的servlet代碼。 在這里, products
是ArrayList<Products>
的對象,我將其發送到JSP文件。 request
是HttpServletRequest
類型的對象。
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.