The update speed of grails is really slow. Now grails 1.0rc1 enters stable. Let's take a look at some highlights of this version.
1. Orm Enhancement
1.1 DSL
The new version will support object relation mapping (ORM) domain specific language (DSL). In the future, we will no longer need to use the traditional hibernate callback ing method. Let's take a look at an 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}
DSL supports the following aspects:
- Table and column name
- Inheritance Policy
- Second-level cache Configuration
- Id Generation Policy
- Combination ID support
- Early/Inert Loading
- Database index
- Custom hibernate User Type
1.2 pessimistic lock
By default, Gorm domain class is configured to use Optimistic Locking. Now you can use the pessimistic lock to obtain the "Select... for update" function:
def book = Book.get(1)book.lock()book.title = "The Stand"book.save()
1.3 session flusing Control
In general, grails will control when to implement the session flush operation, but now you can also control the flush operation on your own.
def book = Book.get(1)book.save(flush:true)// or book.delete(flush:true)
2. Filter (filters)
Grails uses filter in the Controller to implement the interception mechanism. For example:
class SecurityFilters { def filters = { loginCheck(controller:'*', action:'*') { before = { if(!session.user && !actionName.equals('login')) { redirect(action:'login') return false } } } }}
3. Enhanced tag Library
3.1 support for namespaces
This is also a long-awaited feature. With this feature, she is not afraid of Tag name conflicts.
// tag codeclass FormatTagLib { static namespace = 'fmt' def dateFormat = { attrs -> out << new java.text.SimpleDateFormat(attrs.format).format(attrs.value) }}// gsp code<fmt:dateFormat format="dd-MM-yyyy" value="${new Date()}" />// or as a method${fmt.dateFormat(format:'dd-MM-yyyy',value:new Date())}
3.2 body variable
Let's take a look at the example:
// tag codedef repeat = { attrs, body -> def count = attrs.times.toInteger() count.times { body(num:it) }}// gsp code<g:repeat times="10">Number: ${num}</g:repeat>
3.3 added new range support.
New pagination variables can be used in tags. You can use <G: Set> to use some variables.
<g:set var="foo" value="${new Date()}" scope="page" /><g:set var="bar" value="${new Date()-7}" scope="session" />
4. Controller Enhancement
4.1 multi-dimensional parameters
The grails Params object can be defined in multiple dimensions. That is to say, if your form submission uses parameters separated ".",
<g:textField name="book.title" value="The Stand" /><g:textField name="author.name" value="Stephen King" />
You can obtain this parameter using the following methods:
params["book"]
As long as it is used to bind data when processing the domain to the image
def book = new Book(params["book"])def author = new Author(params["author"])
Or
def book = new Book(title:params["book"]?.title)
4.2 Data Binding and type conversion errors
Grails takes into account spring's data binding function and retains the error information and original values during type conversion, for example:
def site = new Site(url:"bad_url")assert site.hasErrors()assert "bad_url" == site.errors.getFieldError("url")
5. Enhanced Configuration
5.1 JNDI data sources
You can use the database connection pool provided by many commercial application servers.
Configuration File: grails-APP/CONF/datasource. GroovyDatasource {jndiname = "Java: COMP/ENV/mydatasource "}5.2 more flexible configuration location
You can define multiple internal or external configuration locations.
grails.config.locations = [ "classpath:${appName}-config.properties", "classpath:${appName}-config.groovy", "file:${userHome}/.grails/${appName}-config.properties", "file:${userHome}/.grails/${appName}-config.groovy"]
Grails now supports spring's property place Hodler function. You can specify the attribute value in config. Groovy to overwrite the original value by using the beans scope.
beans { myService.someProperty = "foo"}
6. Enhanced URL ing
6.1 response code and error handling
You can customize the processing actions corresponding to the error code.
class UrlMappings { static mappings = { "500"(controller:"errors", action:"serverError") "404"(controller:"errors", action:"notFound") "403"(controller:"errors", action:"forbidden") }}
6.2 map URLs to Views
static mappings = { "/"(view:"/index") // map the root URL "/help"(controller:"site",view:"help") // to a view for a controller }
6.3 double wildcards
tatic mappings = { // will match /product/MacBook and /product/Apple/MacBook "/products/**"(controller:"product") }
If you want to obtain the remaining part of the path, you can use a variable
static mappings = { "/products/$path**"(controller:"product", action:"show") } // then in the action def show = { def path = params.path render path // prints "MacBook" or "Apple/MacBook" }