This guide initially follows the Grails documentation almost identically but then derails into a path intended to help you, not only build a more robust application in Grails quicker but also give you a rapid understanding of the structure of a Grails application from a birds eye view.
$ <- This dollar sign denotes a bash shell, meaning anything that follows it is supposed to be executed in a terminal aka command prompt, If you didn’t know.
So all routes to either install Java or make sure its installed and then the JDK 11 All routes to making sure JDK 11 is your systems default Note: Most Unix/Linux based systems have Java and likely the JDK installed out the gate
Install an older version of the JDK, JDK 11
$ sudo pacman -Sy jdk11-openjdk --noconfirm
Change the default JDK version used by the system
$ sudo archlinux-java set java-11-openjdk
Otherwise follow some other method through the documentation Here
$ curl -s "https://get.sdkman.io" | bash
$ source "$HOME/.sdkman/bin/sdkman-init.sh"
$ sdk install grails
$ mkdir myapp
$ cd myapp
$ grails create-app myapp --servlet=tomcat
The “myapp” part can be renamed to whatever you want your app to be, actually called. The
--servlet=tomcat
command flag part : (Lets break this down)
- Tomcat is a web server that puts things into a container called a servlet.
This creates a smaller packaged environment which contains all of the things needed for the application to run without pulling bulky resources from several different places.
This concept drastically decreases the amount of time you can get an application up and running while it is being developed.
The Grails Interactive Console
Just in case you ever need to access it for whatever reason
Note:When you run this command make sure your in either a grails app directory or a place you want to make a new grails app
if you plan on doing anything useful that is.
$ grails
From inside of the interactive console:
$ help
$ exit
Run a Grails app
In the same folder the gradlew file is located (Should be the root app folder)
$ ./gradlew bootRun
$ ./gradlew bootRun --D grails.server.port=9090
If all went well!
If all goes well you should be able to open a web browser and navigate to http://localhost:8080 or whatever port you used in place of 8080
At a glance
After you create a Grails app these files and folders get created
The bulk of your development in Grails
- Your work will largely be focused in the grails-app folder.
These are the folders inside the grails-app folder
To start simple We will start by focusing on the:
- Assets Folder
- Controllers Folder
- Views Folder
The assets folder
Contains what are commonly referred to as static files
- This is where you are going to put your CSS, JavaScript and Images associated with your application
- Grails is going to know to look here when you reference this stuff in your HTML/(.gsp) files
This is how you reference your CSS in your HTML/(.gsp) files
This is how you reference your JavaScript in your HTML/(.gsp) files
The controllers folder
Controllers deal with
- Requests
- This is where you can create logic responsible for handling data to and from the browser
- Controllers render data that can be used in .gsp files through a language that is called “view tags” which we will get into later
To create a new controller enter the Grails interactive console
$ grails
Then from inside the interactive console:
$ create-controller greeting
The views folder
Views contains
- All of your applications HTML inside of individual .gsp files
- To create a new .gsp file
- Go into the views folder and create a new file with the .gsp extension.
- Then add some html in it
Layouts
The layouts folder inside of the views folder:
- This is where you keep .gsp/HTML files which contain reusable code you want to be able to access in other .gsp/HTML files
- The concept of the layouts folder is similar to the idea of HTML partials.
Database Interaction
(Still fuzzy on this need to work on this section later)
Grails Domain Class:
- A way of creating data models which interact with a database.
$ grails
$ create-domain-class yourmodelname
In terms of Grails once it recieves a request:
- The application looks here in this file:
Which will look something like this:
If the URL doesnt contain a /
- Ex.
- coolsite.com and not coolsite.com/about
Grails looks in the UrlMappings.groovy file for an entry that matches the URL in the request.
In this case if the url does not contain anything extra after .com
Grails associates that as single forward slash /
So it looks for an entry with a single forward slash
Then once Grails finds a match:
- Grails looks at the thing next to the match
- In this case the thing next to the forward slash
Then Grails understands it needs to look inside of the views folder for a .gsp file named index
Once Grails finds the .gsp file it renders it to the browser.
Most web frameworks use HTML out the box for web pages.
Grails uses .gsp files which are located in the views folder
.gsp files are almost equivalent to HTML files
They contain regular HTML but also have the ability to interpret other syntax which we will get into later.
If you wanted to stick with regular HTML inside a .gsp file you could though, without an issue.
Make more pages
EX.
- views/test.gsp
Navigate to controllers/myapp/
You just successfully registered a new view
Consider the following
When you create a new controller in grails
- You give the controller a name: ie. ContactList
- However you create the controller, it should be located:
- appname/grails-app/controllers/contactlist/ContactList.groovy
package contactlist
def index() {
}
The controller should have a couple things inside of it
- A package which should be the same name of the directory that the file is sitting in
- A default index method
def index
The index method is saying whenever someone navigates to mycoolsite