Presentation is loading. Please wait.

Presentation is loading. Please wait.

Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Form Submission And Saving Data To.

Similar presentations


Presentation on theme: "Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Form Submission And Saving Data To."— Presentation transcript:

1 Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Form Submission And Saving Data To Database

2 HTML Forms are usually used to gather data from users. This post will show how to make use of forms to save data to the database. This example will render a form that looks like this: And on click of Save, will save the data to the person table. Case Statement 2

3 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. Solution. Step 1: Create a Domain Class 3

4 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". Solution. Step 1: Create a Domain Class 4

5 This is an example of a view page code (form.gsp): Form First Name: Last Name: Age: Solution. Step 2: View Code 5

6 The form tag above specifies that on submit, the data will be passed to the save method/action of the person controller (PersonController.groovy). The rest is self explanatory. Solution. Step 2: View Code 6

7 Below is an example controller code to display the page above, and handle form submission: package com.myapp class PersonController { def form() { } def save() { def person = new Person(params) person.save() render "Success!" } Solution. Step 3: Controller Code 7

8 The first method form() will display form.gsp when the user accessed the url http://yourmachine:8080/yourapp/person/form The second method will accept the submitted data from the HTML form. The code new Person(params) will create a Person instance, and automatically map data from the form to the instance by property name. Solution. Step 3: Controller Code 8

9 Since the form has the property firstName via the code, the instance returned by new Person(params) will populate the firstName property from the data in the form. This is also true for the other fields lastName and age. The code person.save() will save the data to the database, while render "Success!" will display the text to the browser as is. Solution. Step 3: Controller Code 9

10 This is just a simple example to understand how data is passed from HTML, to controllers, and then to the database. Remarks 10


Download ppt "Presented by Alexey Vedishchev Developing Web-applications with Grails framework American University of Nigeria, 2016 Form Submission And Saving Data To."

Similar presentations


Ads by Google