To pass data from a controller to a view we must return a model. The model is a map with all the values we want to show on the Groovy Server Page (GSP). We can explicitly return a model from an action, but if we don't do that the controller's properties are passed as the model to the view. Remember that in Grails a new controller instance is created for each request. So it is save to use the properties of the controller as model in our views.
// File: grails-app/controllers/com/mrhaki/SampleController.groovy
package com.mrhaki
class SampleController {
def values = ['Grails', 'Groovy', 'Griffon', 'Gradle', 'Spock']
def greeting
def index = {
greeting = 'Welcome to My Blog'
// Don't return a model, so the properties become the model.
}
}
<%-- File: grails-app/views/sample/index.gsp --%>
<html>
<head>
</head>
<body>
<h1>${greeting}</h1>
<g:join in="${values}"/> rock!
</body>
</html>