简体   繁体   中英

Need help on groovy syntax

I came across a groovy syntax that creates a link in gsp file like this:

class LoginTagLib {
def loginControl = {
out << """[${link(action:"login",controller:"user"){"Login"}}]"""
}}

I know that it will eventually turned into this in html:

<a href="/racetrack/user/login">Login</a>

However, there are 2 portion of the syntax that I don't understand:

  1. I don't understand ${link(action:"login",controller:"user"){"Login"}} :

    • I get the $() , which is used for string interpolation.
    • I get the link(action:"login",controller:"user") too, just 2 arguments passed into link
    • but what is the {"Login"} doing behind?
  2. I don't understand the """[ ]""" that is used to enclose the whole thing, I tried to take away a pair of " , but it wounldn't work anymore. So it proves to me it has it's significance.

Anybody help to shed some light?

Thanks

  1. In groovy if the last argument of a function is a closure you can you change this syntax foo(arg1, arg2, ..., { ... }) to foo(arg1, arg2, ...) { ... } . this is what happens here, the last argument of link() is a closure that should evaluate to the textual representation of the link
  2. ''' and ''' allow for multi-line string. """ """ are the same but also support variable substitution

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM