ArticleslgStudy

computer science

Grails (framework)

Grails (framework) is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Grails (framework) rather than just read about it. In short: Grails is an open source web application framework that uses the Apache Groovy programming language (which is in turn based on the Java platform). It is intended to be a high-productivity framework by following the "coding by convention" paradigm, providing a stand-alone development environment and hiding much of the configuration detail from the developer.

Key takeaways

  • Grails (framework) belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Grails (framework) to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Grails (framework) from memory before moving on to harder problems.

Reference excerpt

Grails is an open source web application framework that uses the Apache Groovy programming language (which is in turn based on the Java platform). It is intended to be a high-productivity framework by following the "coding by convention" paradigm, providing a stand-alone development environment and hiding much of the configuration detail from the developer. Grails was previously known as "Groovy on Rails"; in March 2006 that name was dropped in response to a request by David Heinemeier Hansson, founder of the Ruby on Rails framework. Work began in July 2005, with the 0.1 release on March 29, 2006, and the 1.0 release announced on February 18, 2008.

Overview Grails was developed to address a number of goals:

Provide a web framework for the Java platform. Re-use existing Java technologies such as Hibernate and Spring under a single interface Offer a consistent development framework. Offer documentation for key portions of the framework: The Persistence framework. Templates using GSP (Groovy Server Pages). Dynamic tag libraries for creating web page components. Customizable and extensible Ajax support. Provide sample applications that demonstrate the framework. Provide a complete development mode, including a web server and automatic reload of resources.

Marketing Grails has three properties that differentiate it from traditional Java web frameworks:

No XML configuration Ready-to-use development environment Functionality available through mixins

No XML configuration Creating web applications in Java traditionally involves configuring environments and frameworks at the start and during development. This configuration is very often externalized in XML files to ease configuration and avoid embedding configuration in application code. XML was initially welcomed as it provided greater consistency to configure applications. However, in recent years, it has become apparent that although XML is great for configuration, it can be tedious to set up an environment. This may reduce productivity as developers spend time understanding and maintaining framework configuration as the application grows. Adding or changing functionality in applications that use XML configuration adds an extra step to the change process, which slows down productivity and may diminish the agility of the entire process. Grails removes the need to add configuration in XML files. Instead, the framework uses a set of rules or conventions while inspecting the code of Grails-based applications. For example, a class name that ends with Controller (for example BookController) is considered a web controller.

Ready-to-use development environment When using traditional Java web toolkits, it's up to developers to assemble development units, which can be tedious. Grails provides a development environment that includes a web server to get developers started right away. All required libraries are part of the Grails distribution, and Grails prepares the Java web environment for deployment automatically.

Functionality available through mixins Grails features dynamic methods on several classes through mixins. A mixin is a method that is added to a class dynamically, as if the functionality had been compiled into the program. These dynamic methods allow developers to perform operations without having to implement interfaces or extend base classes. Grails provides dynamic methods based on the type of class. For example, domain classes have methods to automate persistence operations like save, delete and find

Web framework The Grails web framework has been designed according to the MVC paradigm.

Controllers Grails uses controllers to implement the behavior of web pages. Below is an example of a controller:

The controller above has a list action which returns a model containing all books in the database. To create this controller the grails command is used, as shown below:

grails create-controller Book

This command creates a class in the grails-app/controller directory of the Grails project. Creating the controller class is sufficient to have it recognized by Grails. The list action maps to http://localhost:8080/book/list in development mode.

Views Grails supports JSP and GSP. The example below shows a view written in GSP which lists the books in the model prepared by the controller above:

This view should be saved as grails-app/views/book/list.gsp of the Grails project. This location maps to the BookController and list action. Placing the file in this location is sufficient to have it recognized by Grails.

Dynamic tag libraries Grails provides a large number of tag libraries out of the box. However you can also create and reuse your own tag libraries easily:

The formatDate tag library above formats a java.util.Date object to a String. This tag library should be added to the grails-app/taglib/ApplicationTagLib.groovy file or a file ending with TagLib.groovy in the grails-app/taglib directory. Below is a snippet from a GSP file which uses the formatDate tag library:

<g:formatDate format="yyyyMMdd" date="${myDate}"/>

To use a dynamic tag library in a GSP no import tags have to be used. Dynamic tag libraries can also be used in JSP files although this requires a little more work. [1] Archived 2010-10-17 at the Wayback Machine

Persistence

Model The domain model in Grails is GORM (Grails Object Relational Mapping). Domain classes are saved in the grails-app/domain directory and can be created using the grails command as shown below:

grails create-domain-class Book

This command requests the domain class name and creates the appropriate file. Below the code of the Book class is shown:

Creating this class is all that is required to have it managed for persistence by Grails. With Grails 0.3, GORM has been improved and e.g. adds the properties id and version itself to the domain class if they are not present. The id property is used as the primary key of the corresponding table. The version property is used for optimistic locking.

Methods When a class is defined as a domain class, that is, one managed by GORM, methods are dynamically added to aid in persisting the class's instances. [2] Archived 2010-10-19 at the Wayback Machine

Dynamic Instance Methods The save() method saves an object to the database:

The delete() method deletes an object from the database:

The refresh() method refreshes the state of an object from the database:

The ident() method retrieves the object's identity assigned from the database:

… excerpt ends here. Continue reading the full article.

Worked examples

Example 1 — a first encounter with Grails (framework)

Start with the simplest possible case. Write down what Grails (framework) claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Grails (framework) before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Grails (framework) ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Grails (framework)

In research
Grails (framework) appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Grails (framework) in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Grails (framework) is common in secondary-school and first-year university syllabi. It links to neighbouring topics Java (software platform), Web frameworks, so understanding it makes those chapters shorter.
In everyday life
Look for Grails (framework) outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Grails (framework)” →

How to study Grails (framework) in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Grails (framework) means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Grails (framework) out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Grails (framework) in simple terms?

Grails is an open source web application framework that uses the Apache Groovy programming language (which is in turn based on the Java platform). It is intended to be a high-productivity framework by following the "coding by convention" paradigm, providing a stand-alone development environment and…

Why does Grails (framework) matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Grails (framework)?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Grails (framework).

Tags

  • Java (software platform)
  • Web frameworks

Keep exploring