Learn Java for Web Development

(Tina Meador) #1
CHAPTER 7: Rapid Web Development with Grails 325

Scaffolding


Scaffolding lets you autogenerate a whole application for a given domain class including views and
controller actions for CRUD operations. Scaffolding can be either static or dynamic; both types
generate the same code. The main difference is that in static scaffolding, the generated code is
available to the user before compilation and thus can be easily modified if necessary. In dynamic
scaffolding, however, the code is generated in memory at runtime and is not visible to the user. In the
section that follows, you will learn about both dynamic and static scaffolding.


Dynamic Scaffolding

As explained earlier, the dynamic scaffolding generates controller actions and views for CRUD
applications at runtime. To dynamically scaffold a domain class, you need a controller. You created a
controller (BookController) in Listing 7-3. To use the dynamic scaffolding, change the index action to
a scaffold property and assign it the domain class, as shown in Listing 7-10. This causes List Page,
Create Page, Edit Page, and Show Page views, as well as delete functionality, to be generated for
the specified domain class.


Listing 7-10. Dynamic Scaffolding–Enabled BookController


package bookstore


class BookController {


static scaffold = Book
}


After changing BookController to look like Listing 7-10, execute the run-app target.


The output of executing the run-app target is shown here:


| Loading Grails 2.2.4
| Configuring classpath.
| Environment set to development.....
| Packaging Grails application.....
| Running Grails application
| Server running. Browse to http://localhost:8080/bookstore


Click the BookController link on the welcome page.


Clicking the BookController link brings you to the Book List view shown in Figure 7-21.

Free download pdf