简体   繁体   中英

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?

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).

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM