Presentation is loading. Please wait.

Presentation is loading. Please wait.

Developing Web-applications with Grails framework

Similar presentations


Presentation on theme: "Developing Web-applications with Grails framework"— Presentation transcript:

1 Developing Web-applications with Grails framework
Presented by Alexey Vedishchev Displaying Data From the Database American University of Nigeria, 2016

2 Problem Statement The most common thing we want to do in Grails is to read data from the database and display it to the user. This post is a simple walk-through on how to accomplish this. How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

3 Solution. Step 1: Create a Domain Class
We first need to create a domain class. For example: class Person { String firstName String lastName int age } You need to save this under grails-app/domain folder. It is advisable to create a package structure under this folder. How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

4 Solution. Step 1: Create a Domain Class
When you start your application, Grails will automatically create a database table for each domain class you have. In our example, the table "person" will be created with columns "first_name", "last_name", and "age". How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

5 Solution. Step 2: Insert Some Data using Bootstrap.groovy
For convenience, there is a class in Grails called Bootstrap.groovy. It is under grails- app/conf folder. When your application starts, Grails will execute the code inside it. You can use this to automatically populate your tables with test data. How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

6 Solution. Step 2: Insert Some Data using Bootstrap.groovy
import myapp.Person class BootStrap { def init = { servletContext -> if (Person.count() == 0) { new Person(firstName: 'John', lastName: 'Doe', age: 20).save() new Person(firstName: 'Jane', lastName: 'Smith', age: 18).save() new Person(firstName: 'Scott', lastName: 'Robinson', age: 42).save() } def destroy = { How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

7 Solution. Step 2: Insert Some Data using Bootstrap.groovy
What the code does is on startup, it will check the number of rows in the person table. If none exists, 3 person records will be inserted. How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

8 Solution. Step 3: Controller Code
You can create a simple controller, read data from the database and pass it to your GSP. class PersonController { def index() { def persons = Person.list() [persons:persons] } How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

9 Solution. Step 3: Controller Code
The code def persons = Person.list() will fetch all records in person table and store it in persons variable. In Groovy, the last statement you have in a method, is the value that will be returned to the caller. In the example above, the index() method will return a map of values. It contains a single object with name "persons", and the value is the list of items read from the database. This map will be passed to your view (GSP). How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

10 Solution. Step 4: View Code
The map of objects passed from the controller will be available inside your view: <!DOCTYPE html> <html> <head> <meta name="layout" content="main"/> <title>Persons</title> </head> <body> <g:each in="${persons}" var="person" status="i"> <h3>${i+1}. ${person.lastName}, ${person.firstName}</h3> <p> Age: ${person.age} </p> <br/> </g:each> </body> </html> How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.

11 Remarks Our simple example above will have the following output:
How presentation will benefit audience: Adult learners are more interested in a subject if they know how or why it is important to them. Presenter’s level of expertise in the subject: Briefly state your credentials in this area, or explain why participants should listen to you.


Download ppt "Developing Web-applications with Grails framework"

Similar presentations


Ads by Google