JSTL c:forEach没有循环到值的判断

事件背景:<table>检索到的数据列表,主表存在关系表的id或者code情况,如订单主表,存在会员id,为了显示更友好,需要根据id显示name。由于前期数据不完善或者存在数据删除情况,导致传统的关联查询如inner join查的数据不全。

解决方式1: 

select a.*,b.name as name from a inner join b where 1=1 and ...
union
select a.*,null as name from a where 1=1 and not exists (select * from b where a.name_id = b.id) and ...

然后entity里赋值name,传入前台,直接显示name

解决方式2:

controller page方法里,增加关联表的list,传入前台

<c:forEach items="${page.list}" var="var" varStatus="vs">
<tr>
    <td>${var.orderId}</td>
    ...
    <c:forEach items="${shoplist}" var="shop">
        <c:if test="${var.shopId == shop.id }">
        <c:set var="count" scope="session" value="1"/>
        <td>${shop.name}</td>
    </c:if>
    </c:forEach>
    <c:if test="${count ne 1}">
        <td></td>
    </c:if>  
    <c:set var="count" scope="session" value="0"/>       
</tr>
</c:forEach>
         

使用<c:set 临时赋值count,表示当前取到了值,然后后面重置count=0,防止count值带到下一条数据。

原文地址:https://www.cnblogs.com/x-jingxin/p/12186838.html