views:

200

answers:

1
<c:set var="nameLookup" value="${names}" />
<c:forEach var="result" items="${results}">
    <tr>  
     <td>${result.uglyDisplayName}</td>
     <td>${result.phonenum}</td>
    </tr>
</c:forEach>

This is an excerpt from a jsp I'm trying to edit.

Results is a List<Object> being returned in the ModelAndView from the controller, of which each Object has a getUglyDisplayName and getPhonenum. I'm not actually clear on how that's working. I guess the jsp is doing some getClass().getName() or something behind the scenes? Any pointers on that process would be enlightening. Anyway, that part is working.

In the controller I've added another object to be returned in the ModelAndView. It's a HashMap that has nicer display names for which the ugly display names are the keys. So I want to replace that first td with something like this:

<td>${nameLookup.get(result.uglyDisplayName)}</td>

This doesn't work, obviously, or I wouldn't be posting here. I went ahead and set a var to the name I put the HashMap in the ModelAndView under (top line) but I'm not sure if that's the right way to get at that object.

+2  A: 

For the first part ("what JSP is doing") see the JSP Expression Language.

For the second part, try:

<td>${nameLookup[result.uglyDisplayName]}</td>

I wouldn't like to swear it'll work (it's a long time since I've used JSP) but it's worth a try.

Jon Skeet
Jon is, as he tends to be, correct. Note that non-static method invocation has been implemented for JSR 245 (JEE6 / JSP 2.1 containers). So, in the near future, a call like this will be valid: nameLookup.get(result.uglyDisplayName)
McDowell