For retrieving all data from database table, normally use list() method. So index() method will be
def contacts = Contact.list() render(view: "index", model: [contacts: contacts])
For data showing, index view code will be
<table class="table table-bordered"> <tr> <th>Name</th> <th>Email</th> <th>City</th> <th>Country</th> <th>Action</th> </tr> <g:each in="${contacts}" var="contact" status="i"> <tr> <td>${i + 1}. ${contact.lastName}, ${contact.firstName}</td> <td>${contact.email}</td> <td>${contact.city}</td> <td>${contact.country}</td> <td><g:link controller="contact" action="view" id="${contact.id}">Details</g:link></td> </tr> </g:each> </table>
For individual detail view, we can retrieve data using get(id) where id is specific record Id.
def contact = Contact.get(1)
This uses the get method that expects a database identifier to read the Contact object back from the database. You can also load an object in a read-only state by using the read method:
Contact
def contact = Contact.read(1)
In this case the underlying Hibernate engine will not do any dirty checking and the object will not be persisted. Note that if you explicitly call the save method then the object is placed back into a read-write state.
In addition, you can also load a proxy for an instance by using the load method:
def contact = Contact.load(1)
This incurs no database access until a method other than getId() is called. Hibernate then initializes the proxied instance, or throws an exception if no record is found for the specified id.
Or simply you can do as like bellow when it come from link where first parameter must be a ID
def show(Contact contact) { respond contact }
and show view content as like
<table class="table table-bordered"> <tr><th>Name</th> <td> ${contact.lastName}, ${contact.firstName}</td></tr> <tr><th>Email</th> <td>${contact.email}</td></tr> <tr><th>City</th> <td>${contact.city}</td></tr> <tr><th>Country</th><td>${contact.country}</td></tr> <tr><th>Message</th><td>${contact.message}</td></tr> </tr> </table> <h4><g:link controller="contact" action="index" >Contact List </g:link> </h4>
For more - https://docs.grails.org/latest/guide/GORM.html