Win a copy of Java Mock Exams (software) this week in the Programmer Certification (OCPJP) forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Accessing a map with a parameter in a JSP

 
Derek Eichele
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 65658
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Derek Eichele
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 65658
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Derek Eichele
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 65658
129
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes. When the value is embedded within <c:set> the rendered body is always treated as a string.
 
Derek Eichele
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Thank you very much for the solution and the explanation.
 
What are you doing? You are supposed to be reading this tiny ad!
the new thread boost feature brings a LOT of attention to your favorite threads
https://coderanch.com/t/674455/Thread-Boost-feature
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!