Developing a Sample Website using Grails Framework
Hello again! :)
Today I'm showing you the most basic things we can do with Grails. In last post we installed Grails and now let's find out how to create a small sample website using it.
Open cmd prompt or terminal and navigate to a place where you would like to create your first grails app. For my convenience I create my app inside the grails folder I used to install grails in last post.
If you go inside the demoapp folder you will see following folder structure.
So let's start coding!
Run following command in cmd/terminal. You should be located inside the created demoapp directory. Mine is C:\grails\demoapp
Now if you go to your demoapp/grails-app/domain directory you will see there is a new folder created named demoapp (same name as our app). Inside that folder there will be a new Student.groovy file. that is the domain class we just created. If you open it with a text editor you'll see a code like this.
We'll create two more domain classes to demonstrate our app.
Let's modify the code now.
Student.groovy
School.groovy
Subject.groovy
After coding the demo classes you should create the controllers and the views. In order to do that you can use "generate-all" command (refer documentation to learn the other available commands).
After doing above steps you will see in the project folder that the new controller files and views have been created with default methods and layouts. :)
Finally, when you run the app your first page will be like this.
If you click on the links and check out the inside of the app you will see the required forms and lists and all other spices have been already added by Grails itself. :)
Since Grails is built on Java technology setting up a data source requires a JDBC driver. If you use a database other than H2 you'll need that respective driver. For example for MySQL we would need Connector/J.
So download the latest Connector/J (I used: mysql-connector-java-5.1.21.zip) and extract the JAR file (Mine is: mysql-connector-java-5.1.21-bin.jar) to demoapp/lib directory.
Create a new database called demoapp using MySQL. Don't create the tables, grails will do that for you :)
Now change the DataSource.groovy file in demoapp/grails-app/conf folder as follows (Remember these are only the minimum changes required. You can change other things also. But be knowledgeable before changing anything.)
Then uncomment the mysql dependency in the grails-app/conf/BuildConfig.groovy file to as follows.
Go back to the command line and type "grails clean". Then run the app.
Now check your MySQL databases using command line tool or phpMyAdmin or even MySQL workbench. You will see that grails crated the database structure for you.
So from now on when you save data using the application those data will be saved permanently in your database.
You can go on developing your website like this. Main things we do when creating our own site are:
So major things regarding starting off a fresh web site using Grails Framework is done here. But you have lot more to do if you are developing a real world product.
Today I'm showing you the most basic things we can do with Grails. In last post we installed Grails and now let's find out how to create a small sample website using it.
1. Creating demoapp
Open cmd prompt or terminal and navigate to a place where you would like to create your first grails app. For my convenience I create my app inside the grails folder I used to install grails in last post.
grails create-app demoappThis command will create an app called "demoapp". After creating you will see a url like this - http://localhost:8080/demoapp/ in the command prompt (or the terminal). Type the url in the browser and you will see a page like following.
Default Grails App Home Page |
So let's start coding!
2. Creating domain classes
Run following command in cmd/terminal. You should be located inside the created demoapp directory. Mine is C:\grails\demoapp
grails create-domain-class Student
package demoapp class Student { static constraints = { }
We'll create two more domain classes to demonstrate our app.
grails create-domain-class School grails create-domain-class Subject
Let's modify the code now.
Student.groovy
package demoapp class Student { String firstName String lastName School school Date created static hasMany = [subjects:Subject] static belongsTo = School static constraints = { firstName(nullable:false) lastName(nullable:false) school(nullable:false) created(nullable:false) } }
School.groovy
package demoapp class School { String name String address Date created static hasMany = [students:Student, subjects:Subject] static constraints = { name(nullable:false) address(nullable:false) created(nullable:false) } }
Subject.groovy
package demoapp class Subject { String subjectCode String subjectName Date created static belongsTo = [school:School] static constraints = { subjectCode(nullable:false) subjectName(nullable:false) created(nullable:false) } }
3. Creating controllers and views
After coding the demo classes you should create the controllers and the views. In order to do that you can use "generate-all" command (refer documentation to learn the other available commands).
grails generate-all demoapp.School grails generate-all demoapp.Subject grails generate-all demoapp.Student
After doing above steps you will see in the project folder that the new controller files and views have been created with default methods and layouts. :)
Finally, when you run the app your first page will be like this.
If you click on the links and check out the inside of the app you will see the required forms and lists and all other spices have been already added by Grails itself. :)
3. Configuring our application to MySQL database
Since Grails is built on Java technology setting up a data source requires a JDBC driver. If you use a database other than H2 you'll need that respective driver. For example for MySQL we would need Connector/J.
So download the latest Connector/J (I used: mysql-connector-java-5.1.21.zip) and extract the JAR file (Mine is: mysql-connector-java-5.1.21-bin.jar) to demoapp/lib directory.
Create a new database called demoapp using MySQL. Don't create the tables, grails will do that for you :)
Now change the DataSource.groovy file in demoapp/grails-app/conf folder as follows (Remember these are only the minimum changes required. You can change other things also. But be knowledgeable before changing anything.)
dataSource { pooled = true driverClassName = "com.mysql.jdbc.Driver" username = "root" password = "" } hibernate { cache.use_second_level_cache = true cache.use_query_cache = false cache.region.factory_class = 'net.sf.ehcache.hibernate.EhCacheRegionFactory' } // environment specific settings environments { development { dataSource { dbCreate = "update" // one of 'create', 'create-drop', 'update', 'validate', '' url = "jdbc:mysql://localhost:3306/demoapp" } } test { dataSource { dbCreate = "update" url = "jdbc:mysql://localhost:3306/demoapp" } } production { dataSource { dbCreate = "update" url = "jdbc:mysql://localhost:3306/demoapp" pooled = true properties { maxActive = -1 minEvictableIdleTimeMillis=1800000 timeBetweenEvictionRunsMillis=1800000 numTestsPerEvictionRun=3 testOnBorrow=true testWhileIdle=true testOnReturn=true validationQuery="SELECT 1" } } } }Also remember here I have used the same url for all 3 environments. But in a real environment you would probably want to define a different database for each one or at least for production use.
Then uncomment the mysql dependency in the grails-app/conf/BuildConfig.groovy file to as follows.
... dependencies { // specify dependencies here under either 'build', 'compile', 'runtime', 'test' or 'provided' scopes eg. runtime 'mysql:mysql-connector-java:5.1.21' } ...
Go back to the command line and type "grails clean". Then run the app.
grails clean grails run-app
Now check your MySQL databases using command line tool or phpMyAdmin or even MySQL workbench. You will see that grails crated the database structure for you.
So from now on when you save data using the application those data will be saved permanently in your database.
You can go on developing your website like this. Main things we do when creating our own site are:
- Change the css files (style the site as our need)
- Change the design according to our requirements
- Change the application logic
- Use appropriate plugins to get the work done
- etc.
So major things regarding starting off a fresh web site using Grails Framework is done here. But you have lot more to do if you are developing a real world product.
I hope to cover some more areas I have already touched in Grails in the future. Till then, Happy Coding!!! /bye
Comments
Post a Comment