Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
968 views
in Technique[技术] by (71.8m points)

groovy - What does the question mark mean in GSP/Grails?

I saw this in my generated GSP pages. What does the ? mean?

<g:textField name="name" value="${phoneInstance?.name}" />
question from:https://stackoverflow.com/questions/4581532/what-does-the-question-mark-mean-in-gsp-grails

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

It's the "Safe Navigation Operator", which is a Groovy feature that concisely avoids null pointer exceptions. See http://docs.groovy-lang.org/latest/html/documentation/index.html#_safe_navigation_operator

In this case, if phoneInstance is null, then it doesn't try to get the name property and cause a NPE - it just sets the value of the field tag to null.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...