Grails (framework)

Uses your system voice; voice and rate in settings.

Snapshot

≈ 5 min read

Names in other languages

No names in other languages are held for this entry yet.

Article bodies are English and are not machine-translated. A name in another language is not a translation of the article.

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.

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. class BookController { def list() { [ books: Book.list() ] } } 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. Our books ${it.title} (${it.author.name}) 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. class ApplicationTagLib { def formatDate = { attrs, body -> out The formatDate tag library above formats a object to a . 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: 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. class Book { String title Person author } 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. def book = new Book(title:"The Da Vinci Code", author:Author.findByName("Dan Brown")) book.save() The delete() method deletes an object from the database: def book = Book.findByTitle("The Da Vinci Code") book.delete() The refresh() method refreshes the state of an object from the database: def book = Book.findByTitle("The Da Vinci Code") book.refresh() The ident() method retrieves the object's identity assigned from the database: def book = Book.findByTitle("The Da Vinci Code") def id = book.ident() def bookCount = Book.count() The exists() method returns true if an object exists in the database with a given identifier: def bookExists = Book.exists(1) The find() method returns the first object from the database based on an object query statement: def book = Book.find("from Book b where b.title = ?", [ 'The Da Vinci Code' ) Note that the query syntax is Hibernate HQL. The findAll() method returns all objects existing in the database: def books = Book.findAll() The findAll() method can also take an object query statement for returning a list of objects: def books = Book.findAll("from Book") The findBy*() methods return the first object from the database which matches a specific pattern: def book = Book.findByTitle("The Da Vinci Code") Also: def book = Book.findByTitleLike("%Da Vinci%") The findAllBy*() methods return a list of objects from the database which match a specific pattern: def books = Book.findAllByTitleLike("The%") The findWhere*() methods return the first object from the database which matches a set of named parameters: def book = Book.findWhere(title:"The Da Vinci Code") class BookController { static scaffold = true } By creating this class you can perform CRUD operations on http://localhost:8080/book . This works because the BookController follows the same naming convention as the Book domain class. To scaffold a specific domain class we could reference the class directly in the scaffold property: class SomeController { static scaffold = Book } Currently Grails does not provide scaffolding for associations. Mastering Grails An 18-part on-line tutorial provided by IBM (from 2008) ==Books==

Text derived from https://en.wikipedia.org/w/index.php?oldid=1376305432 under CC BY-SA 4.0; modifications have been made.