【问题标题】:HashMap display in JSP from Controller Springs来自 Controller Springs 的 JSP 中的 HashMap 显示
【发布时间】:2014-08-27 12:48:09
【问题描述】:

我是 Springs 的新手,目前卡在向 JSP 页面显示 HashMap 值。我只需要指定hashmap的key就可以得到对应的My page是一个listing 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

【问题讨论】:

    标签: java jsp spring-mvc hashmap


    【解决方案1】:

    对于jsp代码,

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

    在您的情况下,如果您确实想使用特定键获得价值

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

    【讨论】:

    • 'javax.servlet.ServletException: javax.servlet.jsp.JspTagException: 不知道如何迭代 <forEach>' 中提供的“项目”当我尝试使用您的密钥特定示例时显示此错误
    • @JagjeetSingh 现在我在您的控制器中看不到 List,它只是 Map。
    【解决方案2】:

    这样试试

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

    【讨论】:

    • ${listMapview.key}中的key是否引用了HashMap的key
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-08
    • 2013-04-06
    相关资源
    最近更新 更多