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