grails2.4实用新特性

添加资源的方便之门

将资源文件放置在/grails-app/assets/目录下,不同资源分开,分别是images,javascripts,stylesheets三个子目录

gsp文件中引用资源只需如下:

<asset:javascript src="application.js"/>
<asset:stylesheet href="application.css"/>
<asset:image src="grails_logo.png" height="60" />

更好用的GORM子查询

  • 嵌套子查询
def results = Person.where{
    firstname in where {age < 18}.firstname
}.list()
  • Criteria 与 where 结合使用
def result = Person.withCriteria{
    notIn "firstname",Person.where {age < 18}.firstname
}
  • 联合两个domain的相关查询
def employees = Employee.where {
    region.continent in ['APAC', "EMEA"]
}.id()
def results = Sale.where {
    employee in employees && total > 100000
}.employee.list()
  • 支持简单变量声明的别名进行查询
def query = Employee.where {
    def em1 = Employee
    exists Sale.where {
        def s1 = Sale
        def em2 = employee
        return em2.id == em1.id
    }.id()
}
def results = query.list()

支持异步处理请求

def index() {
    def ctx = startAsync()
    ctx.start {
        new Book(title:"The Stand").save()
        render template:"books", model:[books:Book.list()]
        ctx.complete()
    }
}

参考 http://grails.org/doc/latest/guide/single.html#async

import static grails.async.Promises.*
//...
def index() {
     tasks books: Book.async.list(),
         totalBooks: Book.async.count(),
         otherValue: {
           // do hard work
         }
}

Published: October 28 2014

blog comments powered by Disqus