Grails中GSP中的if语句
我在控制器中调用索引方法Grails中GSP中的if语句,grails,Grails,我在控制器中调用索引方法 def index() { childInstance = Child.get(params.id) if(childInstance){ System.out.println("CHILD" + childInstance.firstname) def messages = currentUserTimeline() [profileMessages: messages,childInstan
def index() {
childInstance = Child.get(params.id)
if(childInstance){
System.out.println("CHILD" + childInstance.firstname)
def messages = currentUserTimeline()
[profileMessages: messages,childInstance:childInstance]
} else {
def messages = currentUserTimeline()
[profileMessages: messages]
System.out.println("ALL")
}
}
在普惠制页面中,我有
${childInstance.firstname}
如果我传递一个childInstance,这很好,但是如果我没有,我会得到一个500,因为一个空指针,有没有一种方法我可以在gsp中执行一个if语句,这样我就可以做到这一点
if(childInstance){
${childInstance.firstname}
} else {
All
}
${childInstance.firstName}
您可以使用g:if
、g:elseif
和g:else
:
<g:if test="${name == 'roberto'}">
Hello Roberto!
</g:if>
<g:elseif test="${name == 'olga'}">
Hello Olga!
</g:elseif>
<g:else>
Hello unknown person!
</g:else>
你好,罗伯托!
你好,奥尔加!
你好,陌生人!
比
更简洁的解决方案是使用安全的解引用操作符?
${childInstance?.firstName}
如果childInstance
不为null,则显示第一个名称;如果为null,则不显示任何内容
${childInstance?.firstName}