4

Some of the Grails conventions for passing data from the controller to the view I find a bit opaque notwithstanding reading the documentation. A few basic questions:

Here are some questions:

1) in the scaffolding created for a 'Person' controller, the index action ends with

respond Person.list(params) model:[personCount: Person.count()]

In in index.gsp, the list is rendered to the browser using:

<f:table collection="${personList}" />

My question is, where did this personList variable come from? Or put another way, how did the output of Person.list(...) in the controller show up in the view with the name personList? Is there a generalizable rule about if you call

respond foo

and foo is a list, then it will show up in the controller under the name "fooList"?

2) If you can provide a object to the controller just by saying "render foo", what is the purpose of the model parameter? i.e. is there some difference between

respond foo

and

respond model:[foo: foo]

It seems like both would be accessible in the view using "${foo}"

3) Finally, what is a concise description of the difference between "respond" and "render"? They both seem to deliver data from the controller to the view.

GGizmos
  • 3,443
  • 4
  • 27
  • 72
  • 1
    If its any consolation, I also find respond very arcane, in that it does internal magic and you don't really have any idea whats going on or how to use if your case doesn't exactly follow an example. E.g. I want to respond with two objects, and I have no idea how to do this as the documentation says you can only pass a single object as a parameter (+ an argument parameter). If you need to use the argument parameter for passing additional objects, or instead of object if you have two, it makes it confusing. I read the respond documentation and decided render would save a lot of time debugging – John Little Jul 16 '17 at 11:18
  • Interestingly, I have written hundreds of JSON APIs, and thousands of UI controllers in Grails. I have almost never had the situation where there is overlap between the API controller methods and output and the UI controller methods, so respond would not help in my cases. – John Little Jul 16 '17 at 11:20

2 Answers2

3

So, the documentation does a very good job of explaining both respond and render and points out the differences between the two. Specifically the questions you asked.

However, I'll answer them here anyway:

  1. When using respond it will attempt to determine the appropriate model variable name based on the type. So for your example, since Person.list() returns a List the model variable becomes personList and in your other example if foo is a List it does become fooList. This is explained in the documentation about respond (with specific examples).
  2. respond and render are diffrent in that respond attempts to reply with the most appropriate model based on the accept header (or file extension). Where as render allows you more specific control to render the response regardless of the accept header (or file extension). This also is made clear in the description of both in the documentation.
  3. The concise description of the differences are found in the description of both respond and render (from the documentation):

Respond: Attempts to return the most appropriate type for the requested content type (specified by either the Accept header or file extension)

and

Render: To render different forms of responses from simple text responses, to view and templates.

To put this simply: "Use respond when you want to support many types of response types and follow the Grails convention, and use render when you want more specific control of the response type.

Joshua Moore
  • 24,706
  • 6
  • 50
  • 73
  • Thanks Joshua. The page I was seeing was the one returned by a Google search for 'Grails respond' which links to the documentation for version 3.1.1 which is quite sketchy. The new page is much improved. – GGizmos Nov 28 '16 at 03:28
0

My question is, where did this personList variable come from?

The respond method is adding the list of Person to the model and by default will generate the model variable name from the Person class name.

Jeff Scott Brown
  • 26,804
  • 2
  • 30
  • 47