1

This code used to work fine, but I just overhauled a lot of the underlying Java code and now it no longer works. I have a bean with values:

<c:forEach items="${actionBean.appliedJobs}" var="aJob" varStatus="loop">
    ${aJob}
</c:forEach>

This prints out: 14513=APPLIED 14535=APPLIED 14515=APPLIED 14514=APPLIED. appliedJobs is populated in Java as a HashMap<Long, String>. I also have code that accesses this array using a variable jobId which is of type Integer.

${jobId} //This prints correctly as 14513
${actionBean.appliedJobs[jobId]} //Prints nothing, is likely null
${actionBean.appliedJobs[14513]} //This prints correctly as APPLIED

The third line in the above block prints fine because 14513 is interpreted as a Long, I think. I'm aware of the Long/Integer jstl issues as described here and here, but the fact that this code used to work is what's throwing me off.

EDIT: For now, I've created a getter() for jobId that returns a Long jobId instead of Integer jobId and that has circumvented the issue, but I'd still like to know the root cause.

Community
  • 1
  • 1
jlewkovich
  • 2,725
  • 2
  • 35
  • 49

1 Answers1

-1

You can do the following to iterate through all elements of a Map :

 <c:forEach items="${actionBean.appliedJobs}" var="aJob" varStatus="loop">
            ${aJob.value}
            ${aJob.key}
 </c:forEach>
Kakarot
  • 4,252
  • 2
  • 16
  • 18
  • `javax.el.PropertyNotFoundException: Property 'value' not found on type javax.servlet.jsp.jstl.core.LoopTagSupport$1Status` – jlewkovich Feb 27 '14 at 22:12
  • my bad..I had used the varStatus variable instead of var. try it using aJob.value – Kakarot Feb 27 '14 at 22:13
  • Prints: `APPLIED APPLIED APPLIED APPLIED` – jlewkovich Feb 27 '14 at 22:21
  • Which is exopected is Applied is the value..if you want to print the key use aJob.key – Kakarot Feb 27 '14 at 22:22
  • I'm using the forEach as a debugging tool, the real problem occurs in `${actionBean.appliedJobs[jobId]}`. Read my edit for more info, I'm looking to identify a root cause. – jlewkovich Feb 27 '14 at 22:27
  • Ultimately the function `return new HashMap();`, populated using `map.put(jobId,"APPLIED")` where jobId is of type Integer – jlewkovich Feb 27 '14 at 22:34
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/48624/discussion-between-kakarot-and-j-l) – Kakarot Feb 27 '14 at 22:35