sproketboy
sproketboy

Reputation: 9452

How can I convert a velocity expression to JSP?

I have a page which I'm converting from Velocity to JSP. I have some complex expressions which I can't figure out how to convert to JSTL el language.

#set ($col = 0)

#foreach ($hour in $form.bean.grid.hours)
  $hour.cells.get($col).hourOfDay
  #set ($col = $col + 1)
#end

Hour is an object which contains a cell which contains a list. I need to get each element through a numeric index.

Any ideas?

Upvotes: 0

Views: 695

Answers (2)

skaffman
skaffman

Reputation: 403441

Something like:

<c:set var="col" value="0"/>

<c:forEach items="${form.bean.grid.hours}" var="hour">
   ${hour.cells[col].hourOfDay}
   <c:set var="col" value="${col + 1}"/>
</c:forEach>

This will only work if hour.cells is a Map, so that the cells.get($col) expression in the original is calling get() on that Map. If it's an arbitrary method call, then it won't work, since JSP EL can only handle bean properties or collections.

As @EliteGentleman points out, you can use the varStatus on the forEach loop to remove the need for a separate loop counter, which you should do. My fragment was a more literal translation.

Upvotes: 0

Buhake Sindi
Buhake Sindi

Reputation: 89169

Basically, you're displaying hours of day. Using JSTL,

<c:forEach items="${form.bean.grid.hours}" var="hour" varStatus="index">
   ${hour.cells[index.count - 1].hourOfDay}
</c:forEach>

The count in index.count starts counting from 1 to N (so negate it by 1).

Upvotes: 1

Related Questions