2012-08-30 15 views
12

मैं एक नियंत्रकअगर Grails में एक जीएसपी में बयान

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 नहीं मिलता है वहाँ एक रास्ता मैं एक एक जीएसपी में बयान तो मैं क्या कर सकते हैं अगर यह

if(childInstance){ 
    ${childInstance.firstname} 
} else { 
    All 
} 

उत्तर

35

आप 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> 
3
<g:if test="${ childInstance }"> 
    ${ childInstance.firstName } 
</g:if> 
क्या कर सकते हैं
4

<g:if> से एक अधिक संक्षिप्त समाधान अगर childInstance नहीं है सुरक्षित-भिन्नता ऑपरेटर ?

${childInstance?.firstName} 

उपयोग करने के लिए पहले नाम प्रदर्शित करेगा है शून्य और अगर यह शून्य है तो कुछ भी प्रदर्शित नहीं करें।

संबंधित मुद्दे