Groovy and Grails Groovy and Grails Web Development & MVC 1 — Questions and Answers
Question 1: In Grails MVC, which artifact is responsible for handling HTTP requests and returning responses?
- Domain class
- Service
- Controller (Correct answer)
- TagLib
Correct answer: Controller
Grails controllers handle incoming HTTP requests, invoke business logic (often via services), and return responses or render views.
Question 2: Which file extension is used for Grails Server Pages (GSP) view templates?
- .groovy
- .gsp (Correct answer)
- .jsp
- .html
Correct answer: .gsp
GSP files use the `.gsp` extension and are Grails's view templating system, supporting Groovy expressions and custom tag libraries.
Question 3: By convention, where does Grails look for views rendered by `MyController`?
- views/my/
- views/myController/
- grails-app/views/my/ (Correct answer)
- web-app/views/my/
Correct answer: grails-app/views/my/
Grails convention places a controller's view files in `grails-app/views/<controllerName>/`, where the controller name is lowercased.
Question 4: What does the `render` method do in a Grails controller action?
- Saves a domain object to the database
- Sends a response to the client (Correct answer)
- Redirects to another URL
- Generates a PDF document
Correct answer: Sends a response to the client
The `render` method in a Grails controller writes a response body — such as a view, text, JSON, or XML — back to the client.
Question 5: Which Grails URL mapping entry would map `/api/products` GET requests to the `ProductController.list` action?
- "/api/products"(controller:'product', action:'list') (Correct answer)
- GET '/api/products' to ProductController
- map("/api/products").to(ProductController.list)
- route("/api/products", 'ProductController#list')
Correct answer: "/api/products"(controller:'product', action:'list')
Grails URL mappings use a DSL where you specify the URL string and a map of controller and action names to define routing.
Question 6: In GSP, which tag is used to iterate over a collection?
- <g:while>
- <g:each> (Correct answer)
- <g:loop>
- <g:forEach>
Correct answer: <g:each>
The `<g:each>` GSP tag iterates over a collection, exposing each element via the `var` attribute.
In Grails MVC, which artifact is responsible for handling HTTP requests and returning responses?