Grails终于迎来了它的1.0版本。这个版本中加入了很多新功能。
New Features
- GORM features ORM DSL for advanced Mappings
- Support for easy to use Filters
- Content Negotiation support
- REST support
- JNDI Support
New Features
ORM DSL
Grails 1.0 RC1 introduces an Object Relational Mapping (ORM) Domain Specific Language (DSL) that reduces the need to fallback to traditional Hibernate mapping. The DSL allows customization of the following aspects:
- Table and column names
- Inheritance strategy
- Second-level cache configuration
- Id generation strategy
- Composide Id support
- Eager/Lazy loading
- Database indices
- Custom Hibernate user types
Example:
class Person {
String firstName
static hasMany = [addresses:Address]
static mapping = {
table 'people'
version false
id column:'person_id'
columns {
firstName column:'First_Name'
addresses lazy:false
}
}
}
class Address {
String street
String postCode
}
Refer to the full documentation here.
Filters
Grails has up until now supported interceptors that can be used within controllers. Filters however offer a more powerful way of defining cross cutting concerns.
Filters can be applied to an entire controller, only specific actions or entire URI spaces. Example:
class SecurityFilters {
def filters = {
loginCheck(controller:'*', action:'*') {
before = {
if(!session.user && !actionName.equals('login')) {
redirect(action:'login')
return false
}
}
}
}
}
Refer to the full documentation here.
Content Negotation
Grails now supports content negotiation via the Accept/Content-Type HTTP headers, a parameter or URI extensions. Mime types can be configured in Config.groovy:
grails.mime.types = [ html: ['text/html','application/xhtml+xml'],
xml: ['text/xml', 'application/xml']
// etc.
]
And requests dealt with via the withFormat method:
def list = {
def results = Book.list()
withFormat {
html bookList:result
xml { render results as XML }
}
}
Automatic XML/JSON Unmarshalling
JSON and XML requests can now be automatically unmarshalled via the params object. Given an incoming XML request of:
Stephen King
...
This can be consumed with:
def save = {
def b = new Book(params['book'])
if(b.save()) {
// deal with book
}
}
Support for Mapping Foreign Key Columns and Join Tables
Grails' ORM DSL now support mappings foreign key columns and join tables for associations. To change the foreign key of a one-to-one you can do:
class Book {
Author author
static mapping = {
columns {
author column:'auth_id'
}
}
}
You can also change the join table and column used for unidirectional one-to-many and many-to-many associations:
class Author {
static hasMany = [books:Book]
static mapping = {
columns {
books joinTable:[name:'authors_books', key:'book_id', column:'author_id']
}
}
}
JNDI Data Sources
Grails now has improved support for JNDI data sources via a jndiName property in the DataSource definition:
grails-app/conf/DataSource.groovy
dataSource {
jndiName = "java:comp/env/myDataSource"
}
Source:Grails
Hi,
Thank you! I would now go on this blog every day!
Have a nice day
Zoran
Hello,
Ugh, I liked! So clear and positively.
Thanks
Eremeeff