| Author |
Accessing a map with a parameter in a JSP
|
Derek Eichele
Greenhorn
Joined: Aug 19, 2010
Posts: 10
|
|
First I wanted to say that I've used this forum as a resource many times, and found many good solutions here.
This time, however, I can't seem to find 'exactly' what I'm looking for, so here I am :-)
I've got a Struts 1.3 application, and in an action class, I create :
HashMap<String, SegmentStatus> segmentStatuses = new HashMap<String, SegmentStatus>();
session.setAttribute("segmentStatuses ", segmentStatuses );
Within the JSP page, I have a variable that I'm creating from another value (mainly for debugging/simplicity but shouldn't be necessary)
<c:set var="personId" value="${person.personId}" scope="session"/>
Using the value in "personId" as the key, I would like to confirm the existence of an entry in the map:
<c:out value='${sessionScope.segmentStatuses[personId]}' />
However, I'm not getting matches when I know that the value in "personId" exists as a key in segmentStatuses.
Using copious debugging statements, I have verified that "segmentStatuses" is populated properly and in the session scope of the JSP. I have also verified that "personId" contains a legitimate value (10327).
However, this statement returns the result I expect:
<c:out value='${sessionScope.segmentStatuses['10327']}' />
Obviously the map is not returning a match when I use "personId" instead of a literal. I've done a lot of reading, some posts suggesting that the key needs to be a Long, which I tried with no success (among many other things!).
If someone could explain where I am going wrong or point me in the right direction I would really appreciate it.
Thanks in advance.
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56165
|
|
|
It could be a string/number mismatch. Are the keys numeric or a string? Does the key you are trying to use match in type?
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Derek Eichele
Greenhorn
Joined: Aug 19, 2010
Posts: 10
|
|
In the line
<c:set var="personId" value="${person.personId}" scope="session"/>
the original type of "person.personId" is int.
The key to the HashMap is String.
Is there way to "force" a conversion?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56165
|
|
|
|
 |
Derek Eichele
Greenhorn
Joined: Aug 19, 2010
Posts: 10
|
|
Excellent, that did it. Now I'd like to understand it.
The version of c:set that I used created a var of the same type as the original (int), whereas your method created a String. Is that because the content of the body of the tag is treated as a String?
|
 |
Bear Bibeault
Author and ninkuma
Marshal
Joined: Jan 10, 2002
Posts: 56165
|
|
|
Yes. When the value is embedded within <c:set> the rendered body is always treated as a string.
|
 |
Derek Eichele
Greenhorn
Joined: Aug 19, 2010
Posts: 10
|
|
Thank you very much for the solution and the explanation.
|
 |
 |
|
|
subject: Accessing a map with a parameter in a JSP
|
|
|