何时在 Grails 框架的标记属性值中使用表达式 ${}?
when to use expression ${} in tag attributes values in Grails framework?
我想知道我怎样才能知道什么时候用表达式 ${} 传递属性的值,什么时候不传递。
例如,在下一个标记调用中,为什么 template
和 model
属性值在没有 ${} 的情况下传递,而 collection
属性值通过 ${} 传递。
<g:render template="displaybook" model="['book':book,'author':author]" collection="${books}" />
我想学习如何阅读本主题中的 grails 标签文档。
${}
只用于需要求值的表达式,不用于字符串文字。
<g:render template="displaybook" model="['book':book,'author':author]" collection="${books}" />
"displaybook"是文字串,不是变量引用,所以放在${}
.
中没有意义
"books" 是变量引用,所以放在 ${}
.
中确实有意义
传递像 ['book':book,'author':author]
这样的文字集合时有特殊处理。解析器将识别周围的 [...]
并将内容评估为表达式,而不是字符串文字,因此那里不需要 ${}
。
希望对您有所帮助。
看,如果如上面的答案所说,您使用 ${} 来评估某些东西,例如:“${book.author.name.toString()}”,将 return "Stephen King" 例如,但是如果你这样写 "book.author.name.toString()" 它只会给你 "book.author.name.toString()" 作为结果。另外,符号[something: 4, otherSomething: "what up"]和java中的这个是一样的:
LinkedTreeMap<String,Object> map = new LinkedTreeMap<String,Object>();
map.put("something",4);
map.put("otherSomething","whats up");
这只是创建地图的一种非常简单的方法,无需所有铸造工作和其他东西(这就是 groovy 如此酷的原因之一)。
您也可以这样做:
def map = [:]
public void getObject(key){
if(map."${key}")
println(map."${key}") //this will print the object not the key, getObject("mobile") would print cellphone not "mobile" understand? That's awsome!
}
这意味着您可以使用来自变量 =D 的运行时密钥。
就是这些希望对你有所帮助
我想知道我怎样才能知道什么时候用表达式 ${} 传递属性的值,什么时候不传递。
例如,在下一个标记调用中,为什么 template
和 model
属性值在没有 ${} 的情况下传递,而 collection
属性值通过 ${} 传递。
<g:render template="displaybook" model="['book':book,'author':author]" collection="${books}" />
我想学习如何阅读本主题中的 grails 标签文档。
${}
只用于需要求值的表达式,不用于字符串文字。
<g:render template="displaybook" model="['book':book,'author':author]" collection="${books}" />
"displaybook"是文字串,不是变量引用,所以放在${}
.
"books" 是变量引用,所以放在 ${}
.
传递像 ['book':book,'author':author]
这样的文字集合时有特殊处理。解析器将识别周围的 [...]
并将内容评估为表达式,而不是字符串文字,因此那里不需要 ${}
。
希望对您有所帮助。
看,如果如上面的答案所说,您使用 ${} 来评估某些东西,例如:“${book.author.name.toString()}”,将 return "Stephen King" 例如,但是如果你这样写 "book.author.name.toString()" 它只会给你 "book.author.name.toString()" 作为结果。另外,符号[something: 4, otherSomething: "what up"]和java中的这个是一样的:
LinkedTreeMap<String,Object> map = new LinkedTreeMap<String,Object>();
map.put("something",4);
map.put("otherSomething","whats up");
这只是创建地图的一种非常简单的方法,无需所有铸造工作和其他东西(这就是 groovy 如此酷的原因之一)。
您也可以这样做:
def map = [:]
public void getObject(key){
if(map."${key}")
println(map."${key}") //this will print the object not the key, getObject("mobile") would print cellphone not "mobile" understand? That's awsome!
}
这意味着您可以使用来自变量 =D 的运行时密钥。
就是这些希望对你有所帮助