从Controller Springs在JSP中显示HashMap

时间:2014-08-27 12:48:10

标签: java jsp spring-mvc hashmap

我是Springs的新手,目前仍然在向JSP页面显示HashMap Value。我需要做的就是指定hashmap的键来获取相应的My page是一个列表页面。这是我的控制器代码,它返回数据成功

    @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

2 个答案:

答案 0 :(得分: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"%>

答案 1 :(得分:0)

对于jsp代码,

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

在您的情况下,如果您想要使用特定密钥获取价值

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