如何检查Gsp中是否存在对象?
<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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不能将其全部包装在另一个测试中以确保列表不为空或为空吗?
Can't you just wrap it all in another test to ensure the list isn't null or empty?