Grails(11)Guide Book Chapter 11 Internationalization
11 Internationalization
grails also leverage the underlying Spring MVC internationalization support.
12 Security
12.4 Security Plugins
12.4.1 Spring Security
12.4.2 Shiro
13 Plugins
13.1 Creating and Installing Plugins
Creating Plugins
Creating a Grails plugin is a simple matter of running the command>
>grails create-plugin [PLUGIN NAME]
This is to create a plugin project
Installing and Distributing Plugins
To distribute a plugin you navigate to its root directory in a console and run
>grails package-plugin
This will create a zip file of the plugin starting with grails-then the plugin name and version.
Install the plugin from a local file
>grails install-plugin /path/to/grails-example-0.1.zip
Or we can install the plugin from remote server
>grails install-plugin http://server.com/plugins/grails-example-0.1.zip
13.2 Plugin Repositories
13.3 Understanding a Plugin's Structure
+ grails-app
+ controllers
+ domain
+ taglib
+ lib
+ src
+ java
+ groovy
+ web-app
+ js
+ css
13.4 Providing Basic
13.6 Hooking into Build Events
13.7 Hooking into Runtime Configuration
13.8 Adding Dynamic Methods at Runtime
13.9 Participating in Auto Reload Events
13.10 Understanding Plugin Load Order
14. Web Services
Web services are all about providing a web API onto your web application and are typically implemented in either REST or SOAP.
14.1 REST
REST is very simple and just involves using plain XML or JSON as a communication medium, combined with URL patterns that are 'representational' of the underlying system, and HTTP methods such as GET, PUT, POST and DELETE
URL patterns
static mappings = {
"/product/$id?"(resource: "product")
}
GET show
PUT update
POST save
DELETE delete
We can alter how HTTP methods are handled by using URL Mappings to map to HTTP methods:
"/product/$id"(controller: "product") {
action = [GET: "show", PUT: "update", DELETE: "delete", POST: "save"]
}
HTTP Methods
XML Marshalling - Reading
import grails.converters.XML
class ProductController {
def show(){
if(params.id && Product.exists(params.id)){
def p = Product.findByName(params.id)
render p as XML
}else{
def all = Product.list()
render all as XML
}
}
}
Data Binding
<?xml version="1.0" encoding="ISO-8859-1" ?>
<product>
<name>MacBook</name>
<vendor id="12">
<name>Apple</name>
</vendor>
</product>
def save(){
def p = new Product(params.product)
if(p.save()){
render p as XML
}else {
render p.errors
}
}
14.2 SOAP
CXF, Axis2, Metro
14.3 RSS and ATOM
15 Grails and Spring
15.1 The underpinning of Grails is Spring MVC
16. Grails and Hibernate
17. Scaffolding
Scaffolding lets you auto-generate a whole application for a given domain class.
Dynamic Scaffolding
The simplest way to get started with scaffolding is to enable it with the scaffold property. Set the scaffold property in the controller to true for the Book domain class.
class BookController {
static scaffold = true
}
class SomeController {
static scaffold = Author
}
Scaffold the controller to domain class Author.
With this configured, when I start my application the actions and views will be auto-generated at runtime. The following actions are dynamically implemented by default by the runtime scaffolding mechanism:
- list
- show
- edit
- delete
- create
- save
- update
A CRUD interface will also be generated. To access this open http://localhost:8080/appName/book
I can add new actions to a scaffolded controller.
class BookController {
static scaffold = Book
def changeAuthor(){ …snip… }
}
I can also override the scaffolded actions:
class BookController {
static scaffold = Book
//overrides scaffolded action
def list(){ …snip… }
}
Customizing the Generated Views
Static Scaffolding
>grails generate-controller Book
>grails generate-views Book
>grails generate-all Book //generate everything
>grails generate-all com.sillycat.Book
18 Deployment
>grails run-app
>grails run-war // Almost the same as run-app, but the applications are deployed as WAR files, so Hot-reloading is disabled.
WAR file
>grails war
References:
http://grails.org/doc/latest/guide/index.html
http://grails.org/doc/latest/guide/i18n.html
http://grails.org/doc/latest/guide/security.html
http://grails.org/doc/latest/guide/plugins.html
http://grails.org/doc/latest/guide/webServices.html
http://grails.org/doc/latest/guide/spring.html
http://grails.org/doc/latest/guide/hibernate.html
http://grails.org/doc/latest/guide/scaffolding.html
http://grails.org/doc/latest/guide/deployment.html
11 Internationalization
grails also leverage the underlying Spring MVC internationalization support.
12 Security
12.4 Security Plugins
12.4.1 Spring Security
12.4.2 Shiro
13 Plugins
13.1 Creating and Installing Plugins
Creating Plugins
Creating a Grails plugin is a simple matter of running the command>
>grails create-plugin [PLUGIN NAME]
This is to create a plugin project
Installing and Distributing Plugins
To distribute a plugin you navigate to its root directory in a console and run
>grails package-plugin
This will create a zip file of the plugin starting with grails-then the plugin name and version.
Install the plugin from a local file
>grails install-plugin /path/to/grails-example-0.1.zip
Or we can install the plugin from remote server
>grails install-plugin http://server.com/plugins/grails-example-0.1.zip
13.2 Plugin Repositories
13.3 Understanding a Plugin's Structure
+ grails-app
+ controllers
+ domain
+ taglib
+ lib
+ src
+ java
+ groovy
+ web-app
+ js
+ css
13.4 Providing Basic
13.6 Hooking into Build Events
13.7 Hooking into Runtime Configuration
13.8 Adding Dynamic Methods at Runtime
13.9 Participating in Auto Reload Events
13.10 Understanding Plugin Load Order
14. Web Services
Web services are all about providing a web API onto your web application and are typically implemented in either REST or SOAP.
14.1 REST
REST is very simple and just involves using plain XML or JSON as a communication medium, combined with URL patterns that are 'representational' of the underlying system, and HTTP methods such as GET, PUT, POST and DELETE
URL patterns
static mappings = {
"/product/$id?"(resource: "product")
}
GET show
PUT update
POST save
DELETE delete
We can alter how HTTP methods are handled by using URL Mappings to map to HTTP methods:
"/product/$id"(controller: "product") {
action = [GET: "show", PUT: "update", DELETE: "delete", POST: "save"]
}
HTTP Methods
XML Marshalling - Reading
import grails.converters.XML
class ProductController {
def show(){
if(params.id && Product.exists(params.id)){
def p = Product.findByName(params.id)
render p as XML
}else{
def all = Product.list()
render all as XML
}
}
}
Data Binding
<?xml version="1.0" encoding="ISO-8859-1" ?>
<product>
<name>MacBook</name>
<vendor id="12">
<name>Apple</name>
</vendor>
</product>
def save(){
def p = new Product(params.product)
if(p.save()){
render p as XML
}else {
render p.errors
}
}
14.2 SOAP
CXF, Axis2, Metro
14.3 RSS and ATOM
15 Grails and Spring
15.1 The underpinning of Grails is Spring MVC
16. Grails and Hibernate
17. Scaffolding
Scaffolding lets you auto-generate a whole application for a given domain class.
Dynamic Scaffolding
The simplest way to get started with scaffolding is to enable it with the scaffold property. Set the scaffold property in the controller to true for the Book domain class.
class BookController {
static scaffold = true
}
class SomeController {
static scaffold = Author
}
Scaffold the controller to domain class Author.
With this configured, when I start my application the actions and views will be auto-generated at runtime. The following actions are dynamically implemented by default by the runtime scaffolding mechanism:
- list
- show
- edit
- delete
- create
- save
- update
A CRUD interface will also be generated. To access this open http://localhost:8080/appName/book
I can add new actions to a scaffolded controller.
class BookController {
static scaffold = Book
def changeAuthor(){ …snip… }
}
I can also override the scaffolded actions:
class BookController {
static scaffold = Book
//overrides scaffolded action
def list(){ …snip… }
}
Customizing the Generated Views
Static Scaffolding
>grails generate-controller Book
>grails generate-views Book
>grails generate-all Book //generate everything
>grails generate-all com.sillycat.Book
18 Deployment
>grails run-app
>grails run-war // Almost the same as run-app, but the applications are deployed as WAR files, so Hot-reloading is disabled.
WAR file
>grails war
References:
http://grails.org/doc/latest/guide/index.html
http://grails.org/doc/latest/guide/i18n.html
http://grails.org/doc/latest/guide/security.html
http://grails.org/doc/latest/guide/plugins.html
http://grails.org/doc/latest/guide/webServices.html
http://grails.org/doc/latest/guide/spring.html
http://grails.org/doc/latest/guide/hibernate.html
http://grails.org/doc/latest/guide/scaffolding.html
http://grails.org/doc/latest/guide/deployment.html