如何在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?

查看:87
本文介绍了如何在JSTL视图层模拟一个group-by,跟踪前一个&循环列表中的当前值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我检索服务层中的 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>

然后遍历 List 并设置 prevNote currNote currNote 到当前票据项目< c:forEach items = ) $ {notes}var =note>

< c:set var =currNote> $ {note}< / c:set>

上一页:< c:out value =$ {prevNote.userCode}/>
curr:< c:out value =$ {currNote.userCode}/>

< / c:forEach>

但是当我运行这个时,我得到这个错误信息:

 无法使用运算符
classjava.lang.String找到note的值。

我在这里要做的是检查 prevNote currNote 包含相同的 userCode 值 - 如果他们这样做,我想跳过列表并显示具有不同的 userCode 的下一个注意(模拟一个 group-by operation)。



如何做到这一点以及为什么我要这个错误消息是什么?

解决方案 body 中设置值 c :set 将基本上使用对象的 toString()方法将其转换为 String 。这个例外也暗示你并不是在一个完整的 Note <中工作/ code>实例,但在字符串一个。



使用属性。

 < c:set var =prevNotevalue =$ {注意}/> 
< c:set var =currNotevalue =$ {note}/>






至于您的功能要求:


我想要做的是检查prevNote和currNote是否包含相同的userCode值 - 如果他们这样做,我想跳过列表中的当前元素,显示下一个具有不同userCode的注释(类似于模拟分组操作)。

仅在当前存储当前注释的循环就足够了。例如:

 < c:forEach items =$ {notes}var =note> 
< c:if test =$ {empty prevNote or note.userCode!= prevNote.userCode}>
< / c:if>
< / c:forEach>


First I retrieve the Note objects in the service layer:

List<Note> notes = this.getNotes();

Then I pass 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>

Then I iterate over the 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>

But when I run this, I get this error message:

    Unable to find a value for "note" in object of 
    class "java.lang.String" using operator ".

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

Any hints on how to do this and why I'm getting this error message?

解决方案

Setting a value in the body of 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.

Use the value attribute instead.

<c:set var="prevNote" value="${note}" />
<c:set var="currNote" value="${note}" />


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.

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

这篇关于如何在JSTL视图层模拟一个group-by,跟踪前一个&amp;循环列表中的当前值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆