简体   繁体   English

如何将速度表达式转换为JSP?

[英]How can I convert a velocity expression to JSP?

I have a page which I'm converting from Velocity to JSP. 我有一个页面要从Velocity转换为JSP。 I have some complex expressions which I can't figure out how to convert to JSTL el language. 我有一些复杂的表达式,无法弄清楚如何转换为JSTL el语言。

#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. Hour是一个对象,其中包含一个包含列表的单元格。 I need to get each element through a numeric index. 我需要通过数字索引获取每个元素。

Any ideas? 有任何想法吗?

Basically, you're displaying hours of day. 基本上,您显示的是一天中的小时数。 Using JSTL, 使用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). count在index.count开始从1至N(所以由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 . 仅在hour.cells是一个Maphour.cells ,因此cells.get($col)中的cells.get($col)表达式正在该Map上调用get() If it's an arbitrary method call, then it won't work, since JSP EL can only handle bean properties or collections. 如果这是一个任意方法调用,那么它将不起作用,因为JSP EL仅能处理bean属性或集合。

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. 正如@EliteGentleman指出的那样,您可以在forEach循环上使用varStatus来消除对单独的循环计数器的需要,您应该这样做。 My fragment was a more literal translation. 我的片段是更直译的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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