【问题标题】:How to iterate returned hashmap and get all values using jsp [duplicate]如何使用jsp迭代返回的hashmap并获取所有值[重复]
【发布时间】:2017-12-01 14:37:36
【问题描述】:

这是我在bean类中的函数——product.java 我从数据库中选择某些细节并将数据库中的值放入哈希图中。 HashMap 是一种类产品。

 public HashMap<String,Products> showProducts()
{  
    HttpServletRequest request = null;
    PreparedStatement preparedStatement;
    HashMap<String,Products>productMap=new HashMap<String,Products>();
    try 
    {
        preparedStatement = con.prepareStatement("select * from productdetails where producttype='toy'");

        ResultSet resultSet=preparedStatement.executeQuery();
        Products toy=new Products();
        while(resultSet.next()){
            toy.setProductId(resultSet.getInt(1));
            toy.setProductName(resultSet.getString(2));
            toy.setProductPrice(resultSet.getInt(3));
            productMap.put("toy",toy);

            request.setAttribute("productSessionMap",productMap);  
        }
                }

    catch(Exception e)
    {
        e.printStackTrace();
    }
    return productMap;

}

这是jsp页面

            <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
    <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
    <%@page import="java.util.Set"%>
    <%@page import="java.util.HashMap"%>
            <!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>
  Welcome!!!! <c:out value="${sessionScope.loginBean.userName}"></c:out>
   <jsp:useBean id="loginBean" class="com.training.entity.ShoppingCart" 
  scope="session"></jsp:useBean>
    <jsp:setProperty property="*" name="loginBean"/>

  <c:set var="status" value="${loginBean.showProducts()}"></c:set>
  <c:set var="keys" value="${status.keySet()}"></c:set>
  <c:out value="${status.toString()}"></c:out>   <!-- This line displays last value of hashmap-!>

 <c:forEach var="type" items="${productSessionMap}">
 <c:out value="${type[keys]}"></c:out>


 </c:forEach>


  </body>
  </html>

我想在不使用 scriptlet 标签的情况下,在 jsp 中迭代并显示 hashmap 中的每个值和键。请帮我解决这个问题。

即使我尝试使用 scriplet 标记.. 但我只在迭代时获得哈希图中的最后一个值..

  <%
  ShoppingCart ob=new ShoppingCart();

  HashMap<Integer,Products>newproductMap=new HashMap<Integer,Products>();
  newproductMap=ob.showProducts();
  Set<Integer>set = newproductMap.keySet();
 for(Integer ent:set){
 String name=newproductMap.get(ent).getProductName().toString();%>
  <%=name%>
  <%-- <%String value = ent.getValue().toString();%>
  <%=value%>
  --%>
 <%}%>

【问题讨论】:

    标签: jsp hashmap jstl scriptlet


    【解决方案1】:

    迭代中的每一项都是 Map.Entry 的一个实例。

    在这种情况下,您的 type 变量将是 Map.Entry。

    您应该首先为每个新产品分配一个新密钥

     while(resultSet.next()){
                    toy.setProductId(resultSet.getInt(1));
                    toy.setProductName(resultSet.getString(2));
                    toy.setProductPrice(resultSet.getInt(3));
                    productMap.put(""+resultSet.getInt(1),toy);//here assign a new unique key
                }
     //this must be moved outside of the while loop
     request.setAttribute("productSessionMap",productMap);  
    

    然后您可以使用 ${type.value} 访问每个 Map.Entry 的值,然后可以调用其 getter 方法访问该值的每个属性(产品类型) 即 ${type.value.productName}

    <table>
                <tbody>
                    <tr>
                        <th>Product Id</th>
                        <th>Product Name</th>
                        <th>Product Price</th>
                    </tr>
                    <c:forEach items="${requestScope.productSessionMap}" var="type">
                        <tr>
                            <td>
                                <c:out value="${type.key}"></c:out><!-- this is the key you specified in the map i.e. 'productId' -->
                            </td>
                            <td>
                                <c:out value="${type.value.productName}"></c:out>
                            </td>
                            <td>
                                <c:out value="${type.value.productPrice}"></c:out>
                            </td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
    

    【讨论】:

    • 但是有没有办法在 jsp 中做到这一点而不使用 scriplet 标签。
    • 我在 hashmap 中使用了重复键。感谢您发现这一点。
    • 试试这样 productId is ${product.key} productPrice 是: productName 是:
    • context.getAttribute("p") 中的“p”是什么
    • 我已经更正了答案,因为您想避免使用 scriplet 标签
    猜你喜欢
    • 2020-08-24
    • 1970-01-01
    • 1970-01-01
    • 2013-10-23
    • 2014-06-14
    • 2014-11-20
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多