0
votes

Whats is the advantage of using respond() over render() in grails 2.xxx ?

I found respond() when I tried to generate the controller and views of a domain.

like in create() it has code like :

respond new DomainObject(params);

also I found request.withFormat(), a little bit strage to me

 droppingInstance.save flush:true;
request.withFormat {
            form multipartForm {
                flash.message = message(code: 'default.created.message', args: [message(code: 'dropping.label', default: 'Dropping'), droppingInstance.id])
                redirect droppingInstance
            }
            '*' { respond droppingInstance, [status: CREATED] }
        }

I read the doc already but found no clue about multiplatform etc ... http://grails.org/doc/latest/ref/Controllers/request.html http://grails.org/doc/latest/ref/Controllers/respond.html

also why not using redirect to display the data ?

could someone explain both methods please ? :)

1

1 Answers

1
votes

Whats is the advantage of using respond() over render() in grails 2.xxx ?

It isn't the case that either respond or render are better than the other. They are 2 different methods that are useful for different things. The big difference is that respond is quite useful when building REST interfaces and you want to respond with different content based information that is in the request header (for example).

As an example, if your controller action does something like this:

respond new DomainObject(params)

You haven't expressed there what format the response should be. That might result in a GSP being rendered, it might result in the domain instance being serialized as JSON, or XML or some other format. That is really flexible and helpful when building REST interfaces which need to support multiple content types and you don't want to muck up the controller action with a bunch of imperative logic to deal with those content types. Details about that are available at http://grails.org/doc/latest/guide/single.html#REST and http://grails.org/doc/latest/ref/Controllers/respond.html.

also I found request(), a little bit strage to me

In what context did you see a call to request()?

also why not using redirect to display the data ?

Because redirect doesn't display data. Redirect causes the browser to redirect somewhere else. That somewhere else might display data with render, respond or do whatever it needs to do but all the redirect does is says "go somewhere else".