Table of contents
1.
Introduction
2.
What is Grails?
3.
Scaffolding
3.1.
Dynamic Scaffolding
3.2.
Static Scaffolding
3.3.
Customizing the Generated Views
3.4.
The Field Plugins
4.
Frequently Asked Questions
4.1.
What is Grails?
4.2.
What are the Features of Grails?
4.3.
What is scaffolding in Grails?
4.4.
How does the Grails framework operate?
4.5.
Is Grails an MVC framework?
5.
Conclusion
Last Updated: Mar 27, 2024
Medium

Grails-Scaffolding

Author Manan Singhal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

Hey Ninjas! As your companion in your grails learning journey, We present another article on web applications using Grails to you. The web application is a program stored on a remote server and delivered over the Internet through a browser interface. Grails is an open-source web application framework that uses the Apache Groovy programming language.

Grails-Scaffolding

In the article, we will learn about scaffolding in Grails. But, before that let's understand a little about grails.

What is Grails?

Grails is a framework for high productivity. Grails was initially known as Groovy on Rails when it was first released in 2005, but the name was changed in 2006 due to a disagreement with the inventor of Ruby on Rails.

Grail's purpose was to create a Java web framework by combining technologies such as Hibernate and Spring into a single interface.

Grails

In the end, the programmers produced a robust web application framework for the Java Virtual Machine built on top of Groovy and Spring Boot. Now, let's understand scaffolding.

Scaffolding

For a domain class, scaffolding enables you to create some straightforward CRUD interfaces, such as:

  • The required views
  • Controller actions for create/read/update/delete CRUD operations.
     
Scaffolding

The following code must be added to build.gradle for an application to declare its dependence on the scaffolding plugin.

dependencies {
	compile "org.grails.plugins:scaffolding"
}

Dynamic Scaffolding

Setting the scaffold property in the controller to a particular domain class enables scaffolding:

class AboutAuthor {
    static scaffold = Author
}
You can also try this code with Online Java Compiler
Run Code

 

With this, you can start your application, and the actions and views will be autogenerated at runtime. The index, show, edit, delete, create, save, and runtime scaffolding mechanism dynamically implements update actions.

The old definition of scaffold property is no longer supported in Grails 3.0 and above.

You may still utilize scaffolding if you maintain your domain model in Java and map it with Hibernate and import the domain class, and set its name as the scaffold argument.

A scaffolded controller can be expanded with new actions, for instance:

class AboutAuthor {

    static scaffold = Author

    def changeAge() {
        def b = Author.get(params.id)
        b.age = Author.get(params["author.age"])
        b.save()

        // redirect to a scaffolded action
        redirect(action:show)
    }
}
You can also try this code with Online Java Compiler
Run Code

 

class AboutAuthor {

    static scaffold = Author

    def index() {
        [aboutAuthorList: Author.list(),
        aboutAuthorTotal: Author.count()]
    }

    def show() {
        def author = Author.get(params.id)
        log.error("{}", author)
        [aboutAuthor : author]
    }
}
You can also try this code with Online Java Compiler
Run Code


This is called dynamic scaffolding, in which the CRUD interface is dynamically constructed at runtime.

Static Scaffolding

From the command line, Grails enables you to produce the controller and views required to build the interface seen above.

Creating a controller type:

grails generate-controller Author


Or to create views:

grails generate-views Author


Or produce everything:

grails generate-all Author

Customizing the Generated Views

The views adjust to the validation restrictions. For instance, by rearranging the constraints in the builder, you can alter the order in which fields appear in the views:

def constraints = {
    name()
    gender()
}


Range constraints on the age:

def constraints = {
    age(range:18..100)
}


If you use the inList constraint, the generator will also produce lists rather than text inputs:

def constraints = {
    category(inList: ["Indian", "Other"])
}

The Field Plugins

The Grails scaffolding templates use the Fields Plugin. After creating the scaffold views, you may modify the forms and tables using the Taglib that the plugin offers.

<f:table collection="aboutAuthorList" properties="['name', 'gender']"/>

Frequently Asked Questions

What is Grails?

Grails is a free, open-source web application framework using the Apache Groovy programming language. Adhering to the code by convention concept .It is a high-productivity framework.

What are the Features of Grails?

Grails have a lot of features including Grails Groovy Lineage, Spring Boot Foundation, Built-in Testing framework, Plugin Library, and Pragmatic Strategy.

What is scaffolding in Grails?

For a domain class, scaffolding enables you to create certain fundamental CRUD interfaces, such as The required views. CRUD (create, read, update, delete) controller actions.

How does the Grails framework operate?

The Grails framework uses the Model View Controller design pattern to divide tasks inside the application. In your system, model classes should represent the domain objects. The application's flow should be under the control of controller classes.

Is Grails an MVC framework?

MVC apps are created using the Grails MVC platform. The MVC architecture is built-in into Grails. In MVC, model classes should represent the domain objects, while the controller represents the application's flow.

Conclusion

In the article, we learned about scaffolding in grails. We hope this article will help you understand the concept of scaffolding. Check out our other blogs on the topic Grails:

Refer to our guided paths on Coding Ninjas Studio to learn about Data Structure and Algorithms, Competitive Programming, JavaScript, etc. Enroll in our courses and refer to our mock test available. Have a look at the interview experiences and interview bundle for placement preparations.

Happy Coding!

Live masterclass