Grails controller 用法

1.actionName

返回当前执行的action名称。

 

class BookController {
	def list = {
		log.info "Executing action $actionName"

render(view:actionName) }

2.afterInterceptor

action执行后,view渲染前的拦截器。

class BookController {
	def afterInterceptor = { model ->
		model.foo = "bar"
	}
}

3.allowedMethods

限制HTTP请求方法能访问的controller 方法。

class PersonController {
  // action1 may be invoked via a POST
  // action2 has no restrictions
  // action3 may be invoked via a POST or DELETE
  def allowedMethods = action1:'POST',
                        action3:['POST', 'DELETE']
  def action1 = { … }
  def action2 = { … }
  def action3 = { … }
}

4.beforeInterceptor

action运行前的拦截器

5.bindData

6.chain

7.controllerName

返回当前执行controller的名称

8.flash

9.grailsApplication

10.params

请求参数值

class BookController {
	def show {
		def book = Book.get(params.id)
	}
}

11.redirect

redirect(uri:"book/list")
redirect(url:"http://www.blogjava.net/BlueSUN")
redirect(action:"show")
redirect(controller:"book",action:"list")
redirect(action:"show",id:4, params:[author:"Stephen King"])

12.render

// renders text to response
render "some text"
// renders text for a specified content-type/encoding
render(text:"<xml>some xml</xml>",contentType:"text/xml",encoding:"UTF-8")

// render a template to the response for the specified model
render(template:"book",model:[book:new Book(title:'The Shining',author:'Stephen King')])

// render each item in the collection using the specified template
render(template:"book",collection:[b1, b2, b3])

// render a template to the response for the specified bean
render(template:"book",bean:new Book(title:'The Shining',author:'Stephen King'))

// render the view with the specified model
render(view:"viewName",model:[book:new Book(author:'Stephen King')])

// render the view with the controller as the model
render(view:"viewName")

// render some markup to the response
render {
    div(id:"myDiv", "some text inside the div")
}

// render some XML markup to the response
render(contentType:"text/xml") {
    books {
         for(b in books) {
            book(title:b.title,author:b.author)
         }
    }
}

// render a JSON ( http://www.json.org ) response with the builder attribute:
render(contentType:"text/json") {
	book(title:b.title,author:b.author)

}

// Automatic marshalling of XML and JSON
import grails.converters.*
…
render Book.list(params) as JSON
render Book.get(params.id) as XML

(1)text - the text to render

(2)builder - the builder to use when rendering markup

(3)view - the view to delegate to rendering to

(4)template - the template to render

(5)var - the name of the variable to passed into a template, defaults to the groovy default argument 'it' if not specified

(6)bean - the bean to use in rendering

(7)model - the model to use in rendering

(8)collection - for renderig a template against each item in a collection

(9)contentType - the contentType of the response

(10)encoding - the encoding to the response

(11) converter - a converter that should be rendered as Response.

13.request

HttpServletRequest类的实例

14.response

HttpServletReponse类的实例

15.servletContext

ServletContext类的实例

16.session

HttpSession类的实例

17.withFormat

根据 request Accpet头、格式参数或URI后缀,进行不同响应。

import grails.converters.*
class BookController {
	def books
	def list = {
		this.books = Book.list()
		withFormat {
			html bookList:books
			js { render "alert('hello')" } 
			xml { render books as XML }
		}
	}
}

 

最后欢迎大家访问我的个人网站:1024s​​​​​​​

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值