2014-08-27 18 views
0

我是Springs的新手,並且當前停留在向JSP頁面顯示HashMap值。我所需要做的就是指定hashmap的關鍵字來獲取相應的我的頁面是一個列表頁面。這裏是我的控制器代碼,返回數據succesfull來自Controller Springs的JSP中的HashMap顯示

@Controller 
    public class GetUserController { 
     @Autowired 
     private AddUserServiceImpl aus; 

     @RequestMapping(value="/getUser.do",method=RequestMethod.GET) 
     public ModelAndView getUser() 
     { 
      HashMap<String,String> listMap = new HashMap<String,String>(); 
      List<User> u = (List<User>) aus.getUser(); 
      System.out.println("List Size "+u.size()); 
      for(User ux : u) 
      { 
       listMap.put("userId", String.valueOf(ux.getUser_id())); 
       listMap.put("userName", String.valueOf(ux.getUserName())); 
       listMap.put("firstName", String.valueOf(ux.getFirstName())); 
       listMap.put("lastName", String.valueOf(ux.getLastName())); 
       listMap.put("email", String.valueOf(ux.getEmail())); 
      } 
      return new ModelAndView("listingView","listMapView",listMap); 
     } 

    } 

MY JSP is as Follows 



<%@ 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"%> 
<!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> 
    <table align="center" style="padding-top: 30px; border: 0.5px;"> 
     <tr> 
      <th bgcolor="#2E64FE">USERNAME</th> 
      <td>&nbsp;&nbsp;</td> 
      <th bgcolor="#2E64FE">FIRSTNAME</th> 
      <td>&nbsp;&nbsp;</td> 
      <th bgcolor="#2E64FE">LASTNAME</th> 
      <td>&nbsp;&nbsp;</td> 
      <th bgcolor="#2E64FE">EMAIL</th> 
      <td>&nbsp;&nbsp;</td> 
     </tr> 

     <c:forEach var="listMapview" items="${listMapView.listMap}" varStatus="status"> 
      <tr> 
       <td>${listMapview.key}</td> 
       <td>${listMapview.key.userName}</td> 
       <td>${listMapview.key.firstName}</td> 
       <td>${listMapview.key.lastName}</td> 
       <td>${listMapview.key.email}</td> 

      </tr> 
     </c:forEach> 
    </table> 
</body> 
</html> 



Controller calls the JSP succesfully returning the data 

回答

0

嘗試這樣

<c:forEach items="${listMapView.listMap}" var="listMapview" varStatus="status"> 
    <tr> 
     <td>${listMapview.key}</td> 
     <td>${listMapview.value}</td> 
    </tr> 
</c:forEach> 

不要忘了,包括這個

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> 
+0

確實在$鍵{} listMapview.key指的HashMap的關鍵 – 2014-08-27 13:25:33

+0

沒錯就是它做什麼 – SparkOn 2014-08-27 13:26:07

+0

工作就像魅力。非常感謝 – 2014-08-29 10:46:43

0

對於JSP代碼,

 <c:forEach items="${listMap}" var="mapItem"> 
      ${mapItem.key} ${mapItem.value} 
     </c:forEach> 

就你而言,如果你有風險的話要使用特定的鍵

  <c:forEach items="${listMap}" var="mapEntry"> 
       ${mapEntry['userId']} 
      </c:forEach> 
+0

「javax.servlet.ServletException獲得價值:javax.servlet.jsp.JspTagException:不知道如何遍歷提供「項目」中<的forEach >「 顯示我這個錯誤,當我嘗試用你的鑰匙具體的例子 – 2014-08-29 10:54:45

+0

@JagjeetSingh現在我看不到列表在你的控制器中,它只是Map <>。 – 2014-08-29 11:02:09