简体   繁体   English

为什么这在带有散列表的jstl中起作用?

[英]why does this work in jstl with hashmaps?

why does display what i want: 为什么显示我想要的内容:

   <c:forEach var="temp" items="${AvailableLessonBean.lessons['description']}">
                    <form action="" method="POST">
                        <tr>
                            <td>
                                <c:out value="${temp}"/>
                            </td>

which displays in a html table: 显示在html表中:

Description Start Date Start Time End Time Level Make Booking Snowboarding for dummies 说明开始日期开始时间结束时间水平进行预订假人单板滑雪
Advanced Carving Techniques 先进的雕刻技术
How to not fall off the draglift 如何不从吊车上掉下来
Gnarliness Extreeeeeeeme 粗俗的外遇
Parallel turns 平行匝
How to splint a broken leg with as 如何用as夹住断腿
Cross-country techniques 越野技术
Aerobatics 特技飞行
Intermediate Slalom 中级激流回旋

and this does not display anything but an exception: 除了异常以外,它什么都不显示:

    <c:forEach var="temp" items="${AvailableLessonBean.lessons}">
                    <form action="" method="POST">
                        <tr>
                            <td>
                                <c:out value="${temp['description']}"/>
                            </td>

I have no hair left because of this!! 因此,我没有头发了!!

Once again, you didn't tell us what these objects are, which makes it impossible to answer. 再一次,您没有告诉我们这些对象是什么,这使得无法回答。 Hopefully for you, I remember the data structure from a previous question. 希望对您来说,我记得上一个问题中的数据结构。

So, AvailableLessonBean.getLessons() returns a Map<String, List<String>> . 因此, AvailableLessonBean.getLessons()返回Map<String, List<String>>

In your first snippet, you iterate over AvailableLessonBean.lessons['description'] . 在第一个代码段中,您遍历AvailableLessonBean.lessons['description'] What this EL code does is that it gets the value associated with the key "description" in the map: 该EL代码的作用是获取与映射中的键“ description”相关联的值:

map.get("description")

that works fine. 效果很好。 It returns a List<String> . 它返回一个List<String> It iterates over that list, and stores the current element of the list in a variable named temp . 遍历该列表,并将列表的当前元素存储在名为temp的变量中。 And at each iteration, it prints temp . 并且在每次迭代时,它都会打印temp This is thus equivalent to the following Java code: 因此,这等效于以下Java代码:

List<String> list = availableLessonBean.getLessons().get("description");
for (String temp : list) {
    out.println(temp);
}

In the second snippet, you're iterating over the map itself. 在第二个片段中,您将遍历地图本身。 The JSTL specifies that when you iterate over a map, you iterate over all the entries of the map. JSTL指定当您遍历地图时,遍历该地图的所有条目。 Each entry has a key and a value. 每个条目都有一个键和一个值。 So the iteration is equivalent to doing, in Java: 因此,迭代等效于在Java中进行:

for (Map.Entry<String, List<String>> temp : availableLessonBean.getLessons().entrySet()) {
    ...
}

But then you're trying to access temp["description"] . 但是,然后您尝试访问temp["description"] That doesn't make any sense. 那没有任何意义。 temp is a Map Entry. temp是一个Map Entry。 A Map Entry has a key and a value. 映射条目具有键和值。 That's all. 就这样。 In this case, the key is a String and the value is a List<String> . 在这种情况下,键是String ,值是List<String>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM