如何在JSTL视图层模拟一个group-by,跟踪前一个&循环列表中的当前值? [英] How to simulate a group-by in JSTL view layer by keeping track of previous & current values while looping over a List?
问题描述
首先,我检索服务层中的 Note
对象:
列表与LT;注> notes = this.getNotes();
然后我传递 List
of Notes
添加到视图层,并为 prevNote
和 currNote
添加到 List 的第一个元素:
< c: forEach items =$ {notes}var =notebegin =0end =1step =1>
< c:set var =currNote> $ {note}< / c:set>
< / c:forEach>
然后遍历 但是当我运行这个时,我得到这个错误信息: 我在这里要做的是检查 如何做到这一点以及为什么我要这个错误消息是什么? 使用 至于您的功能要求: 我想要做的是检查prevNote和currNote是否包含相同的userCode值 - 如果他们这样做,我想跳过列表中的当前元素,显示下一个具有不同userCode的注释(类似于模拟分组操作)。 仅在当前存储当前注释的循环就足够了。例如: First I retrieve the Then I pass Then I iterate over the But when I run this, I get this error message: What I want to do here is check whether Any hints on how to do this and why I'm getting this error message? Setting a value in the body of Use the
As to your functional requirement: What I want to do here is check whether prevNote and currNote contain the same userCode value - if they do I want to skip the current element in the List and display the next Note that has a different userCode (sort of simulating a group-by operation). Just storing the current note at end of the loop is sufficient. E.g.
这篇关于如何在JSTL视图层模拟一个group-by,跟踪前一个&循环列表中的当前值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! List
并设置 prevNote
到 currNote
和 currNote
到当前票据项目< c:forEach items =
) $ {notes}var =note>
上一页:< c:out value =$ {prevNote.userCode}/>
curr:< c:out value =$ {currNote.userCode}/>
< / c:forEach>
无法使用运算符
classjava.lang.String找到note的值。
prevNote
和 currNote
包含相同的 userCode
值 - 如果他们这样做,我想跳过列表
并显示具有不同的 userCode
的下一个注意
(模拟一个 group-by
operation)。
c :set
将基本上使用对象的 toString()
方法将其转换为 String
。这个例外也暗示你并不是在一个完整的 Note <中工作/ code>实例,但在
字符串
一个。
值
属性。
< c:set var =prevNotevalue =$ {注意}/>
< c:set var =currNotevalue =$ {note}/>
< c:forEach items =$ {notes}var =note>
< c:if test =$ {empty prevNote or note.userCode!= prevNote.userCode}>
Note
objects in the service layer:List<Note> notes = this.getNotes();
List
of Notes
to the view layer and set the same initial value for prevNote
and currNote
to the first element of the List
:<c:forEach items="${notes}" var="note" begin="0" end="1" step="1">
<c:set var="prevNote">${note}</c:set>
<c:set var="currNote">${note}</c:set>
</c:forEach>
List
and set prevNote
to currNote
and currNote
to the current note item (note
) from the loop:<c:forEach items="${notes}" var="note">
<c:set var="prevNote">${currNote}</c:set>
<c:set var="currNote">${note}</c:set>
prev:<c:out value="${prevNote.userCode}"/>
curr:<c:out value="${currNote.userCode}"/>
</c:forEach>
Unable to find a value for "note" in object of
class "java.lang.String" using operator ".
prevNote
and currNote
contain the same userCode
value - if they do I want to skip the current element in the List
and display the next Note
that has a different userCode
(sort of simulating a group-by
operation).c:set
will essentially convert it to String
using toString()
method of the object. The exception is also hinting that you aren't working on a fullworthy Note
instance, but on a String
one.value
attribute instead.<c:set var="prevNote" value="${note}" />
<c:set var="currNote" value="${note}" />
<c:forEach items="${notes}" var="note">
<c:if test="${empty prevNote or note.userCode != prevNote.userCode}">
<c:out value="${note.userCode}" />
</c:if>
<c:set var="prevNote" value="${note}" />
</c:forEach>