GSP / Grails中的问号意味着什么? [英] What does the question mark mean in GSP/Grails?

查看:74
本文介绍了GSP / Grails中的问号意味着什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我生成的GSP页面中看到了这一点。 是什么意思?

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

<g:textField name="name" value="${phoneInstance?.name}" />


推荐答案

这是安全导航运算符,它是Groovy功能简洁地避免了空指针异常。请参阅 http://docs.groovy-lang.org/latest/html/documentation/index .html#_safe_navigation_operator

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

在这种情况下,如果 phoneInstance 为空,那么它不会尝试获取名称属性并导致NPE - 它只是将字段标记的值设置为null。

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.

这篇关于GSP / Grails中的问号意味着什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆