aspose file tools
The moose likes JSP and the fly likes Accessing a map with a parameter in a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » JSP
Reply Bookmark "Accessing a map with a parameter in a JSP" Watch "Accessing a map with a parameter in a JSP" New topic
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
    
  13

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
    
  13

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
    
  13

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.
 
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
 
subject: Accessing a map with a parameter in a JSP
 
Similar Threads
struts dynavalidatorBean with HashMap & JSTL
How to store a value so that any JSP page can read it?
How to obtain a Map index
static variables values are not getting refreshed
Iterating over HaspMap<String,List<Object>> and comparing values.