2017-12-01 15 views
0

Das ist meine Funktion in der Bean-Klasse - product.java Im Wählen der bestimmten Details aus der Datenbank und setzen Sie die Werte aus der Datenbank in einer Hashmap. HashMap ist eine Art von Class Products. withous mit scriptlet TagsWie man zurückgegebene hashmap iteriert und alle Werte mit Hilfe von jsp

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; 

} 

Dies ist die jsp Seite

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

Ich möchte in der hashmap in jsp jeden Wert und Schlüssel iterieren und anzuzeigen. Bitte hilf mir dabei.

Auch habe ich versuchte scriplet-Tag .. aber ich bin nur den letzten Wert in der hashmap bekommen, wenn ..

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

Antwort

0

Jedes Element in der Iteration Iteration ist ein istance von Map.Entry.

In diesem Fall wird Ihre type var eine Map.Entry sein.

Sie mit $

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

Dann können Sie den Wert jedes Map.Entry zugreifen {type.value} dann jedes Attribut des Wertes (Art Products) einen neuen Schlüssel zu jedem neuen Produkt zuerst zuweisen soll werden kann dh $ {} 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> 
+0

Aufrufen seiner Getter-Methode zugegriffen Aber gibt es eine Möglichkeit, dies ohne Verwendung scriplet Tags in jsp zu tun. –

+0

Ich verwendete doppelten Schlüssel in hashmap. Danke, dass du das herausgefunden hast. –

+0

Versuchen Sie so etwas wie dieses \t productId ist $ {} product.key \t \t \t \t <% \t \t Produkte p = (Produkte) pageContext.getAttribute ("p"); \t \t%> \t \t product ist: <% = p.productPrice%> \t \t product ist: <% = p.productName%> –

Verwandte Themen