如何检查Gsp中是否存在对象?

发布于 2024-12-03 20:03:46 字数 1282 浏览 1 评论 0原文

<g:each in="${clientTripInstance?.startPointJob?.passengerActivities}" status="i" var="startPassengerActivity">
     <g:if test = "${startPassengerActivity?.passengerCount > 1}">
         <p> ${startPassengerActivity?.activity}  ${startPassengerActivity?.passengerRole?.displayName} (${startPassengerActivity?.passengerCount})
     </p>
     </g:if>
     <g:else>
       <p> ${startPassengerActivity?.activity}  ${startPassengerActivity?.passengerRole?.displayName}
          </p>
     </g:else>
</g:each>

当 clientTripInstance?.startPointJob?.passengerActivities 存在时,此代码工作正常。在某些情况下,clientTripInstance?.startPointJob?.passengerActivities 不存在...那么如何检查不存在?如果它不存在,我不想显示任何内容..

域类是

   class Trip {
            String notes
           List<PointJob> pointJobs = new ArrayList<PointJob>()
   }

   class PointJob {
       Point point
   List<PassengerActivity> passengerActivities = new ArrayList<PassengerActivity>();
   }


  class PassengerActivity {
    PassengerRole passengerRole;
    String activity;
    int passengerCount;

static constraints = {
    passengerRole()
    activity()
    passengerCount(nullable:true)
}

}

<g:each in="${clientTripInstance?.startPointJob?.passengerActivities}" status="i" var="startPassengerActivity">
     <g:if test = "${startPassengerActivity?.passengerCount > 1}">
         <p> ${startPassengerActivity?.activity}  ${startPassengerActivity?.passengerRole?.displayName} (${startPassengerActivity?.passengerCount})
     </p>
     </g:if>
     <g:else>
       <p> ${startPassengerActivity?.activity}  ${startPassengerActivity?.passengerRole?.displayName}
          </p>
     </g:else>
</g:each>

this code works fine when clientTripInstance?.startPointJob?.passengerActivities is exist.. there are some case when clientTripInstance?.startPointJob?.passengerActivities is not exist... so how to check is not exist ? i dont want to display anything if its not exist..

the domain class are

   class Trip {
            String notes
           List<PointJob> pointJobs = new ArrayList<PointJob>()
   }

   class PointJob {
       Point point
   List<PassengerActivity> passengerActivities = new ArrayList<PassengerActivity>();
   }


  class PassengerActivity {
    PassengerRole passengerRole;
    String activity;
    int passengerCount;

static constraints = {
    passengerRole()
    activity()
    passengerCount(nullable:true)
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千里故人稀 2024-12-10 20:03:46

您不能将其全部包装在另一个测试中以确保列表不为空或为空吗?

<g:if test="${clientTripInstance?.startPointJob?.passengerActivities}">
  <g:each in="${clientTripInstance?.startPointJob?.passengerActivities}" status="i" var="startPassengerActivity">
    <g:if test = "${startPassengerActivity?.passengerCount > 1}">
      <p>
        ${startPassengerActivity?.activity}  ${startPassengerActivity?.passengerRole?.displayName} (${startPassengerActivity?.passengerCount})
      </p>
    </g:if>
    <g:else>
      <p>
        ${startPassengerActivity?.activity}  ${startPassengerActivity?.passengerRole?.displayName}
      </p>
    </g:else>
  </g:each>
</g:if>

Can't you just wrap it all in another test to ensure the list isn't null or empty?

<g:if test="${clientTripInstance?.startPointJob?.passengerActivities}">
  <g:each in="${clientTripInstance?.startPointJob?.passengerActivities}" status="i" var="startPassengerActivity">
    <g:if test = "${startPassengerActivity?.passengerCount > 1}">
      <p>
        ${startPassengerActivity?.activity}  ${startPassengerActivity?.passengerRole?.displayName} (${startPassengerActivity?.passengerCount})
      </p>
    </g:if>
    <g:else>
      <p>
        ${startPassengerActivity?.activity}  ${startPassengerActivity?.passengerRole?.displayName}
      </p>
    </g:else>
  </g:each>
</g:if>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文