English 中文(简体)
fetching values from map in jstl
原标题:

I have the following code on my jsp page:

<c:out value="${items}" /><br />

<c:forEach items="${items}" var="item">
     1. <c:out value="${item.key}" /><br />
     2. <c:out value="${item.key eq 70}" /><br />
     3. <c:out value="${items[70]}" /><br />
     4. <c:out value="${items[item.key]}" /><br />
</c:forEach>

And it produces the following output

{70=true}
1. 70
2. true
3.
4. true 

And I just can t figure out why 3. is empty. Any ideas?

The map is of type Map<Integer, Boolean>

问题回答

Basically autoboxing puts an Integer object into the Map.

map.put(new Integer(0), "myValue")

EL evaluates 0 as a Long and thus goes looking for a Long as the key in the map. i.e. it evaluates:

map.get(new Long(0))

As a Long is never equal to an Integer object, it does not find the entry in the map. That s it in a nutshell.

Refer forum http://forums.sun.com/thread.jspa?messageID=9702932#9702932





相关问题
Mysql Foreach Child show Max() and Min() in a single line

Is it possible to use a select statement so that it returns Max and min columns of a child row foreach Parent record? So foreach parent record that has MANY child records I want to see the Max and ...

Foreach in SQL?

I m not quite sure how to do this in SQL. Here it is in pseudocode: Take the list of nodes with content type X. For each node, take the value of field Y. Insert into term_nodes VALUES ((tid that ...

Custom container requirement to work with Qt s foreach

What is the bare minimum amount of code to create a custom container that would work with Qt foreach macro? I have this so far template< class T > class MyList { public: class iterator { ...

foreach loops & stdclass objects

I ve seen similar questions on here but I can t seem to apply the solutions to my problem. I have a variable called $results which I got from an API. I ll change the proper nouns so as to protect my ...

PHP - Foreach loops and ressources

I m using a foreach loop to process a large set of items, unfortunately it s using alot of memory. (probably because It s doing a copy of the array). Apparently there is a way to save some memory with ...

Hide a column programmatically in MS-Access

I want to hide or show a column based on variable data from a users selection. How do you set a column to hidden in MS-Access 2003? For Example, After user change event... For Each ctl In Me....

iterating over map and array simultaneously in a for loop

I am having some trouble creating a for loop within a constructor to iterate over a map and an array at the same time. Here, it is indicated that this cannot be done with an enhanced for loop. I have ...

Changing a struct inside another struct in a foreach loop

The following code prints (when invoking MyMethod): 0 0 0 1 I would expect it to print: 0 0 1 1 Why is this? Code: private struct MyStruct { public MyInnerStruct innerStruct; } private ...

热门标签