Question

request.withFormat {
            form {
                redirect humanInstance
                //Explain : old grails redirect like   redirect(action: "show", id: humanInstance.id) 
                //but in 2.3.0 by  redirect humanInstance  this statement i saw it goes to  def show(Human humanInstance)  action
                //Q1: how it understand that it has to redirect  def show(Human humanInstance) action ?
                //cause here so many methods which receives humanInstance
                //Q2: And where it get processed ?
            }
            '*' {
                respond humanInstance, [status: CREATED]
            }
            //Explain : I tested it that in the above {works for normalCall}'*'{works for restCall}
            //Q3: What is '*' operator do here what it's name?
        }
Was it helpful?

Solution

The '*' is kind of a catch-all which is associated with a block of code that should execute if none of the other blocks matched the accept header. See the following...

request.withFormat {
    xml {
        // execute this if the request content type is xml
    }
    json {
        // execute this if the request content type is json
    }
    '*' {
        // execute this if neither of the other 2 matched the request's content type
    }
}

There is quite a bit that you probably want to understand around this including the mime types that you have configured. You should look at the Content Negotiation section of the official user guide at http://grails.org/doc/2.3.x/guide/theWebLayer.html#contentNegotiation.

I hope that is helpful.

UPDATE:

When you call redirect(someDomainClassInstance) you end up at https://github.com/grails/grails-core/blob/v2.3.8/grails-plugin-controllers/src/main/groovy/org/codehaus/groovy/grails/plugins/web/api/ControllersApi.java#L270

That calls https://github.com/grails/grails-core/blob/v2.3.8/grails-plugin-controllers/src/main/groovy/org/codehaus/groovy/grails/plugins/web/api/ControllersApi.java#L248

That calls https://github.com/grails/grails-core/blob/v2.3.8/grails-plugin-controllers/src/main/groovy/org/codehaus/groovy/grails/web/metaclass/RedirectDynamicMethod.java#L160

That calls https://github.com/grails/grails-core/blob/v2.3.8/grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/DefaultLinkGenerator.groovy#L175.

At that point the value of "method" is "GET_ID". The value in https://github.com/grails/grails-core/blob/v2.3.8/grails-web/src/main/groovy/org/codehaus/groovy/grails/web/mapping/DefaultLinkGenerator.groovy#L56 which is associated with GET_ID is "show" and that is what dictates that you will redirect to the show method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top