Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Grails-The Web Layer
3.
Controllers
4.
Creating a Controller
5.
Creating Actions
6.
The Default Action
7.
Models and Views
8.
Selecting the View
9.
Selecting Views For Namespaced Controllers
10.
Frequently Asked Questions
10.1.
Is Grails based on MVC?
10.2.
What is the web layer in Grails?
10.3.
In Grails, what do controllers do?
10.4.
Do Grails use Maven?
11.
Conclusion
Last Updated: Jun 30, 2023
Medium

Grails-The Web Layer

Author Adya Tiwari
0 upvote

Introduction

Hey Ninja, Before diving deep into the topic, let's discuss grails first. 

grails the web layer image

Grails is a free and open-source framework for developing online applications that makes use of the Apache Groovy language (which is, in turn, based on the Java platform). By adhering to the "code by convention" philosophy, offering a standalone development environment, and keeping much of the configuration information hidden from the developer, it is meant to be a high-productivity framework.

Grails-The Web Layer

Views and controllers make up the bulk of the web layer. GSPs (Groovy Servers Pages), a JSP (Java Server Pages) extension that can also contain Groovy code, are used to construct views, which are in charge of rendering the user interface. By receiving user actions from the view and responding to them—for example, by interacting directly with the domain model, delegating actions to another controller or a different layer, or redirecting to a different view—controllers manage and organize your program.

Controllers

The response is created or prepared by a controller who manages requests. A controller can produce the answer directly or by delegating to a view. Simply create a class in the grails-app/controllers directory (or a subfolder if it's in a package) with a name ending in "Controller" to build a controller.

controller image

The default URL Mapping setting ensures that each action defined inside your controller maps to URIs within the controller name URI and that the initial portion of your controller name is mapped to a URI.

Creating a Controller

The commands create-controller and generate-controller can be used to create controllers. Try executing the following command, for instance, from the project's root:

grails create-controller book

 

At the address grails-app/controllers/app/Controller.groovy, the command will generate a controller:

package app
class Controller {
    def index() { }
}

 

where "app" is the name that will be used by default if a package name is not given.

By default, Controller maps to the /book URI (relative to your application root).

Creating Actions

Multiple public action methods are possible for a controller. Each one corresponds to a URI:

class BookController {
    def list() {
        return model
    }
}

 

Due to the property's name and list, this example, by default, resolves to the /book/list URI.

The Default Action

A controller can have a default URI corresponding to its root URI, for as /book for the BookController. The following guidelines determine what happens when the default URI is requested:

  • When there is only one option, the default is used.
     
  • The default is the index action if you have it your code.
     
  • An alternative is to explicitly set it using the defaultAction property: static defaultAction = "list."

Models and Views

When rendering, the view uses a model, which is a Map. The variable names the view can access are represented by the keys in that Map. There are several options for returning a model. A Map instance can be explicitly returned first:

def show() {
    [book: Book.get(params.id)]
}

 

Remember that not all variable names are allowed to be used in your model: attributes and application.

Using them won't result in an error notice; perhaps, this will change in a later release of Grails.

Selecting the View

No code indicating which view to render was present in the example above in this section. So how do Grails decide which to choose? Conventions hold the key to the solution. For this show action, Grails will search for a view at the path grails-app/views/book/show.gsp:

class BookController {
    def show() {
         [book: Book.get(params.id)]
    }
}

Selecting Views For Namespaced Controllers

The root directory in which Grails will seek views supplied with a relative path will be affected if a controller defines a namespace for itself using the namespace attribute. For views produced by a namespaced controller, the default root directory is grails-app/views/namespace name>/controller name>/. Grails will switch to looking for the view in the non-namespaced directory if the view cannot be located in the namespaced directory.

Frequently Asked Questions

Is Grails based on MVC?

Like many other web frameworks, Grails utilizes the model-view-controller (MVC) software architecture.

What is the web layer in Grails?

Views and controllers make up the bulk of the web layer. GSPs, a JSP extension that can also contain Groovy code, are used to construct views, which are in charge of rendering the user interface.

In Grails, what do controllers do?

A controller in Grails is a class that resides in the grails-app/controllers directory and has a name that follows the convention "Controller." The command grails create-controller org.example.com can be used to construct a controller. 

Do Grails use Maven?

Yes, The Gradle wrapper will be executed automatically by the maven plugin inside the grails application to build it. The build/libs directory is where the application war will be placed.

Conclusion

We have discussed the web layer in grails. If you want to learn more about this, please visit below topics mentioned.

Have a look at web technologies on Code Studio. Don't stop yourself here. Also, practice data structures and algorithmsinterview questionsDBMScomputer networks, and operating systems to crack the interviews of big tech giants. Explore other fields like machine learningdeep learningcomputer vision, and big data. Also, check out Interview Experiences for different companies.

Happy learning!

Live masterclass