Grails 快速入门(二)

这一篇快速过一遍这套框架有什么特性…

Controllers Usage

  • actionName
  • controllerName

吃瓜群众不明白

  • allowedMethods (约束请求方法)
  • bindData

关于 bindDate 的使用

  • chain
  • defaultAction
  • errors
  • flash
  • forward
  • grailsApplication
  • hasErrors
  • namespace
  • params
  • redirect
  • render

关于 render 的使用

从官网来看 http://docs.grails.org/latest/ref/Controllers/render.html
返回的类型主要有:可指定编码格式的文本、模板的渲染 template [model、collection、bean]、直接渲染视图、markup、XML、JSON、HTTP状态码、文件


// Automatic marshalling of XML and JSON
import grails.converters.*
…
render Book.list(params) as JSON
render Book.get(params.id) as XML
// render a file
render(file: new File(absolutePath), fileName: "book.pdf")

  • request
  • respond
  • response
  • responseFormats
  • scope
  • servletContext
  • session
  • withForm
  • withFormat

从domain中获取数据并遍历

上一个教程完整地建立了一个domain(其实我改了很多东西了..)

Service Usage

我们使用 Service 进行操作我们的domain , 需要注意的是

  • In Grails a service is a class with a name that ends in the convention “Service” and lives in the grails-app/services directory.)

上面的意思大概就是你的 Service 必须命名于 Service 后缀(使用插件就自动帮你补全) 且 放在 /services 中.

Domain Usage

关于高级的使用(高质量的干货) : http://sysgears.com/articles/advanced-gorm-features-inheritance-embedded-data-maps-and-lists-storing/

  • addTo

    处理表之间的关系 one-to-many or many-to-many relationship

    
    def fictBook = new Book(title: "IT")
    def nonFictBook = new Book(title: "On Writing: A Memoir of the Craft")
    def a = new Author(name: "Stephen King")
             .addToFiction(fictBook)
             .addToNonFiction(nonFictBook)
             .save()
    
    
    class Author {
    String name
    static hasMany = [fiction: Book, nonFiction: Book]
    }
    class Book {
        String title
         static belongsTo = [author: Author]
    }
    
  • attach

    配合 isAttached() ,关联已经被 discard() 的实例到当前的会话中

    def b = Book.get(1)
    b.title = "Blah"
    b.save(flush:true)
    b.discard()
    …
    if (!b.isAttached()) {
       b.attach()
    }
  • belongsTo

    一对多

  • clearErrors

    清楚错误的信息,用于 validate() 之后

    
        def b = new Book(title: "The Shining")
        b.validate()
        if (b.hasErrors()) {
            // clear the list of errors
            b.clearErrors()
            // fix the validation issues
            b.author = "Stephen King"
            // re-validate
            b.validate()
        }
    
    
  • constraints

    声明验证约束

    
      static constraints = {
        title blank: false, size: 5..150
        author nullable: true
    }
    
  • count

    总计对象的行数

  • countBy

    这个是动态查询,能够无限的延长(怎么用就不知道了),科科,大概就是三十一个…

  • createCriteria

    详细操作 http://docs.grails.org/latest/ref/Domain%20Classes/createCriteria.html

  • delete

    冬眠实例..并立刻删除

    
    book.delete(flush: true)
    
    
  • discard

    不被保存

  • embedded

    将同一张表的内容进行划分,便于理解与操作

  • errors

    配合validate使用

    
    def user = new User(params)
    if (user.validate()) {
        // do something with user
    }
    else {
        user.errors.allErrors.each {
            println it
        }
    }
    
    
  • executeQuery

    执行 HQL

  • executeUpdate

  • exists

  • fetchMode

  • find

  • findAll

  • findAllBy

  • findAllWhere

  • findBy

  • findOrCreateBy

  • findOrCreateWhere

  • findOrSaveBy

  • findOrSaveWhere

  • findWhere

  • first

  • get

  • getAll

  • getDirtyPropertyNames

  • getPersistentValue

  • hasErrors

  • hasMany

  • hasOne

  • ident

  • instanceOf

  • isAttached

  • isDirty

  • last

  • list

  • listOrderBy

  • load

  • lock

  • mapWith

  • mappedBy

  • mapping

  • merge

  • namedQueries

  • properties

  • read

  • refresh

  • removeFrom

  • save

  • transients

  • validate

  • where

  • whereAny

  • withCriteria

  • withNewSession

  • withSession

  • withTransaction

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

天瞳月

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值