6 Web层 - Reference Documentation
Authors: Graeme Rocher, Peter Ledbrook, Marc Palmer, Jeff Brown, Luke Daley, Burt Beckwith
Version: null
Table of Contents
6 Web层
6.1 控制器
A controller handles requests and creates or prepares the response. A controller can generate the response directly or delegate to a view. To create a controller, simply create a class whose name ends with
一个控制器通常用以处理请求,创建或者准备响应,也能直接生成响应或者委托给一个视图。要创建一个控制器,只需要在Controller
in the grails-app/controllers
directory (in a subdirectory if it's in a package).The default URL Mapping configuration ensures that the first part of your controller name is mapped to a URI and each action defined within your controller maps to URIs within the controller name URI.
grails-app/controllers
目录(如果有包的话,要位于相应的子目录下)下简单创建一个名字以Controller
结尾的类。默认的URL映射配置能确保你的控制器名字的第一部分被映射到一个URI上,而控制器中的每个操作定义被映射到控制器命名URI中的URI中。
6.1.1 理解控制器和操作
Creating a controller
Controllers can be created with the create-controller or generate-controller command. For example try running the following command from the root of a Grails project:grails create-controller book
grails-app/controllers/myapp/BookController.groovy
:package myappclass BookController { def index() { }
}
BookController
by default maps to the /book URI (relative to your application root).Thecreate-controller
andgenerate-controller
commands are just for convenience and you can just as easily create controllers using your favorite text editor or IDE
创建控制器
控制器可以通过create-controller或者generate-controller命令创建。比如,在Grails工程的根目录中运行如下命令:grails create-controller book
grails-app/controllers/myapp/BookController.groovy
的控制器:package myappclass BookController { def index() { }
}
BookController
缺省被映射于URI /book (相对于你应用上下文的根而言)create-controller
和generate-controller
命令只是便利方法而已,你也可以使用你喜欢的文本编辑器或者IDE来轻松的创建控制器。
Creating Actions
A controller can have multiple public action methods; each one maps to a URI:class BookController { def list() { // do controller logic // create model return model } }
/book/list
URI by default thanks to the property being named list
.
创建操作
一个控制器可以有多个公共操作方法,每一个都映射于一个URI:class BookController { def list() { // do controller logic // create model return model } }
/book/list
,这要归功于list
属性。Public Methods as Actions
In earlier versions of Grails actions were implemented with Closures. This is still supported, but the preferred approach is to use methods.Leveraging methods instead of Closure properties has some advantages:- Memory efficient
- Allow use of stateless controllers (
singleton
scope) - You can override actions from subclasses and call the overridden superclass method with
super.actionName()
- Methods can be intercepted with standard proxying mechanisms, something that is complicated to do with Closures since they're fields.
grails.compile.artefacts.closures.convert
property to true in BuildConfig.groovy
:
grails.compile.artefacts.closures.convert = true
If a controller class extends some other class which is not defined under the grails-app/controllers/
directory, methods inherited from that class are not converted to controller actions. If the intent is to expose those inherited methods as controller actions the methods may be overridden in the subclass and the subclass method may invoke the method in the super class.
公共方法作为操作
在以前版本的Grails中,操作是通过闭包来实现的。现在依然是支持的,不过更推荐使用方法的方式来实现。使用方法来替代闭包有如下一些优点:- 更高效的内存
- 允许使用状态无关的控制器(作用域是
singleton
) - 你可以在子类中重载操作,并且可以使用
super.actionName()
调用父类的方法 - 方法可以通过标准的代理机制进行拦截,同样的事情闭包更复杂一些,因为它们是属性字段。
BuildConfig.groovy
中的grails.compile.artefacts.closures.convert
属性为true:
grails.compile.artefacts.closures.convert = true
如果一个控制器类继承于其他类,但不是被定义在grails-app/controllers/
目录下,那么继承过来的方法将不会被转换成控制器操作的。如果目标是为了能将那些继承来的方法暴露为控制器操作,那么这些方法应该是能够被子类重载的,并且在子类中也可以调用其父类的方法。
The Default Action
A controller has the concept of a default URI that maps to the root URI of the controller, for example/book
for BookController
. The action that is called when the default URI is requested is dictated by the following rules:
- If there is only one action, it's the default
- If you have an action named
index
, it's the default - Alternatively you can set it explicitly with the
defaultAction
property:
static defaultAction = "list"
缺省操作
一个控制器即映射到控制器的根URI。默认情况下缺省URI在这里的是/book。默认的URI通过以下规则来支配: 一个控制器具有默认URI的概念,其将映射到控制器的根URI。比如BookController
映射到/book
。当缺省URI被请求时,会根据以下规则来调用操作:
- 如果仅有一个操作,那么它就是那个缺省操作
- 如果你有一个
index
操作,那么它就是缺省的 - 或者你可以使用
defaultAction
属性来明确指定:
static defaultAction = "list"
6.1.2 控制器和作用域
Available Scopes
Scopes are hash-like objects where you can store variables. The following scopes are available to controllers:- servletContext - Also known as application scope, this scope lets you share state across the entire web application. The servletContext is an instance of ServletContext
- session - The session allows associating state with a given user and typically uses cookies to associate a session with a client. The session object is an instance of HttpSession
- request - The request object allows the storage of objects for the current request only. The request object is an instance of HttpServletRequest
- params - Mutable map of incoming request query string or POST parameters
- flash - See below
有效作用域
作用域就像是hash对象,允许你存储变量。以下是控制器有效作用域:- servletContext - 也被叫做应用级别范围,它允许你共享整个web应用的状态。 servletContext对象是ServletContext的一个实例
- session - 会话(session)允许关联某个用户的状态,通常使用Cookie把会话与客户端关联起来。session对象是HttpSession的一个实例
- request - 请求对象仅为当前的请求存储对象。request对象是HttpServletRequest的一个实例
- params - 带查询字串(query string)或者POST参数输入请求的可变map
- flash - 见下文
Accessing Scopes
Scopes can be accessed using the variable names above in combination with Groovy's array index operator, even on classes provided by the Servlet API such as the HttpServletRequest:class BookController { def find() { def findBy = params["findBy"] def appContext = request["foo"] def loggedUser = session["logged_user"] } }
class BookController { def find() { def findBy = params.findBy def appContext = request.foo def loggedUser = session.logged_user } }
访问作用域
作用域可以通过上述提到的变量名和Groovy的数组索引操作符的方式来访问,即使这些类是Servlet API的类,例如HttpServletRequest也可以用,比如:class BookController { def find() { def findBy = params["findBy"] def appContext = request["foo"] def loggedUser = session["logged_user"] } }
class BookController { def find() { def findBy = params.findBy def appContext = request.foo def loggedUser = session.logged_user } }
Using Flash Scope
Grails supports the concept of flash scope as a temporary store to make attributes available for this request and the next request only. Afterwards the attributes are cleared. This is useful for setting a message directly before redirecting, for example:def delete() { def b = Book.get(params.id) if (!b) { flash.message = "User not found for id ${params.id}" redirect(action:list) } … // remaining code }
list
action is requested, the message
value will be in scope and can be used to display an information message. It will be removed from the flash
scope after this second request.Note that the attribute name can be anything you want, and the values are often strings used to display messages, but can be any object type.
使用Flash作用域
Grails支持flash作用域的概念,它只存贮本次请求和下次请求之间临时用到的属性,随后属性值将被清除。这在重定向之前,设置提示消息是非常有用的,比如:def delete() { def b = Book.get(params.id) if (!b) { flash.message = "User not found for id ${params.id}" redirect(action:list) } … // remaining code }
list
操作被请求时,message
的值在此范围内有效,可以用以显示一个提示信息。在第二次请求的时候,此值将从flash
作用域移除。注意,属性的名称可以是你期望的任何东西,其值多是用以显示信息的字符串,不过也可以是任何对象。Scoped Controllers
By default, a new controller instance is created for each request. In fact, because the controller isprototype
scoped, it is thread-safe since each request happens on its own thread.You can change this behaviour by placing a controller in a particular scope. The supported scopes are:
prototype
(default) - A new controller will be created for each request (recommended for actions as Closure properties)session
- One controller is created for the scope of a user sessionsingleton
- Only one instance of the controller ever exists (recommended for actions as methods)
scope
property to your class with one of the valid scope values listed above, for examplestatic scope = "singleton"
Config.groovy
with the grails.controllers.defaultScope
key, for example:grails.controllers.defaultScope = "singleton"
Use scoped controllers wisely. For instance, we don't recommend having any properties in a singleton-scoped controller since they will be shared for all requests. Setting a default scope other thanprototype
may also lead to unexpected behaviors if you have controllers provided by installed plugins that expect that the scope isprototype
.
控制器的作用域
通常,每一个请求会创建一个控制器实例。事实上,是因为控制器的作用域是prototype
,并且每个请求都有自己的线程,所以控制器是线程安全的。不过,你还是可以在控制器内放置一个特定的作用域来改变这种行为。其支持的作用域如下:
prototype
(缺省) - 每一次请求创建一个新的控制器实例(当操作为必包属性时推荐使用)session
- 在一个用户会话的作用域内只创建一个控制器实例singleton
- 自始自终只有一个控制器实例(当操作时一个方法时推荐使用)
scope
属性,并且使用上述作用域之一为其赋值,比如:static scope = "singleton"
Config.groovy
中改变grails.controllers.defaultScope
的值来改变缺省策略,比如grails.controllers.defaultScope = "singleton"
请明智的使用控制器的作用域。比如我们不推荐singleton作用域的控制器有任何属性,因为他们将在 所有 的请求共享。此外,如果你安装的插件的控制器是prototype
的,那么修改缺省的prototype
作用域也可能导致不可预知的行为。
6.1.3 模型和视图
Returning the Model
A model is a Map that the view uses when rendering. The keys within that Map correspond to variable names accessible by the view. There are a couple of ways to return a model. First, you can explicitly return a Map instance:def show() { [book: Book.get(params.id)] }
The above does not reflect what you should use with the scaffolding views - see the scaffolding section for more details.If no explicit model is returned the controller's properties will be used as the model, thus allowing you to write code like this:
class BookController { List books List authors def list() { books = Book.list() authors = Author.list() } }
This is possible due to the fact that controllers are prototype scoped. In other words a new controller is created for each request. Otherwise code such as the above would not be thread-safe, and all users would share the same data.In the above example the
books
and authors
properties will be available in the view.A more advanced approach is to return an instance of the Spring ModelAndView class:import org.springframework.web.servlet.ModelAndViewdef index() { // get some books just for the index page, perhaps your favorites def favoriteBooks = ... // forward to the list view to show them return new ModelAndView("/book/list", [ bookList : favoriteBooks ]) }
attributes
application
返回模型
模型是在渲染的时候给视图用的一个映射(Map)。映射的键对应于视图中的命名变量。有很多方法都可以返回一个模型。首先,你可以通过明确返回映射(Map)实例的方式:def show() { [book: Book.get(params.id)] }
上述示例并 不会 影响到脚手架的视图-更多信息请参考脚手架章节。如果没有明确指定模型,那么控制器的属性将作为模型返回给视图,象如下代码所示那样:
class BookController { List books List authors def list() { books = Book.list() authors = Author.list() } }
这是可行的,因为控制器的缺省作用域是prototype。换句话说,每一个请求都将创建一个新的控制器。否则的话,上述的代码就不是线程安全的了,所有的用户将共享同样的数据。在上述示例中,
books
和authors
属性在视图中将是有效的。另外一个更高级的方式是返回一个Spring ModelAndView类的一个实例:import org.springframework.web.servlet.ModelAndViewdef index() { // get some books just for the index page, perhaps your favorites def favoriteBooks = ... // forward to the list view to show them return new ModelAndView("/book/list", [ bookList : favoriteBooks ]) }
attributes
application
Selecting the View
In both of the previous two examples there was no code that specified which view to render. So how does Grails know which one to pick? The answer lies in the conventions. Grails will look for a view at the locationgrails-app/views/book/show.gsp
for this list
action:class BookController { def show() { [book: Book.get(params.id)] } }
def show() {
def map = [book: Book.get(params.id)]
render(view: "display", model: map)
}
grails-app/views/book/display.gsp
. Notice that Grails automatically qualifies the view location with the book
directory of the grails-app/views
directory. This is convenient, but to access shared views you need instead you can use an absolute path instead of a relative one:def show() {
def map = [book: Book.get(params.id)]
render(view: "/shared/display", model: map)
}
grails-app/views/shared/display.gsp
.Grails also supports JSPs as views, so if a GSP isn't found in the expected location but a JSP is, it will be used instead.
选择视图
在前面的两个示例中,我们并没有指定要用那个视图来渲染。那么Grails是如何知道那个将被选择?答案是规约依赖。在下面的示例中,Grails将自动的寻找位于grails-app/views/book/show.gsp
的视图:class BookController { def show() { [book: Book.get(params.id)] } }
def show() {
def map = [book: Book.get(params.id)]
render(view: "display", model: map)
}
grails-app/views/book/display.gsp
来渲染。注意,Grails会根据grails-app/views
下的book
目录来自动地限定视图的位置。常规是这样的,但是要访问那些共享的视图,你还是需要使用绝对路径来替代相对路径:def show() {
def map = [book: Book.get(params.id)]
render(view: "/shared/display", model: map)
}
grails-app/views/shared/display.gsp
来渲染。Grails也支持JSP的视图,因此如果一个预期的GSP没有找到,但是有相应的JSP,那么它将使用此JSP。Rendering a Response
Sometimes it's easier (for example with Ajax applications) to render snippets of text or code to the response directly from the controller. For this, the highly flexiblerender
method can be used:render "Hello World!"
// write some markup
render {
for (b in books) {
div(id: b.id, b.title)
}
}
// render a specific view render(view: 'show')
// render a template for each item in a collection
render(template: 'book_template', collection: Book.list())
// render some text with encoding and content type render(text: "<xml>some xml</xml>", contentType: "text/xml", encoding: "UTF-8")
MarkupBuilder
to generate HTML for use with the render
method be careful of naming clashes between HTML elements and Grails tags, for example:import groovy.xml.MarkupBuilder … def login() { def writer = new StringWriter() def builder = new MarkupBuilder(writer) builder.html { head { title 'Log in' } body { h1 'Hello' form { } } } def html = writer.toString() render html }
MarkupBuilder
). To correctly output a <form>
element, use the following:def login() { // … body { h1 'Hello' builder.form { } } // … }
渲染响应
有时候在控制器中直接渲染文本或者代码片段到响应是很容易的(比如Ajax的应用)。这时,就可以使用灵活性很高的render
方法了:render "Hello World!"
// write some markup
render {
for (b in books) {
div(id: b.id, b.title)
}
}
// render a specific view render(view: 'show')
// render a template for each item in a collection
render(template: 'book_template', collection: Book.list())
// render some text with encoding and content type render(text: "<xml>some xml</xml>", contentType: "text/xml", encoding: "UTF-8")
MarkupBuilder
来生成HTML来供render
方法使用,这时,你要注意HTML元素和Grails标签名字的冲突,比如:import groovy.xml.MarkupBuilder … def login() { def writer = new StringWriter() def builder = new MarkupBuilder(writer) builder.html { head { title 'Log in' } body { h1 'Hello' form { } } } def html = writer.toString() render html }
MarkupBuilder
忽略)。要正确地输出<form>
元素,需要使用如下的例子:def login() { // … body { h1 'Hello' builder.form { } } // … }
6.1.4 重定向和链(Chaining)
Redirects
Actions can be redirected using the redirect controller method:class OverviewController { def login() {} def find() { if (!session.user) redirect(action: 'login') return } … } }
sendRedirect
method.The redirect
method expects one of:
- Another closure within the same controller class:
// Call the login action within the same class redirect(action: login)
- The name of an action (and controller name if the redirect isn't to an action in the current controller):
// Also redirects to the index action in the home controller redirect(controller: 'home', action: 'index')
- A URI for a resource relative the application context path:
// Redirect to an explicit URI
redirect(uri: "/login.html")
- Or a full URL:
// Redirect to a URL
redirect(url: "http://grails.org")
params
argument of the method:redirect(action: 'myaction', params: [myparam: "myvalue"])
params
object is a Map, you can use it to pass the current request parameters from one action to the next:redirect(action: "next", params: params)
redirect(controller: "test", action: "show", fragment: "profile")
重定向
操作可以使用控制器的redirect方法进行重定向:class OverviewController { def login() {} def find() { if (!session.user) redirect(action: 'login') return } … } }
sendRedirect
方法来工作的。redirect
方法的重定向目标用法如下:
- 同一控制器类的另外一个闭包:
// Call the login action within the same class redirect(action: login)
- 操作的名称(如果要重定向的操作名不在同一个控制器内,还需要制定控制器名):
// Also redirects to the index action in the home controller redirect(controller: 'home', action: 'index')
- 一个相对于应用上下文路径的URI
// Redirect to an explicit URI
redirect(uri: "/login.html")
- 或者一个完整的URL:
// Redirect to a URL
redirect(url: "http://grails.org")
params
来实现:redirect(action: 'myaction', params: [myparam: "myvalue"])
params
对象就是一个Map,因此你可以使用它将当前的请求参数从一个操作传递到下个操作:redirect(action: "next", params: params)
redirect(controller: "test", action: "show", fragment: "profile")
Chaining
Actions can also be chained. Chaining allows the model to be retained from one action to the next. For example calling thefirst
action in this action:class ExampleChainController { def first() { chain(action: second, model: [one: 1]) } def second () { chain(action: third, model: [two: 2]) } def third() { [three: 3]) } }
[one: 1, two: 2, three: 3]
chainModel
map. This dynamic property only exists in actions following the call to the chain
method:class ChainController { def nextInChain() { def model = chainModel.myModel … } }
redirect
method you can also pass parameters to the chain
method:chain(action: "action1", model: [one: 1], params: [myparam: "param1"])
链(Chaining)
操作同样可以作为一个链。在从一个操作传递到下个操作的时候,链一直保留着其模型。比如下面调用first
操作的示例:class ExampleChainController { def first() { chain(action: second, model: [one: 1]) } def second () { chain(action: third, model: [two: 2]) } def third() { [three: 3]) } }
[one: 1, two: 2, three: 3]
chainModel
映射来访问。此动态属性只存在于调用chain
方法的操作中:class ChainController { def nextInChain() { def model = chainModel.myModel … } }
redirect
方法一样,你可以传递参数给chain
方法:chain(action: "action1", model: [one: 1], params: [myparam: "param1"])
6.1.5 控制器拦截器
Often it is useful to intercept processing based on either request, session or application state. This can be achieved with action interceptors. There are currently two types of interceptors: before and after.
通常情况下,拦截处理请求、会话或者应用的状态数据是非常有用的。这个可以通过操作的拦截器来完成。当前有两种类型的拦截器:before和after。If your interceptor is likely to apply to more than one controller, you are almost certainly better off writing a Filter. Filters can be applied to multiple controllers or URIs without the need to change the logic of each controller
如果你的拦截可能用于一个以上控制器的话,你最好写一个过滤器。过滤器可以在不需要改变每个控制器逻辑的情况下,应用于多个控制器或者URI。
Before Interception
ThebeforeInterceptor
intercepts processing before the action is executed. If it returns false
then the intercepted action will not be executed. The interceptor can be defined for all actions in a controller as follows:def beforeInterceptor = {
println "Tracing action ${actionUri}"
}
def beforeInterceptor = [action: this.&auth, except: 'login']// defined with private scope, so it's not considered an action private auth() { if (!session.user) { redirect(action: 'login') return false } }def login() { // display login page }
auth
. A private method is used so that it is not exposed as an action to the outside world. The beforeInterceptor
then defines an interceptor that is used on all actions except the login action and it executes the auth
method. The auth
method is referenced using Groovy's method pointer syntax. Within the method it detects whether there is a user in the session, and if not it redirects to the login
action and returns false
, causing the intercepted action to not be processed.
前拦截
beforeInterceptor
在操作被执行以前进行拦截处理。如果其返回值是false
,那么被拦截的操作将得不到执行。拦截对控制器的所有操作进行定义,如下所示:def beforeInterceptor = {
println "Tracing action ${actionUri}"
}
def beforeInterceptor = [action: this.&auth, except: 'login']// defined with private scope, so it's not considered an action private auth() { if (!session.user) { redirect(action: 'login') return false } }def login() { // display login page }
auth
方法。这个私有方法通常用于避免被暴露为一个操作,从而也就不会被从外面访问到。接着beforeInterceptor
定义了一个应用于所有操作的拦截器,login操作除外,此拦截器将先执行auth
方法。auth
方法的引用是Groovy方法的指针语法。此方法检查一个用户是否存在于会话中,如果不存在,那么将重定向到login
操作并且返回false
,这样那些被拦截过的操作也就不会被处理。After Interception
Use theafterInterceptor
property to define an interceptor that is executed after an action:def afterInterceptor = { model ->
println "Tracing action ${actionUri}"
}
def afterInterceptor = { model, modelAndView -> println "Current view is ${modelAndView.viewName}" if (model.someVar) modelAndView.viewName = "/mycontroller/someotherview" println "View is now ${modelAndView.viewName}" }
modelAndView
may be null
if the action being intercepted called redirect
or render
.
后拦截
使用afterInterceptor
属性可以定义一个操作执行之后的拦截器:def afterInterceptor = { model ->
println "Tracing action ${actionUri}"
}
def afterInterceptor = { model, modelAndView -> println "Current view is ${modelAndView.viewName}" if (model.someVar) modelAndView.viewName = "/mycontroller/someotherview" println "View is now ${modelAndView.viewName}" }
redirect
或者render
,其modelAndView
可能是null
。Interception Conditions
Rails users will be familiar with the authentication example and how the 'except' condition was used when executing the interceptor (interceptors are called 'filters' in Rails; this terminology conflicts with Servlet filter terminology in Java):def beforeInterceptor = [action: this.&auth, except: 'login']
def beforeInterceptor = [action: this.&auth, except: ['login', 'register']]
def beforeInterceptor = [action: this.&auth, only: ['secure']]
拦截条件
Rails用户将很熟悉验证的示例以及在执行拦截的时候如何使用'except'条件(在Rails中,拦截器被称为‘过滤器’;这个术语跟Java中Servlet的过滤器冲突):def beforeInterceptor = [action: this.&auth, except: 'login']
def beforeInterceptor = [action: this.&auth, except: ['login', 'register']]
def beforeInterceptor = [action: this.&auth, only: ['secure']]
6.1.6 数据绑定
Data binding is the act of "binding" incoming request parameters onto the properties of an object or an entire graph of objects. Data binding should deal with all necessary type conversion since request parameters, which are typically delivered by a form submission, are always strings whilst the properties of a Groovy or Java object may well not be.Grails uses Spring's underlying data binding capability to perform data binding.
数据绑定是"绑定"输入的请求参数到一个对象的属性或者整个对象的行为。数据绑定将自动转换请求参数的类型,这些参数通常是通过表单提交来的String类型的值,而Groovy或者Java对象的属性很可能不是。Grails是使用Spring的基本数据绑定能力来完成数据绑定。Binding Request Data to the Model
There are two ways to bind request parameters onto the properties of a domain class. The first involves using a domain classes' Map constructor:def save() {
def b = new Book(params)
b.save()
}
new Book(params)
. By passing the params object to the domain class constructor Grails automatically recognizes that you are trying to bind from request parameters. So if we had an incoming request like:/book/save?title=The%20Stand&author=Stephen%20King
title
and author
request parameters would automatically be set on the domain class. You can use the properties property to perform data binding onto an existing instance:def save() { def b = Book.get(params.id) b.properties = params b.save() }
绑定请求数据到领域模型
有两种方法将请求参数绑定到一个领域类的属性上。第一个是使用领域类的构造函数,只要参数类型是Map类型即可:def save() {
def b = new Book(params)
b.save()
}
new Book(params)
代码中发生的。将参数params对象传递给到领域类的构造器时,Grails就可以自动识别你正在试图绑定来自请求中的参数。因此,假设我们有一个如下面所示的输入请求:/book/save?title=The%20Stand&author=Stephen%20King
title
和author
请求参数将自动被设置到领域类上。你也可以使用已经存在实例的properties属性来执行数据绑定:def save() { def b = Book.get(params.id) b.properties = params b.save() }
Data binding and Single-ended Associations
If you have aone-to-one
or many-to-one
association you can use Grails' data binding capability to update these relationships too. For example if you have an incoming request such as:/book/save?author.id=20
.id
suffix on the request parameter and look up the Author
instance for the given id when doing data binding such as:def b = new Book(params)
null
by passing the literal String
"null". For example:/book/save?author.id=null
单关联的数据绑定
如果你有一个one-to-one
或者many-to-one
关联,你也可以利用Grails的数据绑定能力来更新它们,比如:/book/save?author.id=20
.id
请求参数,并且在数据绑定的时候,会查找指定id的Author
实例,比如:def b = new Book(params)
null
,只要传给的String
类型的"null"即可。比如:/book/save?author.id=null
Data Binding and Many-ended Associations
If you have a one-to-many or many-to-many association there are different techniques for data binding depending of the association type.If you have aSet
based association (the default for a hasMany
) then the simplest way to populate an association is to send a list of identifiers. For example consider the usage of <g:select>
below:<g:select name="books"
from="${Book.list()}"
size="5" multiple="yes" optionKey="id"
value="${author?.books}" />
books
association.However, if you have a scenario where you want to update the properties of the associated objects the this technique won't work. Instead you use the subscript operator:<g:textField name="books[0].title" value="the Stand" /> <g:textField name="books[1].title" value="the Shining" />
Set
based association it is critical that you render the mark-up in the same order that you plan to do the update in. This is because a Set
has no concept of order, so although we're referring to books0
and books1
it is not guaranteed that the order of the association will be correct on the server side unless you apply some explicit sorting yourself.This is not a problem if you use List
based associations, since a List
has a defined order and an index you can refer to. This is also true of Map
based associations.Note also that if the association you are binding to has a size of two and you refer to an element that is outside the size of association:<g:textField name="books[0].title" value="the Stand" /> <g:textField name="books[1].title" value="the Shining" /> <g:textField name="books[2].title" value="Red Madder" />
<g:textField name="books[0].title" value="the Stand" /> <g:textField name="books[1].title" value="the Shining" /> <g:textField name="books[5].title" value="Red Madder" />
List
using the same .id
syntax as you would use with a single-ended association. For example:<g:select name="books[0].id" from="${bookList}" value="${author?.books[0]?.id}" /><g:select name="books[1].id" from="${bookList}" value="${author?.books[1]?.id}" /><g:select name="books[2].id" from="${bookList}" value="${author?.books[2]?.id}" />
books List
to be selected separately.Entries at particular indexes can be removed in the same way too. For example:<g:select name="books[0].id"
from="${Book.list()}"
value="${author?.books[0]?.id}"
noSelection="['null': '']"/>
books0
if the empty option is chosen.Binding to a Map
property works the same way except that the list index in the parameter name is replaced by the map key:<g:select name="images[cover].id"
from="${Image.list()}"
value="${book?.images[cover]?.id}"
noSelection="['null': '']"/>
Map
property images
under a key of "cover"
.
多关联的数据绑定
如果你有一个one-to-many或者many-to-many的关联,那么根据关联类型的不同,将对应不同的数据绑定技术。如果你使用基于Set
的关联(hasMany
缺省就是此种关联),那么最简单的方法就是传递一个标识符列表。比如下边<g:select>
的用法:<g:select name="books"
from="${Book.list()}"
size="5" multiple="yes" optionKey="id"
value="${author?.books}" />
books
。即便如此,类似的情况下,你想使用此技术来更新关联对象饿属性,将行不通。不过你可以通过下标(subscript)操作符的方式来实现,比如:<g:textField name="books[0].title" value="the Stand" /> <g:textField name="books[1].title" value="the Shining" />
Set
的关联还是有一个严重问题,那就是你要更新的内容总是以同样的顺序渲染的,这是因为Set
本来就没有顺序的概念,虽然你可以通过books0
和books1
来索引,但这并不能保证其关联顺序在服务器端也一致,当然你可以通过明确指定排序来比避免。如果你使用基于List
的关联的话,这并不是问题,因为List
已经是有序的,并且可以通过索引进行引用。基于Map
的关联也是。还要注意的是,如果你正在绑定的关联是有大小的,最外侧的那个元素所在位置就是关联的大小:<g:textField name="books[0].title" value="the Stand" /> <g:textField name="books[1].title" value="the Shining" /> <g:textField name="books[2].title" value="Red Madder" />
<g:textField name="books[0].title" value="the Stand" /> <g:textField name="books[1].title" value="the Shining" /> <g:textField name="books[5].title" value="Red Madder" />
.id
语法来绑定类型是List
且已存在的关联,比如:<g:select name="books[0].id" from="${bookList}" value="${author?.books[0]?.id}" /><g:select name="books[1].id" from="${bookList}" value="${author?.books[1]?.id}" /><g:select name="books[2].id" from="${bookList}" value="${author?.books[2]?.id}" />
books List
中各自独立的实体分别进行选择。同样的方法也可以用来删除特定的索引元素,比如:<g:select name="books[0].id"
from="${Book.list()}"
value="${author?.books[0]?.id}"
noSelection="['null': '']"/>
books0
的下拉框,当然只有选择项是空的时候才行。绑定到Map
属性的工作方式也是如此,不过要将参数名称中的列表索引替换为映射(map)的键(key):<g:select name="images[cover].id"
from="${Image.list()}"
value="${book?.images[cover]?.id}"
noSelection="['null': '']"/>
"cover"
绑定到Map
类型的images
中。Data binding with Multiple domain classes
It is possible to bind data to multiple domain objects from the params object.For example so you have an incoming request to:/book/save?book.title=The%20Stand&author.name=Stephen%20King
author.
or book.
which is used to isolate which parameters belong to which type. Grails' params
object is like a multi-dimensional hash and you can index into it to isolate only a subset of the parameters to bind.def b = new Book(params.book)
book.title
parameter to isolate only parameters below this level to bind. We could do the same with an Author
domain class:def a = new Author(params.author)
多领域类的数据绑定
通过params对象,是有可能将将数据绑定到多个领域对象的。比如下面的输入请求:/book/save?book.title=The%20Stand&author.name=Stephen%20King
author.
或者book.
,它们通常用来隔离参数所属的类型。Grails的params
对象更象是一个多维的哈希表(hash),你可以单独地只绑定参数的一个子集。def b = new Book(params.book)
book.title
参数的前缀(第一个点以前部分)来隔离此领域的子参数的。同样我们也可以用于领域类Author
:def a = new Author(params.author)
Data Binding and Action Arguments
Controller action arguments are subject to request parameter data binding. There are 2 categories of controller action arguments. The first category is command objects. Complex types are treated as command objects. See the Command Objects section of the user guide for details. The other category is basic object types. Supported types are the 8 primitives, their corresponding type wrappers and java.lang.String. The default behavior is to map request parameters to action arguments by name:class AccountingController { // accountNumber will be initialized with the value of params.accountNumber // accountType will be initialized with params.accountType def displayInvoice(String accountNumber, int accountType) { // … } }
params.accountType
request parameter has to be converted to an int
. If type conversion fails for any reason, the argument will have its default value per normal Java behavior (null for type wrapper references, false for booleans and zero for numbers) and a corresponding error will be added to the errors
property of the defining controller./accounting/displayInvoice?accountNumber=B59786&accountType=bogusValue
controller.errors.hasErrors()
will be true, controller.errors.errorCount
will be equal to 1 and controller.errors.getFieldError('accountType')
will contain the corresponding error.If the argument name does not match the name of the request parameter then the @grails.web.RequestParameter
annotation may be applied to an argument to express the name of the request parameter which should be bound to that argument:import grails.web.RequestParameterclass AccountingController { // mainAccountNumber will be initialized with the value of params.accountNumber // accountType will be initialized with params.accountType def displayInvoice(@RequestParameter('accountNumber') String mainAccountNumber, int accountType) { // … } }
数据绑定和操作参数
控制器的操作参数是绑定请求参数的主题,目前主要有2大类的操作参数。第一大类是命令对象,复杂类型的都被看作命令对象,更多详细请看本手册的命令对象章节。另外一大类是基本的对象类型,所支持的类型包括8个原生类型及其对应的包装类和java.lang.String。缺省情况下,从请求参数到操作参数的映射是通过名称来完成的,比如:class AccountingController { // accountNumber will be initialized with the value of params.accountNumber // accountType will be initialized with params.accountType def displayInvoice(String accountNumber, int accountType) { // … } }
params.accountType
必须要转换成int
才行。如果此转换失败了,不管什么原因导致,此参数将根据普通的Java行为进行设置(包装类是null,布尔类型是false,数字类型是0),并且产生一个相应的错误信息到控制器的errors
属性中。/accounting/displayInvoice?accountNumber=B59786&accountType=bogusValue
controller.errors.hasErrors()
将返回true,controller.errors.errorCount
的数值是1,并且controller.errors.getFieldError('accountType')
将包含其对应的出错信息。如果参数名称跟请求参数的名称并不匹配,那么可以使用@grails.web.RequestParameter
注解解决,只需要将要转换的请求参数名传递给注解即可:import grails.web.RequestParameterclass AccountingController { // mainAccountNumber will be initialized with the value of params.accountNumber // accountType will be initialized with params.accountType def displayInvoice(@RequestParameter('accountNumber') String mainAccountNumber, int accountType) { // … } }
Data binding and type conversion errors
Sometimes when performing data binding it is not possible to convert a particular String into a particular target type. This results in a type conversion error. Grails will retain type conversion errors inside the errors property of a Grails domain class. For example:class Book { … URL publisherURL }
Book
that uses the java.net.URL
class to represent URLs. Given an incoming request such as:/book/save?publisherURL=a-bad-url
a-bad-url
to the publisherURL
property as a type mismatch error occurs. You can check for these like this:def b = new Book(params)if (b.hasErrors()) { println "The value ${b.errors.getFieldError('publisherURL').rejectedValue}" + " is not a valid URL!" }
grails-app/i18n/messages.properties
file to use for the error. You can use a generic error message handler such as:typeMismatch.java.net.URL=The field {0} is not a valid URL
typeMismatch.Book.publisherURL=The publisher URL you specified is not a valid URL
数据绑定和类型转换错误
有时,在执行数据绑定时,可能不会把一个特殊的String转换到特殊的目标类型。这样,你就得到一个类型转换错误。Grails将类型转换错误保存到领域类的errors属性中。比如::class Book { … URL publisherURL }
Book
,它使用了类java.net.URL
来存储URLs。现在假设请求如下:/book/save?publisherURL=a-bad-url
a-bad-url
绑定到publisherURL
属性是不可能的,因为发生了类型不匹配的错误。你可以象下面那样进行检查:def b = new Book(params)if (b.hasErrors()) { println "The value ${b.errors.getFieldError('publisherURL').rejectedValue}" + " is not a valid URL!" }
grails-app/i18n/messages.properties
文件来定义错误信息。你可以使用通用的错误消息处理,比如:typeMismatch.java.net.URL=The field {0} is not a valid URL
typeMismatch.Book.publisherURL=The publisher URL you specified is not a valid URL
Data Binding and Security concerns
When batch updating properties from request parameters you need to be careful not to allow clients to bind malicious data to domain classes and be persisted in the database. You can limit what properties are bound to a given domain class using the subscript operator:def p = Person.get(1)p.properties['firstName','lastName'] = params
firstName
and lastName
properties will be bound.Another way to do this is is to use Command Objects as the target of data binding instead of domain classes. Alternatively there is also the flexible bindData method.The bindData
method allows the same data binding capability, but to arbitrary objects:def p = new Person()
bindData(p, params)
bindData
method also lets you exclude certain parameters that you don't want updated:def p = new Person()
bindData(p, params, [exclude: 'dateOfBirth'])
def p = new Person()
bindData(p, params, [include: ['firstName', 'lastName]])
Note that if an empty List is provided as a value for the include
parameter then all fields will be subject to binding if they are not explicitly excluded.
数据绑定和安全
当从请求参数进行批量地更新领域类地属性时,你要当心,绝不允许让用户的恶意数据绑定到领域类,并且持久化到数据库中。你可以使用下标操作符来限制那些属性可以被绑定到给定的领域类:def p = Person.get(1)p.properties['firstName','lastName'] = params
firstName
和lastName
属性将被绑定。另外一种方法是使用命令对象来替代领域类进行数据绑定。或者使用更灵活的bindData方法来绑定。bindData
方法具有同样的数据绑定能力,不过可以是任意对象:def p = new Person()
bindData(p, params)
bindData
方法还可以排除那些你不想更新的某些参数/属性:def p = new Person()
bindData(p, params, [exclude: 'dateOfBirth'])
def p = new Person()
bindData(p, params, [include: ['firstName', 'lastName]])
注意!如果include
参数值是一个空的列表,并且没有指定排除的话,那么所有的字段属性将被绑定。
6.1.7 XML和JSON响应
Using the render method to output XML
Grails supports a few different ways to produce XML and JSON responses. The first is the render method.Therender
method can be passed a block of code to do mark-up building in XML:def list() { def results = Book.list() render(contentType: "text/xml") { books { for (b in results) { book(title: b.title) } } } }
<books> <book title="The Stand" /> <book title="The Shining" /> </books>
def list() { def books = Book.list() // naming conflict here render(contentType: "text/xml") { books { for (b in results) { book(title: b.title) } } } }
books
which Groovy attempts to invoke as a method.
使用render方法输出XML
Grails支持几种不同的方法来产生XML和JSON响应。第一个就是render方法。render
方法可以被传递一个代码块,在代码块中,使用标签生成器来构建XML:def list() { def results = Book.list() render(contentType: "text/xml") { books { for (b in results) { book(title: b.title) } } } }
<books> <book title="The Stand" /> <book title="The Shining" /> </books>
def list() { def books = Book.list() // naming conflict here render(contentType: "text/xml") { books { for (b in results) { book(title: b.title) } } } }
books
试图被当作一个方法来调用。Using the render method to output JSON
Therender
method can also be used to output JSON:def list() { def results = Book.list() render(contentType: "text/json") { books = array { for (b in results) { book title: b.title } } } }
[ {title:"The Stand"}, {title:"The Shining"} ]
使用render方法输出JSON
render
方法也可以被用来输出JSON:def list() { def results = Book.list() render(contentType: "text/json") { books = array { for (b in results) { book title: b.title } } } }
[ {title:"The Stand"}, {title:"The Shining"} ]
Automatic XML Marshalling
Grails also supports automatic marshalling of domain classes to XML using special converters.To start off with, import thegrails.converters
package into your controller:import grails.converters.*
render Book.list() as XML
<?xml version="1.0" encoding="ISO-8859-1"?> <list> <book id="1"> <author>Stephen King</author> <title>The Stand</title> </book> <book id="2"> <author>Stephen King</author> <title>The Shining</title> </book> </list>
def xml = Book.list().encodeAsXML() render xml
自动XML编组(Marshalling)
Grails还支持将领域类自动编组为XML的用法,不过要借助于特定的转换器。首先,要先在你的控制器中的导入包grails.converters
:import grails.converters.*
render Book.list() as XML
<?xml version="1.0" encoding="ISO-8859-1"?> <list> <book id="1"> <author>Stephen King</author> <title>The Stand</title> </book> <book id="2"> <author>Stephen King</author> <title>The Shining</title> </book> </list>
def xml = Book.list().encodeAsXML() render xml
Automatic JSON Marshalling
Grails also supports automatic marshalling to JSON using the same mechanism. Simply substituteXML
with JSON
:render Book.list() as JSON
[ {"id":1, "class":"Book", "author":"Stephen King", "title":"The Stand"}, {"id":2, "class":"Book", "author":"Stephen King", "releaseDate":new Date(1194127343161), "title":"The Shining"} ]
encodeAsJSON
to achieve the same effect.
自动JSON编组
Grails同样也支持自动JSON编组的功能,这跟XML机制完全相同,因此只需要简单地将XML
替换为JSON
即可:render Book.list() as JSON
[ {"id":1, "class":"Book", "author":"Stephen King", "title":"The Stand"}, {"id":2, "class":"Book", "author":"Stephen King", "releaseDate":new Date(1194127343161), "title":"The Shining"} ]
encodeAsJSON
来达到相同的效果。
6.1.8 关于JSONBuilder
The previous section on on XML and JSON responses covered simplistic examples of rendering XML and JSON responses. Whilst the XML builder used by Grails is the standard XmlSlurper found in Groovy, the JSON builder is a custom implementation specific to Grails.
在以前关于XML和JSON响应的章节中,我们曾经简单地涉猎到渲染XML和JSON响应的例子。而Grails的XML生成器是Groovy标准的 XmlSlurper ,JSON生成器是Grails自己实现的一个规范。JSONBuilder and Grails versions
JSONBuilder behaves different depending on the version of Grails you use. For version below 1.2 the deprecated grails.web.JSONBuilder class is used. This section covers the usage of the Grails 1.2 JSONBuilderFor backwards compatibility the oldJSONBuilder
class is used with the render
method for older applications; to use the newer/better JSONBuilder
class set the following in Config.groovy
:grails.json.legacy.builder = false
JSONBuilder和Grails版本
JSONBuilder的行为根据你使用的Grails版本而有所不同。对于版本低于1.2,使用的是可以被废弃的grails.web.JSONBuilder类。本节将涉及到Grails 1.2 JSONBuilder的用法。因为要兼容以前版本的原因,旧的JSONBuilder
类被用于旧应用的render
方法;而要使用更新更好的JSONBuilder
类,需要在Config.groovy
中做如下设置:grails.json.legacy.builder = false
Rendering Simple Objects
To render a simple JSON object just set properties within the context of the Closure:render(contentType: "text/json") { hello = "world" }
{"hello":"world"}
渲染简单对象
要渲染简单的JSON对象,只需要在闭包的上下内设置属性即可:render(contentType: "text/json") { hello = "world" }
{"hello":"world"}
Rendering JSON Arrays
To render a list of objects simple assign a list:render(contentType: "text/json") {
categories = ['a', 'b', 'c']
}
{"categories":["a","b","c"]}
render(contentType: "text/json") { categories = [ { a = "A" }, { b = "B" } ] }
{"categories":[ {"a":"A"} , {"b":"B"}] }
element
method to return a list as the root:render(contentType: "text/json") {
element 1
element 2
element 3
}
[1,2,3]
渲染JSON数组
要渲染一个对象的列表,只需要简单给其赋值一个列表即可:render(contentType: "text/json") {
categories = ['a', 'b', 'c']
}
{"categories":["a","b","c"]}
render(contentType: "text/json") { categories = [ { a = "A" }, { b = "B" } ] }
{"categories":[ {"a":"A"} , {"b":"B"}] }
element
方法可以返回一个根范围的列表:render(contentType: "text/json") {
element 1
element 2
element 3
}
[1,2,3]
Rendering Complex Objects
Rendering complex objects can be done with Closures. For example:render(contentType: "text/json") { categories = ['a', 'b', 'c'] title = "Hello JSON" information = { pages = 10 } }
{"categories":["a","b","c"],"title":"Hello JSON","information":{"pages":10}}
渲染复杂对象
渲染复杂对象要在闭包内完成,比如:render(contentType: "text/json") { categories = ['a', 'b', 'c'] title = "Hello JSON" information = { pages = 10 } }
{"categories":["a","b","c"],"title":"Hello JSON","information":{"pages":10}}
Arrays of Complex Objects
As mentioned previously you can nest complex objects within arrays using Closures:render(contentType: "text/json") { categories = [ { a = "A" }, { b = "B" } ] }
array
method to build them up dynamically:def results = Book.list() render(contentType: "text/json") { books = array { for (b in results) { book title: b.title } } }
复杂对象数组
如前面所提到的,你可以在闭包内使用嵌套的复杂对象来实现数组:render(contentType: "text/json") { categories = [ { a = "A" }, { b = "B" } ] }
array
方法来动态地构建它们:def results = Book.list() render(contentType: "text/json") { books = array { for (b in results) { book title: b.title } } }
Direct JSONBuilder API Access
If you don't have access to therender
method, but still want to produce JSON you can use the API directly:def builder = new JSONBuilder()def result = builder.build { categories = ['a', 'b', 'c'] title = "Hello JSON" information = { pages = 10 } }// prints the JSON text println result.toString()def sw = new StringWriter() result.render sw
直接使用JSONBuilder API
如果你不能使用render
方法,你还是可以通过直接使用API来产生JSON的:def builder = new JSONBuilder()def result = builder.build { categories = ['a', 'b', 'c'] title = "Hello JSON" information = { pages = 10 } }// prints the JSON text println result.toString()def sw = new StringWriter() result.render sw
6.1.9 上传文件
Programmatic File Uploads
Grails supports file uploads using Spring's MultipartHttpServletRequest interface. The first step for file uploading is to create a multipart form like this:Upload Form: <br /> <g:uploadForm action="upload"> <input type="file" name="myFile" /> <input type="submit" /> </g:uploadForm>
uploadForm
tag conveniently adds the enctype="multipart/form-data"
attribute to the standard <g:form>
tag.There are then a number of ways to handle the file upload. One is to work with the Spring MultipartFile instance directly:def upload() { def f = request.getFile('myFile') if (f.empty) { flash.message = 'file cannot be empty' render(view: 'uploadForm') return } f.transferTo(new File('/some/local/dir/myfile.txt')) response.sendError(200, 'Done') }
InputStream
and so on with the MultipartFile interface.
上传文件编程
Grails通过Spring的MultipartHttpServletRequest接口来支持文件上传。第一步就是要为文件上传创建一个multipart的表单,比如:Upload Form: <br /> <g:uploadForm action="upload"> <input type="file" name="myFile" /> <input type="submit" /> </g:uploadForm>
uploadForm
标签在标准<g:form>
标签的基础上添加了enctype="multipart/form-data"
属性。此后就可以采用多种方式来处理文件上传。其一就是直接使用Spring的MultipartFile:def upload() { def f = request.getFile('myFile') if (f.empty) { flash.message = 'file cannot be empty' render(view: 'uploadForm') return } f.transferTo(new File('/some/local/dir/myfile.txt')) response.sendError(200, 'Done') }
InputStream
来操作文件等,不过这都依赖于MultipartFile接口。File Uploads through Data Binding
File uploads can also be performed using data binding. Consider thisImage
domain class:class Image { byte[] myFile static constraints = { // Limit upload file size to 2MB myFile maxSize: 1024 * 1024 * 2 } }
params
object in the constructor as in the example below, Grails will automatically bind the file's contents as a byte
to the myFile
property:def img = new Image(params)
byte
properties.It is also possible to set the contents of the file as a string by changing the type of the myFile
property on the image to a String type:class Image {
String myFile
}
文件上传和数据绑定
文件上传也可以通过数据绑定来完成。假设如下的Image
领域类:class Image { byte[] myFile static constraints = { // Limit upload file size to 2MB myFile maxSize: 1024 * 1024 * 2 } }
params
的构造方法来创建一个图像(image),那么Grails将会自动地把文件内容转换为byte
,并且绑定到myFile
属性上:def img = new Image(params)
byte
的,H2和MySQL缺省的blob大小是255字节。你也可以将myFile
属性的类型改成字符串来保存文件的内容:class Image {
String myFile
}
6.1.10 命令对象
Grails controllers support the concept of command objects. A command object is similar to a form bean in a framework like Struts, and they are useful for populating a subset of the properties needed to update a domain class. Or where there is no domain class required for the interaction, but you need features such as data binding and validation.
Grails控制器支持命令对象(command objects)的概念。一个命令对象类似于Struts中的一个表单(form)bean,当你想要更新领域类属性的一个子集的时候,或者虽然没有领域类,你还是需要数据绑定和校验特性的时候,是非常有用的,Declaring Command Objects
Command objects are typically declared in the same source file as a controller, directly below the controller class definition. For example:class UserController { … }class LoginCommand { String username String password static constraints = { username(blank: false, minSize: 6) password(blank: false, minSize: 6) } }
声明命令对象
命令对象通常声明在同一个控制器的源文件中,并且直接位于控制器类的下面。比如:class UserController { … }class LoginCommand { String username String password static constraints = { username(blank: false, minSize: 6) password(blank: false, minSize: 6) } }
Using Command Objects
To use command objects, controller actions may optionally specify any number of command object parameters. The parameter types must be supplied so that Grails knows what objects to create, populate and validate.Before the controller action is executed Grails will automatically create an instance of the command object class, populate its properties with by binding the request parameters, and validate the command object. For example:class LoginController { def login = { LoginCommand cmd -> if (cmd.hasErrors()) { redirect(action: 'loginForm') return } // work with the command object data } }
class LoginController { def login(LoginCommand cmd) { if (cmd.hasErrors()) { redirect(action: 'loginForm') return } // work with the command object data } }
使用命令对象
为了使用命令对象,控制器可以随意指定任何数目的命令对象参数。参数的类型是必需的,因为Grails需要知道什么样的对象被创建,写入和验证。在控制器的操作被执行之前,Grails将自动创建一个命令对象类的实例,用相应名字的请求参数写入到命令对象属性,并且验证它们,例如:class LoginController { def login = { LoginCommand cmd -> if (cmd.hasErrors()) { redirect(action: 'loginForm') return } // work with the command object data } }
class LoginController { def login(LoginCommand cmd) { if (cmd.hasErrors()) { redirect(action: 'loginForm') return } // work with the command object data } }
Command Objects and Dependency Injection
Command objects can participate in dependency injection. This is useful if your command object has some custom validation logic uses Grails services:class LoginCommand { def loginService String username String password static constraints = { username validator: { val, obj -> obj.loginService.canLogin(obj.username, obj.password) } } }
loginService
bean which is injected by name from the Spring ApplicationContext
.
命令对象和依赖注入
命令对象也支持依赖注入。这在你自定义校验的逻辑依赖Grails的服务时,非常有用:class LoginCommand { def loginService String username String password static constraints = { username validator: { val, obj -> obj.loginService.canLogin(obj.username, obj.password) } } }
ApplicationContext
中的loginService
进行交互。
6.1.11 处理重复的表单提交
Grails has built-in support for handling duplicate form submissions using the "Synchronizer Token Pattern". To get started you define a token on the form tag:Then in your controller code you can use the withForm method to handle valid and invalid requests:If you only provide the withForm method and not the chained
Grails内置了对表单重复提交的处理,其使用的模式是“同步标志模式(Synchronizer Token Pattern)”。作为开始,你需要先在form标签中定义一个标志:<g:form useToken="true" ...>
withForm { // good request }.invalidToken { // bad request }
invalidToken
method then by default Grails will store the invalid token in a flash.invalidToken
variable and redirect the request back to the original page. This can then be checked in the view:<g:if test="${flash.invalidToken}"> Don't click the button twice! </g:if>
The withForm tag makes use of the session and hence requires session affinity or clustered sessions if used in a cluster.
<g:form useToken="true" ...>
withForm { // good request }.invalidToken { // bad request }
invalidToken
方法的话,Grails将缺省地存储无效的标志到flash.invalidToken
变量中,并且将请求重定向到上一个原始页面。这样就可以在视图中检查了:<g:if test="${flash.invalidToken}"> Don't click the button twice! </g:if>
withForm标签使用的是session,因此要求是兼容会话的或者支持集群的会话-如果在集群中使用的话。
6.1.12 简单类型转换器
Type Conversion Methods
If you prefer to avoid the overhead of Data Binding and simply want to convert incoming parameters (typically Strings) into another more appropriate type the params object has a number of convenience methods for each type:def total = params.int('total')
int
method, and there are also methods for boolean
, long
, char
, short
and so on. Each of these methods is null-safe and safe from any parsing errors, so you don't have to perform any additional checks on the parameters.Each of the conversion methods allows a default value to be passed as an optional second argument. The default value will be returned if a corresponding entry cannot be found in the map or if an error occurs during the conversion. Example:def total = params.int('total', 42)
attrs
parameter of GSP tags.
类型转换方法
如果你倾向于避免数据绑定的开销,而只想简单地将输入参数(通常是字符串)转换为另外更合适地类型,可以通过params对象提供的一些便利方法来实现:def total = params.int('total')
int
方法,除此之外还有boolean
、long
、char
、short
等方法。每一个方法都是空指针安全的(null-safe)和类型解析安全的,因此你也就不需要执行任何额外的参数检查了。每一个转换方法都允许将一个缺省值传递给第二个可选参数。如果映射中没有找到对应的实体或者进行转换的时候出现了错误,此缺省值将被返回。比如:def total = params.int('total', 42)
attrs
参数。Handling Multi Parameters
A common use case is dealing with multiple request parameters of the same name. For example you could get a query string such as?name=Bob&name=Judy
.In this case dealing with one parameter and dealing with many has different semantics since Groovy's iteration mechanics for String
iterate over each character. To avoid this problem the params object provides a list
method that always returns a list:for (name in params.list('name')) {
println name
}
处理多个重名参数
我们会经常碰到要处理多个请求参数名相同的情况。比如得到你得到一个内容是?name=Bob&name=Judy
的查询串这种情况下,处理一个参数和多个参数的语法是有些不同的,因为Groovy的String
迭代是基于字符的。要避免此问题,可以使用params对象提供的list
方法,此方法总是返回一个列表:for (name in params.list('name')) {
println name
}
6.1.13 异步请求处理
Grails support asynchronous request processing as provided by the Servlet 3.0 specification. To enable the async features you need to set your servlet target version to 3.0 in BuildConfig.groovy:With that done ensure you do a clean re-compile as some async features are enabled at compile time.
Grails支持Servlet 3.0规范的异步请求处理。要使异步功能生效,你需要在BuildConfig.groovy中设置servlet的版本为3.0:grails.servlet.version = "3.0"
With a Servlet target version of 3.0 you can only deploy on Servlet 3.0 containers such as Tomcat 7 and above.
grails.servlet.version = "3.0"
使用Servlet 3.0版本以后,你只能将应用部署于支持Servlet 3.0的容器中,比如Tomcat 7及其以上版本。
Asynchronous Rendering
You can render content (templates, binary data etc.) in an asynchronous manner by calling thestartAsync
method which returns an instance of the Servlet 3.0 AsyncContext
. Once you have a reference to the AsyncContext
you can use Grails' regular render method to render content:def index() { def ctx = startAsync() ctx.start { new Book(title:"The Stand").save() render template:"books", model:[books:Book.list()] ctx.complete() } }
complete()
method to terminate the connection.
异步渲染
你可以通过调用startAsync
方法的方式进行异步的内容渲染(比如模板、二进制数据等),此方法的返回值是Servlet 3.0 AsyncContext
的一个实例。一旦获取到了AsyncContext
的引用,你就可以使用Grails的render方法来渲染内容了:def index() { def ctx = startAsync() ctx.start { new Book(title:"The Stand").save() render template:"books", model:[books:Book.list()] ctx.complete() } }
complete()
方法来中止此连接。Resuming an Async Request
You resume processing of an async request (for example to delegate to view rendering) by using thedispatch
method of the AsyncContext
class:def index() {
def ctx = startAsync()
ctx.start {
// do working
…
// render view
ctx.dispatch()
}
}
恢复一个异步请求
你可以通过AsyncContext
类的dispatch
方法来恢复一个异步请求(比如将其代理到一个视图):def index() {
def ctx = startAsync()
ctx.start {
// do working
…
// render view
ctx.dispatch()
}
}
6.2 Groovy服务器页面(GSP)
Groovy Servers Pages (or GSP for short) is Grails' view technology. It is designed to be familiar for users of technologies such as ASP and JSP, but to be far more flexible and intuitive.GSPs live in the A GSP is typically a mix of mark-up and GSP tags which aid in view rendering.This action will look up a
Groovy服务器页面(或者简称为GSP)是Grails的视图技术。它是专为熟悉ASP和JSP技术的用户而设计,不过更加灵活和直观。GSPs位于grails-app/views
directory and are typically rendered automatically (by convention) or with the render method such as:render(view: "index")
Although it is possible to have Groovy logic embedded in your GSP and doing this will be covered in this document, the practice is strongly discouraged. Mixing mark-up and code is a bad thing and most GSP pages contain no code and needn't do so.A GSP typically has a "model" which is a set of variables that are used for view rendering. The model is passed to the GSP view from a controller. For example consider the following controller action:
def show() { [book: Book.get(params.id)] }
Book
instance and create a model that contains a key called book
. This key can then be referenced within the GSP view using the name book
:${book.title}
grails-app/views
目录下边,通常情况下是自动渲染的(基于规约)或者通过render方法,比如:render(view: "index")
虽然在你的GSP中可以使用内嵌的Groovy逻辑(本文档将会涉及),但是作为最佳实践,是非常不鼓励这么做的。混合使用标记和代码是一件很 不好 的事情,而且大多数的GSP页面无需也不必包含代码。GSP通常都会有一个用以视图渲染所需的变量集合"模型(model)",此模型是从控制器传递到GSP视图的。以如下的控制器操作为例:
def show() { [book: Book.get(params.id)] }
Book
实例,并且创建一个包含键book
的模型。 此键在GSP视图中可以通过名字book
来引用:${book.title}
6.2.1 GSP基础
In the next view sections we'll go through the basics of GSP and what is available to you. First off let's cover some basic syntax that users of JSP and ASP should be familiar with.GSP supports the usage of You can also use the GSP also supports JSP-style server-side comments (which are not rendered in the HTML response) as the following example demonstrates:
在下一个视图章节中,我们将涉及GSP的基础部分以及那些是你所需的。本节首先简单介绍一些基础的语法,对于JSP和ASP用户来说,这些应该都很熟悉。GSP支持内嵌Groovy代码的用法(再次强调,不提倡这样用)是通过<% %>
scriptlet blocks to embed Groovy code (again this is discouraged):<html> <body> <% out << "Hello GSP!" %> </body> </html>
<%= %>
syntax to output values:<html> <body> <%="Hello GSP!" %> </body> </html>
<html> <body> <%-- This is my comment --%> <%="Hello GSP!" %> </body> </html>
<% %>
的脚本代码块的来实现的,比如:<html> <body> <% out << "Hello GSP!" %> </body> </html>
<%= %>
的语法来输出:<html> <body> <%="Hello GSP!" %> </body> </html>
<html> <body> <%-- This is my comment --%> <%="Hello GSP!" %> </body> </html>
6.2.1.1 变量和作用域
Within the and then access those variables later in the page:Within the scope of a GSP there are a number of pre-defined variables, including:
在<% %>
brackets you can declare variables:<% now = new Date() %>
<%=now%>
application
- The javax.servlet.ServletContext instanceapplicationContext
The Spring ApplicationContext instanceflash
- The flash objectgrailsApplication
- The GrailsApplication instanceout
- The response writer for writing to the output streamparams
- The params object for retrieving request parametersrequest
- The HttpServletRequest instanceresponse
- The HttpServletResponse instancesession
- The HttpSession instancewebRequest
- The GrailsWebRequest instance
<% %>
内,你可以声明变量:<% now = new Date() %>
<%=now%>
application
- javax.servlet.ServletContext实例applicationContext
Spring的ApplicationContext实例flash
- flash对象grailsApplication
- GrailsApplication实例out
- 响应输出流输出器params
- params对象,用于接收请求参数request
- HttpServletRequest实例response
- HttpServletResponse实例session
- HttpSession实例webRequest
- GrailsWebRequest实例
6.2.1.2 逻辑和迭代
Using the As well as logical branching:
使用<% %>
syntax you can embed loops and so on using this syntax:<html> <body> <% [1,2,3,4].each { num -> %> <p><%="Hello ${num}!" %></p> <%}%> </body> </html>
<html> <body> <% if (params.hello == 'true')%> <%="Hello!"%> <% else %> <%="Goodbye!"%> </body> </html>
<% %>
语法你可以内嵌循环之类的用法,其语法如下:<html> <body> <% [1,2,3,4].each { num -> %> <p><%="Hello ${num}!" %></p> <%}%> </body> </html>
<html> <body> <% if (params.hello == 'true')%> <%="Hello!"%> <% else %> <%="Goodbye!"%> </body> </html>
6.2.1.3 页面指令
GSP also supports a few JSP-style page directives.The import directive lets you import classes into the page. However, it is rarely needed due to Groovy's default imports and GSP Tags:GSP also supports the contentType directive:The contentType directive allows using GSP to render other formats.
GSP也支持一些JSP风格的页面指令。import指令可以让你将Java类导入到页面中。但是它应该很少使用,因为已经有Groovy缺省导入和GSP标签:<%@ page import="java.awt.*" %>
<%@ page contentType="text/json" %>
<%@ page import="java.awt.*" %>
<%@ page contentType="text/json" %>
6.2.1.4 表达式
In GSP the However, unlike JSP EL you can have any Groovy expression within the Other possible values are 'none' (for no default encoding) and 'base64'.
在GSP中,一开始所介绍的<%= %>
syntax introduced earlier is rarely used due to the support for GSP expressions. A GSP expression is similar to a JSP EL expression or a Groovy GString and takes the form ${expr}
:<html> <body> Hello ${params.name} </body> </html>
${..}
block. Variables within the ${..}
block are not escaped by default, so any HTML in the variable's string is rendered directly to the page. To reduce the risk of Cross-site-scripting (XSS) attacks, you can enable automatic HTML escaping with the grails.views.default.codec
setting in grails-app/conf/Config.groovy
:grails.views.default.codec='html'
<%= %>
语法是很少被应用于GSP表达式的。一个GSP表达式类似于JSP EL表达式或者Groovy GString,使用的是${expr}
形式:<html> <body> Hello ${params.name} </body> </html>
${..}
代码块中使用任意Groovy表达式。${..}
块中的变量缺省是 不 被转义的,因此变量中字符串将会直接被渲染到页面HTML。要减少这种Cross-site-scripting (XSS)攻击风险,你可以使用自动HTML转义来避免,只需要在grails-app/conf/Config.groovy
中配置grails.views.default.codec
即可:grails.views.default.codec='html'
6.2.2 GSP标签
Now that the less attractive JSP heritage has been set aside, the following sections cover GSP's built-in tags, which are the preferred way to define GSP pages.GSP tags can also have a body such as:Expressions can be passed into GSP tag attributes, if an expression is not used it will be assumed to be a String value:Maps can also be passed into GSP tag attributes, which are often used for a named parameter style syntax:Note that within the values of attributes you must use single quotes for Strings:With the basic syntax out the way, the next sections look at the tags that are built into Grails by default.
现在没有吸引力的JSP遗留部分已经被废除了,那么接下来的章节,我们将讨论GSP的内置标签,它们是定义GSP页面非常有力的方法。The section on Tag Libraries covers how to add your own custom tag libraries.All built-in GSP tags start with the prefix
g:
. Unlike JSP, you don't specify any tag library imports. If a tag starts with g:
it is automatically assumed to be a GSP tag. An example GSP tag would look like:<g:example />
<g:example> Hello world </g:example>
<g:example attr="${new Date()}"> Hello world </g:example>
<g:example attr="${new Date()}" attr2="[one:1, two:2, three:3]"> Hello world </g:example>
<g:example attr="${new Date()}" attr2="[one:'one', two:'two']"> Hello world </g:example>
标签库章节讨论的是如何添加你自己定制的标签库所有内置的GSP标签都是以前缀
g:
开始的。跟JSP不同的是,你不需要指定任何的标签库导入。如果一个标签以g:
开头,那么将会自动地被当作GSP标签看待。一个GSP的标签的样子如下所示:<g:example />
<g:example> Hello world </g:example>
<g:example attr="${new Date()}"> Hello world </g:example>
<g:example attr="${new Date()}" attr2="[one:1, two:2, three:3]"> Hello world </g:example>
<g:example attr="${new Date()}" attr2="[one:'one', two:'two']"> Hello world </g:example>
6.2.2.1 变量和作用域
Variables can be defined within a GSP using the set tag:Here we assign a variable called Variables can also be placed in one of the following scopes:
在GSP中,可以通过set标签来定义变量:<g:set var="now" value="${new Date()}" />
now
to the result of a GSP expression (which simply constructs a new java.util.Date
instance). You can also use the body of the <g:set>
tag to define a variable:<g:set var="myHTML"> Some re-usable code on: ${new Date()} </g:set>
page
- Scoped to the current page (default)request
- Scoped to the current requestflash
- Placed within flash scope and hence available for the next requestsession
- Scoped for the user sessionapplication
- Application-wide scope.
scope
attribute:<g:set var="now" value="${new Date()}" scope="request" />
<g:set var="now" value="${new Date()}" />
java.util.Date
实例)的结果赋值给now
变量。你也可以使用<g:set>
标签的主体来定义一个变量:<g:set var="myHTML"> Some re-usable code on: ${new Date()} </g:set>
page
- 作用于当前页面(缺省)request
- 作用于当前请求flash
- 置于flash作用域内,因此在下一个请求中是有效的session
- 作用于用户会话application
- 应用级别的作用域
scope
属性:<g:set var="now" value="${new Date()}" scope="request" />
6.2.2.2 逻辑和迭代
GSP also supports logical and iterative tags out of the box. For logic there are if, else and elseif tags for use with branching:Use the each and while tags for iteration:
GSP也支持逻辑和迭代地标签。if、else和elseif标签用于逻辑,用以处理分支:<g:if test="${session.role == 'admin'}"> <%-- show administrative functions --%> </g:if> <g:else> <%-- show basic functions --%> </g:else>
<g:each in="${[1,2,3]}" var="num"> <p>Number ${num}</p> </g:each><g:set var="num" value="${1}" /> <g:while test="${num < 5 }"> <p>Number ${num++}</p> </g:while>
<g:if test="${session.role == 'admin'}"> <%-- show administrative functions --%> </g:if> <g:else> <%-- show basic functions --%> </g:else>
<g:each in="${[1,2,3]}" var="num"> <p>Number ${num}</p> </g:each><g:set var="num" value="${1}" /> <g:while test="${num < 5 }"> <p>Number ${num++}</p> </g:while>
6.2.2.3 搜索和过滤
If you have collections of objects you often need to sort and filter them. Use the findAll and grep tags for these tasks:The Or using a regular expression:The above example is also interesting due to its usage of GPath. GPath is an XPath-like language in Groovy. The
如果你的对象是集合,那么你经常需要排序和过滤。使用findAll和grep标签可以完成这些任务:Stephen King's Books: <g:findAll in="${books}" expr="it.author == 'Stephen King'"> <p>Title: ${it.title}</p> </g:findAll>
expr
attribute contains a Groovy expression that can be used as a filter. The grep tag does a similar job, for example filtering by class:<g:grep in="${books}" filter="NonFictionBooks.class"> <p>Title: ${it.title}</p> </g:grep>
<g:grep in="${books.title}" filter="~/.*?Groovy.*?/"> <p>Title: ${it}</p> </g:grep>
books
variable is a collection of Book
instances. Since each Book
has a title
, you can obtain a list of Book titles using the expression books.title
. Groovy will auto-magically iterate the collection, obtain each title, and return a new list!
Stephen King's Books: <g:findAll in="${books}" expr="it.author == 'Stephen King'"> <p>Title: ${it.title}</p> </g:findAll>
expr
属性使用一个Groovy表达式来作为过滤器。grep标签完成类似的任务,比如要过滤对象类:<g:grep in="${books}" filter="NonFictionBooks.class"> <p>Title: ${it.title}</p> </g:grep>
<g:grep in="${books.title}" filter="~/.*?Groovy.*?/"> <p>Title: ${it}</p> </g:grep>
books
变量是一个Book
实例的集合。因为每一个Book
都有title
,你可以使用表达式books.title
来获取Book标题的列表。Groovy将会自动地对集合迭代,获取每一个标题,最终返回一个新的列表。
6.2.2.4 链接和资源
GSP also features tags to help you manage linking to controllers and actions. The link tag lets you specify controller and action name pairing and it will automatically work out the link based on the URL Mappings, even if you change them! For example:
GSP标签也能帮助你来管理控制器和操作的超链接。link标签让你来指定控制器和操作名称对,并且标签会自动生成基于URL映射的链接,即使映射改变了也没有问题,比如:<g:link action="show" id="1">Book 1</g:link><g:link action="show" id="${currentBook.id}">${currentBook.name}</g:link><g:link controller="book">Book Home</g:link><g:link controller="book" action="list">Book List</g:link><g:link url="[action: 'list', controller: 'book']">Book List</g:link><g:link params="[sort: 'title', order: 'asc', author: currentBook.author]" action="list">Book List</g:link>
<g:link action="show" id="1">Book 1</g:link><g:link action="show" id="${currentBook.id}">${currentBook.name}</g:link><g:link controller="book">Book Home</g:link><g:link controller="book" action="list">Book List</g:link><g:link url="[action: 'list', controller: 'book']">Book List</g:link><g:link params="[sort: 'title', order: 'asc', author: currentBook.author]" action="list">Book List</g:link>
6.2.2.5 表单和字段
Form Basics
GSP supports many different tags for working with HTML forms and fields, the most basic of which is the form tag. This is a controller/action aware version of the regular HTML form tag. Theurl
attribute lets you specify which controller and action to map to:<g:form name="myForm" url="[controller:'book',action:'list']">...</g:form>
myForm
that submits to the BookController
's list
action. Beyond that all of the usual HTML attributes apply.
表单基础
GSP有很多不同的标签来支持HTML表单和字段,不过最基础的还是form标签。常规的HTML表单标签支持controller/action属性,而url
属性让你以映射(map)的方式来指定controller和action:<g:form name="myForm" url="[controller:'book',action:'list']">...</g:form>
myForm
表单,它将会提交到BookController
控制器的list
操作。此外HTML的所有通用属性都可以使用。Form Fields
In addition to easy construction of forms, GSP supports custom tags for dealing with different types of fields, including:- textField - For input fields of type 'text'
- passwordField - For input fields of type 'password'
- checkBox - For input fields of type 'checkbox'
- radio - For input fields of type 'radio'
- hiddenField - For input fields of type 'hidden'
- select - For dealing with HTML select boxes
<g:textField name="myField" value="${myValue}" />
表单字段
除了轻松地构造表单之外,GSP自定义的标签支持不同的字段类型,包括:- textField - 针对类型是'text'的输入字段
- passwordField - 针对类型是'password'的输入字段
- checkBox - 针对类型是'checkbox'的输入字段
- radio - 针对类型是'radio'的输入字段
- hiddenField - 针对类型是'hidden'的输入字段
- select - 针对HTML的下拉框(select boxes)
<g:textField name="myField" value="${myValue}" />
Multiple Submit Buttons
The age old problem of dealing with multiple submit buttons is also handled elegantly with Grails using the actionSubmit tag. It is just like a regular submit, but lets you specify an alternative action to submit to:<g:actionSubmit value="Some update label" action="update" />
多个提交按钮
处理多个提交按钮这一个古老的问题,也得到优雅的解决,那就是使用Grails的actionSubmit标签。跟常规的提交类似,只不过你可以指定另外一个操作来提交:<g:actionSubmit value="Some update label" action="update" />
6.2.2.6 标签的方法调用
One major different between GSP tags and other tagging technologies is that GSP tags can be called as either regular tags or as method calls from controllers, tag libraries or GSP views.This is particularly useful for using a tag within an attribute:In view technologies that don't support this feature you have to nest tags within tags, which becomes messy quickly and often has an adverse effect of WYSWIG tools such as Dreamweaver that attempt to render the mark-up as it is not well-formed:
One major different between GSP tags and other tagging technologies is that GSP tags can be called as either regular tags or as method calls from controllers, tag libraries or GSP views.Tags as method calls from GSPs
Tags return their results as a String-like object (aStreamCharBuffer
which has all of the same methods as String) instead of writing directly to the response when called as methods. For example:Static Resource: ${createLinkTo(dir: "images", file: "logo.jpg")}
<img src="${createLinkTo(dir: 'images', file: 'logo.jpg')}" />
<img src="<g:createLinkTo dir="images" file="logo.jpg" />" />
在GSP中以方法调用标签
当标签以方法的方式调用时,其返回一个类似String(一个StreamCharBuffer
,有着跟String完全相同的方法)的对象,而不是直接写回到响应器。比如:Static Resource: ${createLinkTo(dir: "images", file: "logo.jpg")}
<img src="${createLinkTo(dir: 'images', file: 'logo.jpg')}" />
<img src="<g:createLinkTo dir="images" file="logo.jpg" />" />
Tags as method calls from Controllers and Tag Libraries
You can also invoke tags from controllers and tag libraries. Tags within the defaultg:
namespace can be invoked without the prefix and a StreamCharBuffer
result is returned:def imageLocation = createLinkTo(dir:"images", file:"logo.jpg").toString()
def imageLocation = g.createLinkTo(dir:"images", file:"logo.jpg").toString()
def editor = fckeditor.editor(name: "text", width: "100%", height: "400")
在控制器和标签库中的以方法调用标签
你可以可以在控制器和标签库中调用标签。命名空间是g:
的标签调用可以忽略其前缀,并且一个StreamCharBuffer
类型的结果被返回:def imageLocation = createLinkTo(dir:"images", file:"logo.jpg").toString()
def imageLocation = g.createLinkTo(dir:"images", file:"logo.jpg").toString()
def editor = fckeditor.editor(name: "text", width: "100%", height: "400")
6.2.3 视图和模板
Grails also has the concept of templates. These are useful for partitioning your views into maintainable chunks, and combined with Layouts provide a highly re-usable mechanism for structured views.Use the render tag to render this template from one of the views in Notice how we pass into a model to use using the
Grails也有模板的概念。这对于将你的视图分割成可维护的模块也是颇有裨益的,并且结合布局还可为结构化视图提供一个高复用机制。Template Basics
Grails uses the convention of placing an underscore before the name of a view to identify it as a template. For example, you might have a template that renders Books located atgrails-app/views/book/_bookTemplate.gsp
:<div class="book" id="${book?.id}"> <div>Title: ${book?.title}</div> <div>Author: ${book?.author?.name}</div> </div>
grails-app/views/book
:<g:render template="bookTemplate" model="[book: myBook]" />
model
attribute of the render
tag. If you have multiple Book
instances you can also render the template for each Book
using the render tag with a collection
attribute:<g:render template="bookTemplate" var="book" collection="${bookList}" />
模板基础
Grails使用在其视图名称前放置一个下划线的方式来标识一个模板。比如,你可能有一个渲染Books的模板,位于grails-app/views/book/_bookTemplate.gsp
:<div class="book" id="${book?.id}"> <div>Title: ${book?.title}</div> <div>Author: ${book?.author?.name}</div> </div>
grails-app/views/book
中的一个视图中,使用render标签来渲染此模板:<g:render template="bookTemplate" model="[book: myBook]" />
render
标签的model
属性来传递模型的。如果你有多个Book
实例,你还可以通过render
标签的collection
属性来为每一个Book
渲染模板:<g:render template="bookTemplate" var="book" collection="${bookList}" />
Shared Templates
In the previous example we had a template that was specific to theBookController
and its views at grails-app/views/book
. However, you may want to share templates across your application.In this case you can place them in the root views directory at grails-app/views or any subdirectory below that location, and then with the template attribute use an absolute location starting with /
instead of a relative location. For example if you had a template called grails-app/views/shared/_mySharedTemplate.gsp
, you would reference it as:<g:render template="/shared/mySharedTemplate" />
<g:render template="/book/bookTemplate" model="[book: myBook]" />
共享的模板
在上一个示例中,我们有了一个跟BookController
相关的模板,其视图都位于grails-app/views/book
中。然而,有时候,你可能想将你的模板在整个应用中共享。在这种情况下,你可以将模板放在grails-app/views这个视图根目录下,或者跟目录下的任意子目录中,然后在template属性中使用以/
开头的绝对位置而非相对位置。比如,你有一个grails-app/views/shared/_mySharedTemplate.gsp
模板,你就可以这样引用:<g:render template="/shared/mySharedTemplate" />
<g:render template="/book/bookTemplate" model="[book: myBook]" />
The Template Namespace
Since templates are used so frequently there is template namespace, calledtmpl
, available that makes using templates easier. Consider for example the following usage pattern:<g:render template="bookTemplate" model="[book:myBook]" />
tmpl
namespace as follows:<tmpl:bookTemplate book="${myBook}" />
模板的命名空间
因为模板是如此频繁地被使用,因此tmpl
这个模板命名空间就产生了,这样模板的使用也更简易。比如下例所示地用法:<g:render template="bookTemplate" model="[book:myBook]" />
tmpl
命名空间的表达如下所示:<tmpl:bookTemplate book="${myBook}" />
Templates in Controllers and Tag Libraries
You can also render templates from controllers using the render controller method. This is useful for Ajax applications where you generate small HTML or data responses to partially update the current page instead of performing new request:def bookData() {
def b = Book.get(params.id)
render(template:"bookTemplate", model:[book:b])
}
def bookData() { def b = Book.get(params.id) String content = g.render(template:"bookTemplate", model:[book:b]) render content }
g
namespace which tells Grails we want to use the tag as method call instead of the render method.
控制器和标签库的模板
你还可以在控制器中使用render方法来渲染模板。这在Ajax的应用中是非常有用的,你可以通过生成小的HTML或者数据响应来部分的更新当前页面,而不是发起一个新的请求:def bookData() {
def b = Book.get(params.id)
render(template:"bookTemplate", model:[book:b])
}
def bookData() { def b = Book.get(params.id) String content = g.render(template:"bookTemplate", model:[book:b]) render content }
g
命名空间的用法,它会让Grails知道我们想用标签的方法调用,而不是render方法。
6.2.4 使用Sitemesh布局
Creating Layouts
Grails leverages Sitemesh, a decorator engine, to support view layouts. Layouts are located in thegrails-app/views/layouts
directory. A typical layout can be seen below:<html> <head> <title><g:layoutTitle default="An example decorator" /></title> <g:layoutHead /> </head> <body onload="${pageProperty(name:'body.onload')}"> <div class="menu"><!--my common menu goes here--></menu> <div class="body"> <g:layoutBody /> </div> </div> </body> </html>
layoutTitle
- outputs the target page's titlelayoutHead
- outputs the target page's head tag contentslayoutBody
- outputs the target page's body tag contents
创建布局
Grails使用Sitemesh(一个装饰引擎)来支持视图布局。布局位于grails-app/views/layouts
目录下边。一个典型的布局可以如下所示:<html> <head> <title><g:layoutTitle default="An example decorator" /></title> <g:layoutHead /> </head> <body onload="${pageProperty(name:'body.onload')}"> <div class="menu"><!--my common menu goes here--></menu> <div class="body"> <g:layoutBody /> </div> </div> </body> </html>
layoutTitle
- 输出目标页面的标题(title)layoutHead
- 输出目标页面的head标签的内容layoutBody
- 输出目标页面的body标签的内容
Triggering Layouts
There are a few ways to trigger a layout. The simplest is to add a meta tag to the view:<html> <head> <title>An Example Page</title> <meta name="layout" content="main" /> </head> <body>This is my content!</body> </html>
grails-app/views/layouts/main.gsp
will be used to layout the page. If we were to use the layout from the previous section the output would resemble this:<html> <head> <title>An Example Page</title> </head> <body onload=""> <div class="menu"><!--my common menu goes here--></div> <div class="body"> This is my content! </div> </body> </html>
触发布局
有几种方法来触发一个布局。最简单的一种就是在视图中增加一个meta标签:<html> <head> <title>An Example Page</title> <meta name="layout" content="main" /> </head> <body>This is my content!</body> </html>
grails-app/views/layouts/main.gsp
的布局将被用于安排页面。如果我们使用上一小节的布局,那么其输出类似下面所示:<html> <head> <title>An Example Page</title> </head> <body onload=""> <div class="menu"><!--my common menu goes here--></div> <div class="body"> This is my content! </div> </body> </html>
Specifying A Layout In A Controller
Another way to specify a layout is to specify the name of the layout by assigning a value to the "layout" property in a controller. For example, if you have a controller such as:class BookController {
static layout = 'customer' def list() { … }
}
grails-app/views/layouts/customer.gsp
which will be applied to all views that the BookController
delegates to. The value of the "layout" property may contain a directory structure relative to the grails-app/views/layouts/
directory. For example:class BookController {
static layout = 'custom/customer' def list() { … }
}
grails-app/views/layouts/custom/customer.gsp
template.
在控制器中指定一个布局
另外一种指定布局的方法是在控制器中为"layout"属性赋值一个布局的名称。举一个例子,假设你有如下一个控制器:class BookController {
static layout = 'customer' def list() { … }
}
grails-app/views/layouts/customer.gsp
布局,这样BookController
所有的所有视图将使用此布局。"layout"属性的值还可以包含一个目录结构,不过要相对于grails-app/views/layouts/
目录。比如:class BookController {
static layout = 'custom/customer' def list() { … }
}
grails-app/views/layouts/custom/customer.gsp
模板来装饰。Layout by Convention
Another way to associate layouts is to use "layout by convention". For example, if you have this controller:class BookController { def list() { … } }
grails-app/views/layouts/book.gsp
, which will be applied to all views that the BookController
delegates to.Alternatively, you can create a layout called grails-app/views/layouts/book/list.gsp
which will only be applied to the list
action within the BookController
.If you have both the above mentioned layouts in place the layout specific to the action will take precedence when the list action is executed.If a layout may not be located using any of those conventions, the convention of last resort is to look for the application default layout which
is grails-app/views/layouts/application.gsp
. The name of the application default layout may be changed by defining a property
in grails-app/conf/Config.groovy
as follows:grails.sitemesh.default.layout = 'myLayoutName'
grails-app/views/layouts/myLayoutName.gsp
.
布局规约
另外一种关联布局的方式是使用"布局规约"。比如,你的控制器如下所示:class BookController { def list() { … } }
grails-app/views/layouts/book.gsp
布局,此布局将会应用于一个BookController
的所有视图。此外,你也可以创建一个grails-app/views/layouts/book/list.gsp
布局,其用于BookController
的list
操作。如果你有如上所述的两个布局,那么当list操作被执行时,跟操作相关的布局将优先使用。如果一个布局在这些约定中没有找到,那么此规约的最后顺序是查找应用的缺省布局grails-app/views/layouts/application.gsp
。应用的缺省布局名称可以通过修改grails-app/conf/Config.groovy
中的属性来改变,比如:grails.sitemesh.default.layout = 'myLayoutName'
grails-app/views/layouts/myLayoutName.gsp
。Inline Layouts
Grails' also supports Sitemesh's concept of inline layouts with the applyLayout tag. This can be used to apply a layout to a template, URL or arbitrary section of content. This lets you even further modularize your view structure by "decorating" your template includes.Some examples of usage can be seen below:<g:applyLayout name="myLayout" template="bookTemplate" collection="${books}" /><g:applyLayout name="myLayout" url="http://www.google.com" /><g:applyLayout name="myLayout"> The content to apply a layout to </g:applyLayout>
内联布局
Grails同样支持Sitemesh的内联布局概念,可以使用applyLayout标签来实现。此标签可以将一个布局应用于一个模板,URL或者任意部分的内容。更甚者,通过“装饰”你的模板你可以将你的视图模块化。这些用法的一些示例如下:<g:applyLayout name="myLayout" template="bookTemplate" collection="${books}" /><g:applyLayout name="myLayout" url="http://www.google.com" /><g:applyLayout name="myLayout"> The content to apply a layout to </g:applyLayout>
Server-Side Includes
While the applyLayout tag is useful for applying layouts to external content, if you simply want to include external content in the current page you use the include tag:<g:include controller="book" action="list" />
<g:applyLayout name="myLayout"> <g:include controller="book" action="list" /> </g:applyLayout>
def content = include(controller:"book", action:"list")
服务器端的包含
applyLayout标签可以将布局应用到外部内容,而如果你只想简单地将外部内容包含到当前页面,你可以使用include标签:<g:include controller="book" action="list" />
<g:applyLayout name="myLayout"> <g:include controller="book" action="list" /> </g:applyLayout>
def content = include(controller:"book", action:"list")
6.2.5 静态资源
Grails 2.0 integrates with the Resources plugin to provide sophisticated static resource management. This plugin is installed by default in new Grails applications.The basic way to include a link to a static resource in your application is to use the resource tag. This simple approach creates a URI pointing to the file.However modern applications with dependencies on multiple JavaScript and CSS libraries and frameworks (as well as dependencies on multiple Grails plugins) require something more powerful.The issues that the Resources framework tackles are:
Grails 2.0集成了资源插件以提供更复杂的静态资源管理。此插件在新建Grails应用中是缺省安装的。在你的应用中要引用一个静态资源链接的基本方法,就是使用resource标签。此种方式会创建一个指向文件的URI。但是,现在的应用往往会依赖于多个JavaScript、CSS库和框架(即依赖于多个Grails插件),这就要求一些更强大的功能来支撑。本资源(Resources)框架要解决的主要问题如下:
- Web application performance tuning is difficult
- Correct ordering of resources, and deferred inclusion of JavaScript
- Resources that depend on others that must be loaded first
- The need for a standard way to expose static resources in plugins and applications
- The need for an extensible processing chain to optimize resources
- Preventing multiple inclusion of the same resource
- Web应用的性能调优是非常困难的
- 正确地对资源排序,推迟引用JavaScript
- 依赖于其他的资源必须要优先加载
- 在插件和应用中需要采用一种标准的方式来暴露静态资源
- 需要扩展性更好的处理链来优化资源
- 阻止同样的资源被多次引用
6.2.5.1 通过资源标签引用资源
Pulling in resources with r:require
To use resources, your GSP page must indicate which resource modules it requires. For example with the jQuery plugin, which exposes a "jquery" resource module, to use jQuery in any page on your site you simply add:<html> <head> <r:require module="jquery"/> <r:layoutResources/> </head> <body> … <r:layoutResources/> </body> </html>
r:require
multiple times in a GSP page, and you use the "modules" attribute to provide a list of modules:<html> <head> <r:require modules="jquery, main, blueprint, charting"/> <r:layoutResources/> </head> <body> … <r:layoutResources/> </body> </html>
使用r:require获取资源
要使用资源,你的GSP页面必须要知道那些资源模块是所需要的。以jQuery插件为例,其导出了一个"jquery"的资源模块,要在你站点的任何页面使用jQuery,你需要简单地增加如下代码:<html> <head> <r:require module="jquery"/> <r:layoutResources/> </head> <body> … <r:layoutResources/> </body> </html>
r:require
,也可以使用"modules"属性提供一个模块列表:<html> <head> <r:require modules="jquery, main, blueprint, charting"/> <r:layoutResources/> </head> <body> … <r:layoutResources/> </body> </html>
Rendering the links to resources with r:layoutResources
When you have declared the resource modules that your GSP page requires, the framework needs to render the links to those resources at the correct time.To achieve this correctly, you must include the r:layoutResources tag twice in your page, or more commonly, in your GSP layout:<html> <head> <g:layoutTitle/> <r:layoutResources/> </head> <body> <g:layoutBody/> <r:layoutResources/> </body> </html>
使用r:layoutResources渲染资源链接
当在你的GSP页面中声明所需要的资源模块时,插件框架需要在正确的时间渲染那些资源的链接。要正确地处理,你必须在你的页面中引用两次r:layoutResources标签,或者更通用的方式是在你的GSP布局中处理:<html> <head> <g:layoutTitle/> <r:layoutResources/> </head> <body> <g:layoutBody/> <r:layoutResources/> </body> </html>
Adding page-specific JavaScript code with r:script
Grails has the javascript tag which is adapted to defer to Resources plugin if installed, but it is recommended that you callr:script
directly when you need to include fragments of JavaScript code.This lets you write some "inline" JavaScript which is actually not rendered inline, but either in the <head> or at the end of the body, based on the disposition.Given a Sitemesh layout like this:<html> <head> <g:layoutTitle/> <r:layoutResources/> </head> <body> <g:layoutBody/> <r:layoutResources/> </body> </html>
<html> <head> <title>Testing r:script magic!</title> </head> <body> <r:script disposition="head"> window.alert('This is at the end of <head>'); </r:script> <r:script disposition="defer"> window.alert('This is at the end of the body, and the page has loaded.'); </r:script> </body> </html>
使用r:script增加特定页面的JavaScript代码
在资源插件安装以后,Grails的javascript标签将被适配到优先使用资源插件,即便如此,如果你需要直接使用JavaScript代码片段,还是推荐你直接调用r:script
。这可以让你写一些“内联(inline)”的JavaScript,但实际 不在 内联时渲染,而是根据其安排,决定是在<head>或者body的结尾。假设一个Sitemesh布局如下所示:<html> <head> <g:layoutTitle/> <r:layoutResources/> </head> <body> <g:layoutBody/> <r:layoutResources/> </body> </html>
<html> <head> <title>Testing r:script magic!</title> </head> <body> <r:script disposition="head"> window.alert('This is at the end of <head>'); </r:script> <r:script disposition="defer"> window.alert('This is at the end of the body, and the page has loaded.'); </r:script> </body> </html>
Linking to images with r:img
This tag is used to render<img>
markup, using the Resources framework to process the resource on the fly (if configured to do so - e.g. make it eternally cacheable).This includes any extra attributes on the <img>
tag if the resource has been previously declared in a module.With this mechanism you can specify the width, height and any other attributes in the resource declaration in the module, and they will be pulled in as necessary.Example:<html> <head> <title>Testing r:img</title> </head> <body> <r:img uri="/images/logo.png"/> </body> </html>
g:img
tag as a shortcut for rendering <img>
tags that refer to a static resource. The Grails img tag is Resources-aware and will delegate to r:img
if found. However it is recommended that you use r:img
directly if using the Resources plugin.Alongside the regular Grails resource tag attributes, this also supports the "uri" attribute for increased brevity.See r:resource documentation for full details.
使用r:img链接图片
此标签被用以渲染HTML的<img>
标签,并且通过资源框架来处理那些频繁访问的资源(如果配置了的话,比如使其永久的缓存)。如果资源在以前已经被声明为一个模块的话,那么r:img会包含<img>
标签的任何额外属性。基于此机制,你可以在声明资源模块的时候,来指定width、height以及其他任何属性,然后在需要的时候获取一下即可。比如:<html> <head> <title>Testing r:img</title> </head> <body> <r:img uri="/images/logo.png"/> </body> </html>
g:img
标签只是渲染静态资源<img>
的一个快捷方式而已。Grails的img的标签如果感知到资源插件,那么将会将其代理给r:img
。即便如此,如果使用了资源插件的话,还是推荐直接你使用r:img
。跟常规的Grails的resource标签属性一样,为了增加简洁性,r:img也支撑"uri"属性。更多完整的详细信息请参考r:resource文档。
6.2.5.2 其他资源标签
r:resource
This is equivalent to the Grails resource tag, returning a link to the processed static resource. Grails' owng:resource
tag delegates to this implementation if found, but if your code requires the Resources plugin, you should use r:resource
directly.Alongside the regular Grails resource tag attributes, this also supports the "uri" attribute for increased brevity.See r:resource documentation for full details.
r:resource
这跟Grails的resource标签相当,都是返回一个处理过的静态资源链接。如果发现资源插件已经安装,Grails自带的g:resource
标签将代理给r:resource,但是如果你的代码依赖资源插件,最好还是直接使用r:resource
的好。跟常规的Grails的resource标签属性一样,为了增加简洁性,r:resource也支撑"uri"属性。更多完整的详细信息请参考r:resource文档。r:external
This is a resource-aware version of Grails external tag which renders the HTML markup necessary to include an external file resource such as CSS, JS or a favicon.See r:resource documentation for full details.r:external
这是一个资源感知(resource-aware)版本的Grails的external标签,用以渲染那些必要的HTML标签所需要的外部资源文件,比如CSS、JS或者favicon。更多完整的详细信息请参考r:resource文档。6.2.5.3 声明资源
A DSL is provided for declaring resources and modules. This can go either in your This defines three resource modules; 'core', 'utils' and 'forms'. The resources in these modules will be automatically bundled out of the box according to the module name, resulting in fewer files. You can override this with
系统提供了一个DSL专门用于声明资源和模块。其可以位于Config.groovy
in the case of application-specific resources, or more commonly in a resources artefact in grails-app/conf
.Note that you do not need to declare all your static resources, especially images. However you must to establish dependencies or other resources-specific attributes. Any resource that is not declared is called "ad-hoc" and will still be processed using defaults for that resource type.Consider this example resource configuration file, grails-app/conf/MyAppResources.groovy
:modules = { core { dependsOn 'jquery, utils' resource url: '/js/core.js', disposition: 'head' resource url: '/js/ui.js' resource url: '/css/main.css', resource url: '/css/branding.css' resource url: '/css/print.css', attrs: [media: 'print'] } utils { dependsOn 'jquery' resource url: '/js/utils.js' } forms { dependsOn 'core,utils' resource url: '/css/forms.css' resource url: '/js/forms.js' } }
bundle:'someOtherName'
on each resource, or call defaultBundle
on the module (see resources plugin documentation).It declares dependencies between them using dependsOn
, which controls the load order of the resources.When you include an <r:require module="forms"/>
in your GSP, it will pull in all the resources from 'core' and 'utils' as well as 'jquery', all in the correct order.You'll also notice the disposition:'head'
on the core.js
file. This tells Resources that while it can defer all the other JS files to the end of the body, this one must go into the <head>
.The CSS file for print styling adds custom attributes using the attrs
map option, and these are passed through to the r:external
tag when the engine renders the link to the resource, so you can customize the HTML attributes of the generated link.There is no limit to the number of modules or xxxResources.groovy artefacts you can provide, and plugins can supply them to expose modules to applications, which is exactly how the jQuery plugin works.To define modules like this in your application's Config.groovy, you simply assign the DSL closure to the grails.resources.modules
Config variable.For full details of the resource DSL please see the resources plugin documentation.
Config.groovy
中特定应用的资源容器,或者更常用的是grails-app/conf
下的一个资源工件中。注意!你并不需要声明所有的静态资源,尤其是图片。但是你必须要建立所需依赖或者其他资源相关的属性。没有被声明的资源称之为"ad-hoc",并且根据其资源类型被缺省处理。假设如下所示的grails-app/conf/MyAppResources.groovy
资源配置文件:modules = { core { dependsOn 'jquery, utils' resource url: '/js/core.js', disposition: 'head' resource url: '/js/ui.js' resource url: '/css/main.css', resource url: '/css/branding.css' resource url: '/css/print.css', attrs: [media: 'print'] } utils { dependsOn 'jquery' resource url: '/js/utils.js' } forms { dependsOn 'core,utils' resource url: '/css/forms.css' resource url: '/js/forms.js' } }
bundle:'someOtherName'
来覆盖之,或者调用模块的defaultBundle
(更多请参考资源插件文档)。通过dependsOn
来声明的依赖关系,可以控制资源的加载顺序。当你在的GSP中引用<r:require module="forms"/>
的时候,它将从'core'和'utils'还有'jquery'中获取所有的资源,并以正确的顺序加载。你将会注意到core.js
文件中的disposition:'head'
。它将告诉资源插件当所有的其他JS文件推迟到body末尾加载的时候,此文件(core.js)必须要在<head>
加载。用于打印风格的CSS文件通过attrs
映射选项来添加自定义属性,并且在渲染到资源链接的时候,它们将被传递给r:external
标签,因此你可以自定义HTML的属性来生成链接。模块或者你定义的xxxResources.groovy工件的数量是没有限制的,插件也可以将资源模块暴露给应用,正如jQuery插件所做的那样。要在你应用中的Config.groovy定义模块,你可以简单地将DSL闭包赋给grails.resources.modules
配置变量。完整的资源DSL信息请参考资源插件文档。
6.2.5.4 覆盖插件资源
Because a resource module can define the bundle groupings and other attributes of resources, you may find that the settings provided are not correct for your application.For example, you may wish to bundle jQuery and some other libraries all together in one file. There is a load-time and caching trade-off here, but often it is the case that you'd like to override some of these settings.To do this, the DSL supports an "overrides" clause, within which you can change the This will put all code into a single bundle named 'monolith'. Note that this can still result in multiple files, as separate bundles are required for head and defer dispositions, and JavaScript and CSS files are bundled separately.Note that overriding individual resources requires the original declaration to have included a unique id for the resource.For full details of the resource DSL please see the resources plugin documentation.
因为一个资源模块定义了捆绑(bundle)组和资源的其他属性,因此你可能会发现设置所提供的并不适合你的应用。比如,你可能希望将jQuery和其他的库捆绑到一个文件中。此处就要根据加载时间和缓存做一个权衡,但是在此种情况下,你经常会想重载这些配置的一部分。这时候,DSL提供了"overrides"子句来完成此功能,子句内你可以修改一个模块的defaultBundle
setting for a module, or attributes of individual resources that have been declared with a unique id:modules = { core { dependsOn 'jquery, utils' defaultBundle 'monolith' resource url: '/js/core.js', disposition: 'head' resource url: '/js/ui.js' resource url: '/css/main.css', resource url: '/css/branding.css' resource url: '/css/print.css', attrs: [media: 'print'] } utils { dependsOn 'jquery' defaultBundle 'monolith' resource url: '/js/utils.js' } forms { dependsOn 'core,utils' defaultBundle 'monolith' resource url: '/css/forms.css' resource url: '/js/forms.js' } overrides { jquery { defaultBundle 'monolith' } } }
defaultBundle
,或者每个单独资源的属性,不过每个资源必须要声明一个唯一的id:modules = { core { dependsOn 'jquery, utils' defaultBundle 'monolith' resource url: '/js/core.js', disposition: 'head' resource url: '/js/ui.js' resource url: '/css/main.css', resource url: '/css/branding.css' resource url: '/css/print.css', attrs: [media: 'print'] } utils { dependsOn 'jquery' defaultBundle 'monolith' resource url: '/js/utils.js' } forms { dependsOn 'core,utils' defaultBundle 'monolith' resource url: '/css/forms.css' resource url: '/js/forms.js' } overrides { jquery { defaultBundle 'monolith' } } }
6.2.5.5 优化资源
The Resources framework uses "mappers" to mutate the resources into the final format served to the user.The resource mappers are applied to each static resource once, in a specific order. You can create your own resource mappers, and several plugins provide some already for zipping, caching and minifying.Out of the box, the Resources plugin provides bundling of resources into fewer files, which is achieved with a few mappers that also perform CSS re-writing to handle when your CSS files are moved into a bundle.
资源框架使用"映射器(mappers)"来将资源转变为最终用户所需的格式。资源映射器以一个特定的顺序将每一个静态资源处理一次。你可以创建你自己的资源映射器,有一些插件已经提供了比如压缩(zipping)、缓存(caching)和缩少(minifying)等映射。除此之外,资源插件还提供了捆绑多个资源到较少的文件功能,在将你的CSS文件移动到一个捆绑束的时候,其使用一些映射器执行重写CSS处理。Bundling multiple resources into fewer files
The 'bundle' mapper operates by default on any resource with a "bundle" defined - or inherited from adefaultBundle
clause on the module. Modules have an implicit default bundle name the same as the name of the module.Files of the same kind will be aggregated into this bundle file. Bundles operate across module boundaries:modules = { core { dependsOn 'jquery, utils' defaultBundle 'common' resource url: '/js/core.js', disposition: 'head' resource url: '/js/ui.js', bundle: 'ui' resource url: '/css/main.css', bundle: 'theme' resource url: '/css/branding.css' resource url: '/css/print.css', attrs: [media: 'print'] } utils { dependsOn 'jquery' resource url: '/js/utils.js', bundle: 'common' } forms { dependsOn 'core,utils' resource url: '/css/forms.css', bundle: 'ui' resource url: '/js/forms.js', bundle: 'ui' } }
捆绑多个资源到较少的文件
缺省情况下,'bundle'映射器会操作使用"bundle"定义的任何资源-或者继承自模块的defaultBundle
子句。模块有一个隐含的跟模块名称相同的缺省捆绑束名。同样类型的文件将会被汇集到当前的捆绑束文件中。捆绑束是通过模块的边界来操作的:modules = { core { dependsOn 'jquery, utils' defaultBundle 'common' resource url: '/js/core.js', disposition: 'head' resource url: '/js/ui.js', bundle: 'ui' resource url: '/css/main.css', bundle: 'theme' resource url: '/css/branding.css' resource url: '/css/print.css', attrs: [media: 'print'] } utils { dependsOn 'jquery' resource url: '/js/utils.js', bundle: 'common' } forms { dependsOn 'core,utils' resource url: '/css/forms.css', bundle: 'ui' resource url: '/js/forms.js', bundle: 'ui' } }
Making resources cache "eternally" in the client browser
Caching resources "eternally" in the client is only viable if the resource has a unique name that changes whenever the contents change, and requires caching headers to be set on the response.The cached-resources plugin provides a mapper that achieves this by hashing your files and renaming them based on this hash. It also sets the caching headers on every response for those resources. To use, simply install the cached-resources plugin.Note that the caching headers can only be set if your resources are being served by your application. If you have another server serving the static content from your app (e.g. Apache HTTPD), configure it to send caching headers. Alternatively you can configure it to request and proxy the resources from your container.让资源“永久”地缓存在客户浏览器
在客户端“永久”地缓存资源只有在资源有一个唯一的名字的情况下,才切实可行,并且当资源的内容变化时,其名字也要做相应的变化,还要求在响应中设置缓存标头(caching headers)。cached-resources插件提供了一个映射器来完成此功能,它是通过对你的文件做哈稀校验并根据校验值来重命名来实现的。此插件也会在每一次的响应中根据这些资源的情况来设置缓存标头。要使用它,只需要简单的安装cached-resources插件即可。注意!只有在你的应用管辖范围内的资源,才会有可能设置缓存标头.如果你有另外一个服务器专门管理你应用的静态资源(比如Apache的HTTPD),那么需要配置此服务器来发送缓存标头。或者你也可以配置它来请求和代理你容器内的资源。Zipping resources
Returning gzipped resources is another way to reduce page load times and reduce bandwidth.The zipped-resources plugin provides a mapper that automatically compresses your content, excluding by default already compressed formats such as gif, jpeg and png.Simply install the zipped-resources plugin and it works.压缩资源
返回用gzip压缩过的资源是另外减少页面加载时间和带宽的方法。zipped-resources插件提供了一个映射器来自动地压缩你的资源内容。当然那些已经压缩过地除外,比如gif、jpeg和png。简单地安装zipped-resources插件后,即可工作。Minifying
There are a number of CSS and JavaScript minifiers available to obfuscate and reduce the size of your code. At the time of writing none are publicly released but releases are imminent.缩少资源
已经有很多的CSS和JavaScript缩少器可以用来混淆和减少你代码的大小。这可以解决发布迫在眉睫,而现在编码时没有什么可公开发布的情况。6.2.5.6 调试
When your resources are being moved around, renamed and otherwise mutated, it can be hard to debug client-side issues. Modern browsers, especially Safari, Chrome and Firefox have excellent tools that let you view all the resources requested by a page, including the headers and other information about them.There are several debugging features built in to the Resources framework.
当你的资源正在移动、重命名以及其他变动的时候,要调试客户端的问题是非常困难的。现代的浏览器,尤其是Safari、Chrome和Firefox,都有非常优秀的工具来查看一个请求页面的所有资源,包括其请求头和其他的信息。除此之外,Resources框架还提供了几个内置的调试特性。X-Grails-Resources-Original-Src Header
Every resource served in development mode will have the X-Grails-Resources-Original-Src: header added, indicating the original source file(s) that make up the response.X-Grails-Resources-Original-Src信息头
在开发模式下,每一个用到的资源都会添加X-Grails-Resources-Original-Src的头信息,用以表示此响应对应的原始代码文件。Adding the debug flag
If you add a query parameter _debugResources=y to your URL and request the page, Resources will bypass any processing so that you can see your original source files.This also adds a unique timestamp to all your resource URLs, to defeat any caching that browsers may use. This means that you should always see your very latest code when you reload the page.添加调试标记
如果在你的URL和请求页面的参数中增加 _debugResources=y 的话,Resources将会不管任何处理,而直接显示和使用原始的代码文件。此外,你资源的URLs还会添加一个唯一的时间戳,用以处理浏览器导致的缓存问题。这意味着在你重现加载页面的时候,你总是得到最新的代码。Turning on debug all the time
You can turn on the aforementioned debug mechanism without requiring a query parameter, but turning it on in Config.groovy:grails.resources.debug = true
打开调试
你可以在不需要额外请求参数的情况下打开如上所述的调试机制,要如此,只要在Config.groovy中配置一下即可:grails.resources.debug = true
6.2.5.7 阻止资源处理
Sometimes you do not want a resource to be processed in a particular way, or even at all. Occasionally you may also want to disable all resource mapping.
有时候,你并不想以一种特别的方式处理资源,甚至根本就不想。偶尔,你还想禁止所有的资源映射。Preventing the application of a specific mapper to an individual resource
All resource declarations support a convention of noXXXX:true where XXXX is a mapper name.So for example to prevent the "hashandcache" mapper from being applied to a resource (which renames and moves it, potentially breaking relative links written in JavaScript code), you would do this:modules = { forms { resource url: '/css/forms.css', nohashandcache: true resource url: '/js/forms.js', nohashandcache: true } }
阻止到一个单独资源的特定映射
所有的资源声明都支持noXXXX:true的用法,此处的XXXX是一个映射器的名称。因此在下例中,要阻止"hashandcache"映射器应用到一个资源(重命名,移动甚至断开JavaScript代码中的相关链接)你可以这样做:modules = { forms { resource url: '/css/forms.css', nohashandcache: true resource url: '/js/forms.js', nohashandcache: true } }
Excluding/including paths and file types from specific mappers
Mappers have includes/excludes Ant patterns to control whether they apply to a given resource. Mappers set sensible defaults for these based on their activity, for example the zipped-resources plugin's "zip" mapper is set to exclude images by default.You can configure this in yourConfig.groovy
using the mapper name e.g:// We wouldn't link to .exe files using Resources but for the sake of example: grails.resources.zip.excludes = ['**/*.zip', '**/*.exe']// Perhaps for some reason we want to prevent bundling on "less" CSS files: grails.resources.bundle.excludes = ['**/*.less']
从特定映射器中 排除/包含 路径和文件类型
映射器的排除/包含使用Ant语法来控制是否要应用到给定的资源上。映射器会根据其活动情况来设置缺省的感知类型,以资源压缩(zipped-resources)插件为例,其"zip"映射器会缺省地排除那些镜像文件。你可以通过你的Config.groovy
文件地映射器名称来配置相关信息,比如:// We wouldn't link to .exe files using Resources but for the sake of example: grails.resources.zip.excludes = ['**/*.zip', '**/*.exe']// Perhaps for some reason we want to prevent bundling on "less" CSS files: grails.resources.bundle.excludes = ['**/*.less']
Controlling what is treated as an "ad-hoc" (legacy) resource
Ad-hoc resources are those undeclared, but linked to directly in your application without using the Grails or Resources linking tags (resource, img or external).These may occur with some legacy plugins or code with hardcoded paths in.There is a Config.groovy setting grails.resources.adhoc.patterns which defines a list of Servlet API compliant filter URI mappings, which the Resources filter will use to detect such "ad-hoc resource" requests.By default this is set to:grails.resources.adhoc.patterns = ['images/*', '*.js', '*.css']
控制"ad-hoc"(遗留)资源
Ad-hoc资源是那些未声明的,并且 不 使用Grails或者Resources的链接标签(resource, img or external),而是在你的应用中直接链接的资源。这可能会在那些遗留插件或者硬编码路径的时候会碰到。Config.groovy中的 grails.resources.adhoc.patterns 配置就是用来定义一系列Servlet API兼容的URI映射的过滤器,其资源过滤器通常用来检测那些"ad-hoc resource"请求。其缺省值如下:grails.resources.adhoc.patterns = ['images/*', '*.js', '*.css']
6.2.5.8 其他资源感知的插件
At the time of writing, the following plugins include support for the Resources framework:
截至到书写为止,资源框架已经被下列插件所支撑:
6.2.6 Sitemesh的内容块
Although it is useful to decorate an entire page sometimes you may find the need to decorate independent sections of your site. To do this you can use content blocks. To get started, partition the page to be decorated using the Then within the layout you can reference these components and apply individual layouts to each:
虽然装饰整个页面是有用的,但有时候你可能只需要装饰站点单独的部分。这时,你可以使用内容块来完成。首先,对于要装饰的页面部分使用<content>
tag:<content tag="navbar"> … draw the navbar here… </content><content tag="header"> … draw the header here… </content><content tag="footer"> … draw the footer here… </content><content tag="body"> … draw the body here… </content>
<html> <body> <div id="header"> <g:applyLayout name="headerLayout"> <g:pageProperty name="page.header" /> </g:applyLayout> </div> <div id="nav"> <g:applyLayout name="navLayout"> <g:pageProperty name="page.navbar" /> </g:applyLayout> </div> <div id="body"> <g:applyLayout name="bodyLayout"> <g:pageProperty name="page.body" /> </g:applyLayout> </div> <div id="footer"> <g:applyLayout name="footerLayout"> <g:pageProperty name="page.footer" /> </g:applyLayout> </div> </body> </html>
<content>
标签来处理:<content tag="navbar"> … draw the navbar here… </content><content tag="header"> … draw the header here… </content><content tag="footer"> … draw the footer here… </content><content tag="body"> … draw the body here… </content>
<html> <body> <div id="header"> <g:applyLayout name="headerLayout"> <g:pageProperty name="page.header" /> </g:applyLayout> </div> <div id="nav"> <g:applyLayout name="navLayout"> <g:pageProperty name="page.navbar" /> </g:applyLayout> </div> <div id="body"> <g:applyLayout name="bodyLayout"> <g:pageProperty name="page.body" /> </g:applyLayout> </div> <div id="footer"> <g:applyLayout name="footerLayout"> <g:pageProperty name="page.footer" /> </g:applyLayout> </div> </body> </html>
6.2.7 修改已经部署的应用
One of the main issues with deploying a Grails application (or typically any servlet-based one) is that any change to the views requires that you redeploy your whole application. If all you want to do is fix a typo on a page, or change an image link, it can seem like a lot of unnecessary work. For such simple requirements, Grails does have a solution: the
The first line tells Grails that modified GSP files should be reloaded at runtime. If you don't have this setting, you can make as many changes as you like but they won't be reflected in the running application until you restart. The second line tells Grails where to load the views and layouts from.
The key point here is that you must retain the view directory structure, including the
GSP reloading is supported for precompiled GSPs since Grails 1.3.5 .
部署一个Grails应用(或者任意基于servlet的应用)的一个主要问题是视图的任何修改都需要重新再部署你的整个应用。假如你只是想修复一个页面的打字错误或者修改一个图像链接,那么这种再部署像是比较多余的工作。对于这种比较简单的需求,Grails提供了一个解决方案:配置grails.gsp.view.dir
configuration setting.How does this work? The first step is to decide where the GSP files should go. Let's say we want to keep them unpacked in a /var/www/grails/my-app
directory. We add these two lines to grails-app/conf/Config.groovy
:
grails.gsp.enable.reload = true grails.gsp.view.dir = "/var/www/grails/my-app/"
The trailing slash on the grails.gsp.view.dir
value is important! Without it, Grails will look for views in the parent directory.
Setting "grails.gsp.view.dir" is optional. If it's not specified, you can update files directly to the application server's deployed war directory. Depending on the application server, these files might get overwritten when the server is restarted. Most application servers support "exploded war deployment" which is recommended in this case.With those settings in place, all you need to do is copy the views from your web application to the external directory. On a Unix-like system, this would look something like this:
mkdir -p /var/www/grails/my-app/grails-app/views cp -R grails-app/views/* /var/www/grails/my-app/grails-app/views
grails-app/views
bit. So you end up with the path /var/www/grails/my-app/grails-app/views/...
.One thing to bear in mind with this technique is that every time you modify a GSP, it uses up permgen space. So at some point you will eventually hit "out of permgen space" errors unless you restart the server. So this technique is not recommended for frequent or large changes to the views.There are also some System properties to control GSP reloading:
Name | Description | Default |
---|---|---|
grails.gsp.enable.reload | altervative system property for enabling the GSP reload mode without changing Config.groovy | |
grails.gsp.reload.interval | interval between checking the lastmodified time of the gsp source file, unit is milliseconds | 5000 |
grails.gsp.reload.granularity | the number of milliseconds leeway to give before deciding a file is out of date. this is needed because different roundings usually cause a 1000ms difference in lastmodified times | 1000 |
grails.gsp.view.dir
属性。那么它是如何工作的呢?第一步就是要确定GSP文件位于什么地方。假设我们想让这些文件解压缩到/var/www/grails/my-app
目录,那么我们需要在grails-app/conf/Config.groovy
增加如下两行:
grails.gsp.enable.reload = true grails.gsp.view.dir = "/var/www/grails/my-app/"
grails.gsp.view.dir
值的最后一个反斜杠是很重要的!没有它,Grails将会在其上一级目录寻找视图。
"grails.gsp.view.dir"的值是可选的。如果没有设置,你可以直接更新部署在应用服务器的war目录下的文件。这些文件可能会在应用服务器重新启动的时候被覆盖,不过这是跟服务器相关的。在这个时候,大部分的应用服务器所支持的“war额外加载部署(exploded war deployment)”模式是值得推荐的。所有这些设置完毕以后,你所需要做的就是从你的web应用中拷贝视图文件到外部的目录中。在一个Unix类的系统中,这可能看起来如下所示:
mkdir -p /var/www/grails/my-app/grails-app/views cp -R grails-app/views/* /var/www/grails/my-app/grails-app/views
grails-app/views
本身。因此你的路径是/var/www/grails/my-app/grails-app/views/...
的形式。使用此技术,要牢记的一件事情是在你每一次修改GSP文件的时候,会增加permgen的内存空间。因此最终你将会碰到"permgen内存空间益"的错误,当然你可以通过重新启动服务器来解决。所以,此技术不推荐应用于视图被频繁或者大量修改的情况。此外还有一些系统级的属性配置来控制GSP的重新加载:
名称 | 描述 | 缺省值 |
---|---|---|
grails.gsp.enable.reload | 在不修改Config.groovy的情况下,通过系统设置变量来启动GSP重栽模式 | |
grails.gsp.reload.interval | 轮询gsp源文件最后修改时间的时间间隔,单位是毫秒 | 5000 |
grails.gsp.reload.granularity | 在一个文件超时以前预留的毫秒数,此项是需要的,因为最后修改时间精度会导致1000毫秒的误差 | 1000 |
6.2.8 GSP调试
Viewing the generated source code
- Adding "?showSource=true" or "&showSource=true" to the url shows the generated Groovy source code for the view instead of rendering it. It won't show the source code of included templates. This only works in development mode
- The saving of all generated source code can be activated by setting the property "grails.views.gsp.keepgenerateddir" (in Config.groovy) . It must point to a directory that exists and is writable.
- During "grails war" gsp pre-compilation, the generated source code is stored in grails.project.work.dir/gspcompile (usually in ~/.grails/(grails_version)/projects/(project name)/gspcompile).
Debugging GSP code with a debugger
Viewing information about templates used to render a single url
GSP templates are reused in large web applications by using theg:render
taglib. Several small templates can be used to render a single page.
It might be hard to find out what GSP template actually renders the html seen in the result.
The debug templates -feature adds html comments to the output. The comments contain debug information about gsp templates used to render the page.Usage is simple: append "?debugTemplates" or "&debugTemplates" to the url and view the source of the result in your browser.
"debugTemplates" is restricted to development mode. It won't work in production.Here is an example of comments added by debugTemplates :
<!-- GSP #2 START template: /home/.../views/_carousel.gsp
precompiled: false lastmodified: … -->
.
.
.
<!-- GSP #2 END template: /home/.../views/_carousel.gsp
rendering time: 115 ms -->
查看生成的源代码
- 在url中增加"?showSource=true"或者"&showSource=true"来显示生成的用于查看的Groovy源代码。它将不会显示包含模板的源代码,并且只工作于开发模式。
- 要保存所有生成的源代码,可以通过配置"grails.views.gsp.keepgenerateddir"(在Config.groovy中)来完成。指向的目录必须存在而且可写。
- 在"grails war"的gsp预编译阶段,其生成的源代码被保存在grails.project.work.dir/gspcompile中(通常位于~/.grails/(grails_version)/projects/(project name)/gspcompile中)。
在调试器中调试GSP代码
- 详细请参考 在STS中调试GSP
查看渲染成一个url的模板信息
在大型的WEB应用中,GSP的模板可以通过使用g:render
标签而得以复用。几个小模板可以被渲染到一个单独的页面中。
在最后渲染的html中,很难区分出那些是那个GSP模板被实际渲染到那里。
调试模板功能将会在输出中添加html注释。这些注释包含着关于gsp模板渲染的调试信息。用法也很简单:添加"?debugTemplates"或者"&debugTemplates"到url中,然后查看你浏览器中的源代码。
"debugTemplates"仅限于开发模式,在生产环境中将无效。下面是增加了debugTemplates后的一个带有注释的示例:
<!-- GSP #2 START template: /home/.../views/_carousel.gsp
precompiled: false lastmodified: … -->
.
.
.
<!-- GSP #2 END template: /home/.../views/_carousel.gsp
rendering time: 115 ms -->
6.3 标签库
Like Java Server Pages (JSP), GSP supports the concept of custom tag libraries. Unlike JSP, Grails' tag library mechanism is simple, elegant and completely reloadable at runtime.Quite simply, to create a tag library create a Groovy class that ends with the convention Now to create a tag create a Closure property that takes two arguments: the tag attributes and the body content:The As demonstrated above there is an implicit
跟Java Server Pages (JSP)类似,GSP支持自定义标签库的概念。而跟JSP不同的是,Grails的标签库机制是简单而优雅的,并且完全可以在运行时重新加载。要创建一个标签库是很简单的,只需要根据规约创建一个以TagLib
and place it within the grails-app/taglib
directory:class SimpleTagLib {}
class SimpleTagLib { def simple = { attrs, body -> } }
attrs
argument is a Map of the attributes of the tag, whilst the body
argument is a Closure that returns the body content when invoked:class SimpleTagLib { def emoticon = { attrs, body -> out << body() << (attrs.happy == 'true' ? " :-)" : " :-(") } }
out
variable that refers to the output Writer
which you can use to append content to the response. Then you can reference the tag inside your GSP; no imports are necessary:<g:emoticon happy="true">Hi John</g:emoticon>
To help IDEs like SpringSource Tool Suite (STS) and others autocomplete tag attributes, you should add Javadoc comments to your tag closures with@attr
descriptions. Since taglibs use Groovy code it can be difficult to reliably detect all usable attributes.For example:and any mandatory attributes should include the REQUIRED keyword, e.g.class SimpleTagLib { /** * Renders the body with an emoticon. * * @attr happy whether to show a happy emoticon ('true') or * a sad emoticon ('false') */ def emoticon = { attrs, body -> out << body() << (attrs.happy == 'true' ? " :-)" : " :-(") } }class SimpleTagLib { /** * Creates a new password field. * * @attr name REQUIRED the field name * @attr value the field value */ def passwordField = { attrs -> attrs.type = "password" attrs.tagName = "passwordField" fieldImpl(out, attrs) } }
TagLib
结尾的Groovy类,并且放到grails-app/taglib
下边就好了:class SimpleTagLib {}
class SimpleTagLib { def simple = { attrs, body -> } }
attrs
参数是此标签的属性,类型为映射(Map),而body
参数是一个闭包,它在被调用的时候将返回一个主体内容:class SimpleTagLib { def emoticon = { attrs, body -> out << body() << (attrs.happy == 'true' ? " :-)" : " :-(") } }
out
变量将引用Writer
输出器,用以往响应中追加内容。因此,你可以在不导入任何东西的情况下,于你的GSP内使用标签:<g:emoticon happy="true">Hi John</g:emoticon>
为了有助于像SpringSource Tool Suite (STS)这样的IDE来自动补齐标签属性,你应该在Javadoc注释中增加标签闭包的@attr
描述。因为标签库也是Groovy代码,因此不能保证检测到的所有属性都是准确可靠的。比如:并且,任何必须的属性都应该包含REQUIRED关键字,比如:class SimpleTagLib { /** * Renders the body with an emoticon. * * @attr happy whether to show a happy emoticon ('true') or * a sad emoticon ('false') */ def emoticon = { attrs, body -> out << body() << (attrs.happy == 'true' ? " :-)" : " :-(") } }class SimpleTagLib { /** * Creates a new password field. * * @attr name REQUIRED the field name * @attr value the field value */ def passwordField = { attrs -> attrs.type = "password" attrs.tagName = "passwordField" fieldImpl(out, attrs) } }
6.3.1 变量和作用域
Within the scope of a tag library there are a number of pre-defined variables including:
在一个标签库的作用域内,已经预定义了一些变量,它们包括:
actionName
- The currently executing action namecontrollerName
- The currently executing controller nameflash
- The flash objectgrailsApplication
- The GrailsApplication instanceout
- The response writer for writing to the output streampageScope
- A reference to the pageScope object used for GSP rendering (i.e. the binding)params
- The params object for retrieving request parameterspluginContextPath
- The context path to the plugin that contains the tag libraryrequest
- The HttpServletRequest instanceresponse
- The HttpServletResponse instanceservletContext
- The javax.servlet.ServletContext instancesession
- The HttpSession instance
actionName
- 当前正在运行的操作名称controllerName
- 当前正在运行的控制器名称flash
- flash对象grailsApplication
- GrailsApplication实例out
- 响应输出器,用于将内容写到输出流中pageScope
- 一个pageScope对象引用,用于GSP的渲染(比如绑定)params
- 用于接受请求参数的params对象pluginContextPath
- 包含标签库的插件上下文路径request
- HttpServletRequest实例response
- HttpServletResponse实例servletContext
- javax.servlet.ServletContext实例session
- HttpSession实例
6.3.2 简单标签
As demonstrated it the previous example it is easy to write simple tags that have no body and just output content. Another example is a The above uses Java's With simple tags sometimes you need to write HTML mark-up to the response. One approach would be to embed the content directly:Although this approach may be tempting it is not very clean. A better approach would be to reuse the render tag:And then have a separate GSP template that does the actual rendering.
正如以前示例所演示的那样,要写一个只输出内容而没有主体(body)的标签是很容易的。另外的一个示例是dateFormat
style tag:def dateFormat = { attrs, body ->
out << new java.text.SimpleDateFormat(attrs.format).format(attrs.date)
}
SimpleDateFormat
class to format a date and then write it to the response. The tag can then be used within a GSP as follows:<g:dateFormat format="dd-MM-yyyy" date="${new Date()}" />
def formatBook = { attrs, body -> out << "<div id="${attrs.book.id}">" out << "Title : ${attrs.book.title}" out << "</div>" }
def formatBook = { attrs, body ->
out << render(template: "bookTemplate", model: [book: attrs.book])
}
dateFormat
风格的标签:def dateFormat = { attrs, body ->
out << new java.text.SimpleDateFormat(attrs.format).format(attrs.date)
}
SimpleDateFormat
类来格式化一个日期,并且将它写回到响应中。然后标签就可以在GSP中像下面所示那样使用:<g:dateFormat format="dd-MM-yyyy" date="${new Date()}" />
def formatBook = { attrs, body -> out << "<div id="${attrs.book.id}">" out << "Title : ${attrs.book.title}" out << "</div>" }
def formatBook = { attrs, body ->
out << render(template: "bookTemplate", model: [book: attrs.book])
}
6.3.3 逻辑标签
You can also create logical tags where the body of the tag is only output once a set of conditions have been met. An example of this may be a set of security tags:The tag above checks if the user is an administrator and only outputs the body content if he/she has the correct set of access privileges:
你也可以创建一个逻辑标签,一旦一组条件表达式满足,就输出标签的主体。一组安全标签的示例如下:def isAdmin = { attrs, body ->
def user = attrs.user
if (user && checkUserPrivs(user)) {
out << body()
}
}
<g:isAdmin user="${myUser}"> // some restricted content </g:isAdmin>
def isAdmin = { attrs, body ->
def user = attrs.user
if (user && checkUserPrivs(user)) {
out << body()
}
}
<g:isAdmin user="${myUser}"> // some restricted content </g:isAdmin>
6.3.4 迭代标签
Iterative tags are easy too, since you can invoke the body multiple times:In this example we check for a Notice how in this example we use the implicit That value is then passed as the default variable Here we check if there is a Notice how we use the
因为你可以多次调用主体(body),所以迭代标签也是很容易的:def repeat = { attrs, body -> attrs.times?.toInteger()?.times { num -> out << body(num) } }
times
attribute and if it exists convert it to a number, then use Groovy's times
method to iterate the specified number of times:<g:repeat times="3"> <p>Repeat this 3 times! Current repeat = ${it}</p> </g:repeat>
it
variable to refer to the current number. This works because when we invoked the body we passed in the current value inside the iteration:out << body(num)
it
to the tag. However, if you have nested tags this can lead to conflicts, so you should should instead name the variables that the body uses:def repeat = { attrs, body -> def var = attrs.var ?: "num" attrs.times?.toInteger()?.times { num -> out << body((var):num) } }
var
attribute and if there is use that as the name to pass into the body invocation on this line:out << body((var):num)
Note the usage of the parenthesis around the variable name. If you omit these Groovy assumes you are using a String key and not referring to the variable itself.Now we can change the usage of the tag as follows:
<g:repeat times="3" var="j"> <p>Repeat this 3 times! Current repeat = ${j}</p> </g:repeat>
var
attribute to define the name of the variable j
and then we are able to reference that variable within the body of the tag.
def repeat = { attrs, body -> attrs.times?.toInteger()?.times { num -> out << body(num) } }
times
属性,如果存在呢,就将其转换为一个数字,然后使用Groovy的times
方法来迭代给定的次数:<g:repeat times="3"> <p>Repeat this 3 times! Current repeat = ${it}</p> </g:repeat>
it
变量来引用当前的数字。此种方式是有效的,因为在迭代内部,我们将当前值传给了正在调用的主体(body):out << body(num)
it
变量传给了标签。但是,如果你有嵌套的标签的话,那么这将会导致冲突,因此你应该给给调用的主体变量命名:def repeat = { attrs, body -> def var = attrs.var ?: "num" attrs.times?.toInteger()?.times { num -> out << body((var):num) } }
var
属性,如果有,那么将使用其值作为变量名称传递给正在调用的主体,如下所示:out << body((var):num)
注意!变量名称两边的括号。如果你忽略它们,那么Groovy将会认为你正在使用一个String类型的键,而不是变量本身。现在你可以修改标签的使用方法了,如下所示:
<g:repeat times="3" var="j"> <p>Repeat this 3 times! Current repeat = ${j}</p> </g:repeat>
var
属性来将变量名称定义为j
,然后就可以在标签的主体内来引用此变量了。 tag.
6.3.5 标签命名空间
By default, tags are added to the default Grails namespace and are used with the Here we have specified a where the prefix is the same as the value of the static This works from GSP, controllers or tag libraries
一般情况下,标签使用Grails的缺省命名空间,并且在GSP页面中使用g:
prefix in GSP pages. However, you can specify a different namespace by adding a static property to your TagLib
class:class SimpleTagLib { static namespace = "my" def example = { attrs -> … } }
namespace
of my
and hence the tags in this tag lib must then be referenced from GSP pages like this:<my:example name="..." />
namespace
property. Namespaces are particularly useful for plugins.Tags within namespaces can be invoked as methods using the namespace as a prefix to the method call:out << my.example(name:"foo")
g:
前缀。但是你也可以通过在TagLib
类中增加一个静态属性来指定另外一个命名空间:class SimpleTagLib { static namespace = "my" def example = { attrs -> … } }
namespace
指定为my
,因此此标签库的标签在GSP页面中必须像如下所示那样引用:<my:example name="..." />
namespace
的值是一样的。命名空间对插件来说特别有用。带命名空间的标签也可以以方法的方式调用,需要将其命名空间作为前缀赋给方法调用:out << my.example(name:"foo")
6.3.6 使用JSP标签库
In addition to the simplified tag library mechanism provided by GSP, you can also use JSP tags from GSP. To do so simply declare the JSP to use with thetaglib
directive:<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<fmt:formatNumber value="${10}" pattern=".00"/>
${fmt.formatNumber(value:10, pattern:".00")}
6.3.7 标签的返回值
Since Grails 1.2, a tag library call returns an instance oforg.codehaus.groovy.grails.web.util.StreamCharBuffer
class by default.
This change improves performance by reducing object creation and optimizing buffering during request processing.
In earlier Grails versions, a java.lang.String
instance was returned.Tag libraries can also return direct object values to the caller since Grails 1.2..
Object returning tag names are listed in a static returnObjectForTags
property in the tag library class.Example:
class ObjectReturningTagLib { static namespace = "cms" static returnObjectForTags = ['content'] def content = { attrs, body -> CmsContent.findByCode(attrs.code)?.content } }
6.4 URL映射
Throughout the documentation so far the convention used for URLs has been the default of/controller/action/id
. However, this convention is not hard wired into Grails and is in fact controlled by a URL Mappings class located at grails-app/conf/UrlMappings.groovy
.The UrlMappings
class contains a single property called mappings
that has been assigned a block of code:class UrlMappings {
static mappings = {
}
}
6.4.1 映射到控制器和操作
To create a simple mapping simply use a relative URL as the method name and specify named parameters for the controller and action to map to:"/product"(controller: "product", action: "list")
/product
to the list
action of the ProductController
. Omit the action definition to map to the default action of the controller:"/product"(controller: "product")
"/product" { controller = "product" action = "list" }
"/hello"(uri: "/hello.dispatch")
6.4.2 嵌入式变量
Simple Variables
The previous section demonstrated how to map simple URLs with concrete "tokens". In URL mapping speak tokens are the sequence of characters between each slash, '/'. A concrete token is one which is well defined such as as/product
. However, in many circumstances you don't know what the value of a particular token will be until runtime. In this case you can use variable placeholders within the URL for example:static mappings = { "/product/$id"(controller: "product") }
id
. For example given the URL /product/MacBook
, the following code will render "MacBook" to the response:class ProductController { def index() { render params.id } }
static mappings = { "/$blog/$year/$month/$day/$id"(controller: "blog", action: "show") }
/graemerocher/2007/01/10/my_funky_blog_entry
year
, month
, day
, id
and so on.Dynamic Controller and Action Names
Variables can also be used to dynamically construct the controller and action name. In fact the default Grails URL mappings use this technique:static mappings = { "/$controller/$action?/$id?"() }
controller
, action
and id
embedded within the URL.You can also resolve the controller name and action name to execute dynamically using a closure:static mappings = { "/$controller" { action = { params.goHere } } }
Optional Variables
Another characteristic of the default mapping is the ability to append a ? at the end of a variable to make it an optional token. In a further example this technique could be applied to the blog URL mapping to have more flexible linking:static mappings = { "/$blog/$year?/$month?/$day?/$id?"(controller:"blog", action:"show") }
/graemerocher/2007/01/10/my_funky_blog_entry
/graemerocher/2007/01/10
/graemerocher/2007/01
/graemerocher/2007
/graemerocher
Arbitrary Variables
You can also pass arbitrary parameters from the URL mapping into the controller by just setting them in the block passed to the mapping:"/holiday/win" { id = "Marrakech" year = 2007 }
Dynamically Resolved Variables
The hard coded arbitrary variables are useful, but sometimes you need to calculate the name of the variable based on runtime factors. This is also possible by assigning a block to the variable name:"/holiday/win" { id = { params.id } isEligible = { session.user != null } // must be logged in }
6.4.3 映射到视图
You can resolve a URL to a view without a controller or action involved. For example to map the root URL/
to a GSP at the location grails-app/views/index.gsp
you could use:static mappings = { "/"(view: "/index") // map the root URL }
static mappings = { "/help"(controller: "site", view: "help") // to a view for a controller }
6.4.4 映射到响应代码
Grails also lets you map HTTP response codes to controllers, actions or views. Just use a method name that matches the response code you are interested in:static mappings = { "403"(controller: "errors", action: "forbidden") "404"(controller: "errors", action: "notFound") "500"(controller: "errors", action: "serverError") }
static mappings = { "403"(view: "/errors/forbidden") "404"(view: "/errors/notFound") "500"(view: "/errors/serverError") }
Declarative Error Handling
In addition you can configure handlers for individual exceptions:static mappings = { "403"(view: "/errors/forbidden") "404"(view: "/errors/notFound") "500"(controller: "errors", action: "illegalArgument", exception: IllegalArgumentException) "500"(controller: "errors", action: "nullPointer", exception: NullPointerException) "500"(controller: "errors", action: "customException", exception: MyException) "500"(view: "/errors/serverError") }
IllegalArgumentException
will be handled by the illegalArgument
action in ErrorsController
, a NullPointerException
will be handled by the nullPointer
action, and a MyException
will be handled by the customException
action. Other exceptions will be handled by the catch-all rule and use the /errors/serverError
view.You can access the exception from your custom error handing view or controller action using the request's exception
attribute like so:class ErrorController { def handleError() { def exception = request.exception // perform desired processing to handle the exception } }
If your error-handling controller action throws an exception as well, you'll end up with a StackOverflowException
.
6.4.5 映射到HTTP方法
URL mappings can also be configured to map based on the HTTP method (GET, POST, PUT or DELETE). This is very useful for RESTful APIs and for restricting mappings based on HTTP method.As an example the following mappings provide a RESTful API URL mappings for theProductController
:static mappings = { "/product/$id"(controller:"product") { action = [GET:"show", PUT:"update", DELETE:"delete", POST:"save"] } }
6.4.6 映射到通配符
Grails' URL mappings mechanism also supports wildcard mappings. For example consider the following mapping:static mappings = { "/images/*.jpg"(controller: "image") }
/image/logo.jpg
. Of course you can achieve the same effect with a variable:static mappings = { "/images/$name.jpg"(controller: "image") }
static mappings = { "/images/**.jpg"(controller: "image") }
/image/logo.jpg
as well as /image/other/logo.jpg
. Even better you can use a double wildcard variable:static mappings = { // will match /image/logo.jpg and /image/other/logo.jpg "/images/$name**.jpg"(controller: "image") }
name
parameter obtainable from the params object:def name = params.name println name // prints "logo" or "other/logo"
excludes
setting inside the UrlMappings.groovy
class:class UrlMappings { static excludes = ["/images/*", "/css/*"] static mappings = { … } }
/images
or /css
.
6.4.7 自动重写链接
Another great feature of URL mappings is that they automatically customize the behaviour of the link tag so that changing the mappings don't require you to go and change all of your links.This is done through a URL re-writing technique that reverse engineers the links from the URL mappings. So given a mapping such as the blog one from an earlier section:static mappings = { "/$blog/$year?/$month?/$day?/$id?"(controller:"blog", action:"show") }
<g:link controller="blog" action="show" params="[blog:'fred', year:2007]"> My Blog </g:link><g:link controller="blog" action="show" params="[blog:'fred', year:2007, month:10]"> My Blog - October 2007 Posts </g:link>
<a href="/fred/2007">My Blog</a> <a href="/fred/2007/10">My Blog - October 2007 Posts</a>
6.4.8 应用约束
URL Mappings also support Grails' unified validation constraints mechanism, which lets you further "constrain" how a URL is matched. For example, if we revisit the blog sample code from earlier, the mapping currently looks like this:static mappings = { "/$blog/$year?/$month?/$day?/$id?"(controller:"blog", action:"show") }
/graemerocher/2007/01/10/my_funky_blog_entry
/graemerocher/not_a_year/not_a_month/not_a_day/my_funky_blog_entry
"/$blog/$year?/$month?/$day?/$id?" { controller = "blog" action = "show" constraints { year(matches:/\d{4}/) month(matches:/\d{2}/) day(matches:/\d{2}/) } }
year
, month
and day
parameters match a particular valid pattern thus relieving you of that burden later on.
6.4.9 命名URL映射
URL Mappings also support named mappings, that is mappings which have a name associated with them. The name may be used to refer to a specific mapping when links are generated.The syntax for defining a named mapping is as follows:static mappings = {
name <mapping name>: <url pattern> {
// …
}
}
static mappings = { name personList: "/showPeople" { controller = 'person' action = 'list' } name accountDetails: "/details/$acctNumber" { controller = 'product' action = 'accountDetails' } }
<g:link mapping="personList">List People</g:link>
<a href="/showPeople">List People</a>
<g:link mapping="accountDetails" params="[acctNumber:'8675309']"> Show Account </g:link>
<a href="/details/8675309">Show Account</a>
<link:personList>List People</link:personList>
<a href="/showPeople">List People</a>
<link:accountDetails acctNumber="8675309">Show Account</link:accountDetails>
<a href="/details/8675309">Show Account</a>
href
, specify a Map
value to the attrs
attribute. These attributes will be applied directly to the href, not passed through to be used as request parameters.<link:accountDetails attrs="[class: 'fancy']" acctNumber="8675309"> Show Account </link:accountDetails>
<a href="/details/8675309" class="fancy">Show Account</a>
6.4.10 自定义URL格式
The default URL Mapping mechanism supports camel case names in the URLs. The default URL for accessing an action namedaddNumbers
in a controller named MathHelperController
would be something like /mathHelper/addNumbers
. Grails allows for the customization of this pattern and provides an implementation which replaces the camel case convention with a hyphenated convention that would support URLs like /math-helper/add-numbers
. To enable hyphenated URLs assign a value of "hyphenated" to the grails.web.url.converter
property in grails-app/conf/Config.groovy
.// grails-app/conf/Config.groovygrails.web.url.converter = 'hyphenated'
grails.web.UrlConverter.BEAN_NAME
. If Grails finds a bean in the context with that name, it will be used as the default converter and there is no need to assign a value to the grails.web.url.converter
config property.// src/groovy/com/myapplication/MyUrlConverterImpl.groovypackage com.myapplicationclass MyUrlConverterImpl implements grails.web.UrlConverter { String toUrlElement(String propertyOrClassName) { // return some representation of a property or class name that should be used in URLs… } }
// grails-app/conf/spring/resources.groovybeans = {
"${grails.web.UrlConverter.BEAN_NAME}"(com.myapplication.MyUrlConverterImpl)
}
6.5 Web工作流
Overview
Grails supports the creation of web flows built on the Spring Web Flow project. A web flow is a conversation that spans multiple requests and retains state for the scope of the flow. A web flow also has a defined start and end state.Web flows don't require an HTTP session, but instead store their state in a serialized form, which is then restored using a flow execution key that Grails passes around as a request parameter. This makes flows far more scalable than other forms of stateful application that use the HttpSession and its inherit memory and clustering concerns.Web flow is essentially an advanced state machine that manages the "flow" of execution from one state to the next. Since the state is managed for you, you don't have to be concerned with ensuring that users enter an action in the middle of some multi step flow, as web flow manages that for you. This makes web flow perfect for use cases such as shopping carts, hotel booking and any application that has multi page work flows.
From Grails 1.2 onwards Webflow is no longer in Grails core, so you must install the Webflow plugin to use this feature: grails install-plugin webflow
Creating a Flow
To create a flow create a regular Grails controller and add an action that ends with the conventionFlow
. For example:class BookController { def index() {
redirect(action: "shoppingCart")
} def shoppingCartFlow = {
…
}
}
Flow
suffix. In other words the name of the action of the above flow is shoppingCart
.
6.5.1 开始和结束状态
As mentioned before a flow has a defined start and end state. A start state is the state which is entered when a user first initiates a conversation (or flow). The start state of a Grails flow is the first method call that takes a block. For example:class BookController { … def shoppingCartFlow ={ showCart { on("checkout").to "enterPersonalDetails" on("continueShopping").to "displayCatalogue" } … displayCatalogue { redirect(controller: "catalogue", action: "show") } displayInvoice() } }
showCart
node is the start state of the flow. Since the showCart state doesn't define an action or redirect it is assumed be a view state that, by convention, refers to the view grails-app/views/book/shoppingCart/showCart.gsp
.Notice that unlike regular controller actions, the views are stored within a directory that matches the name of the flow: grails-app/views/book/shoppingCart
.The shoppingCart
flow also has two possible end states. The first is displayCatalogue
which performs an external redirect to another controller and action, thus exiting the flow. The second is displayInvoice
which is an end state as it has no events at all and will simply render a view called grails-app/views/book/shoppingCart/displayInvoice.gsp
whilst ending the flow at the same time.Once a flow has ended it can only be resumed from the start state, in this case showCart
, and not from any other state.
6.5.2 操作状态和视图状态
View states
A view state is a one that doesn't define anaction
or a redirect
. So for example this is a view state:enterPersonalDetails { on("submit").to "enterShipping" on("return").to "showCart" }
grails-app/views/book/shoppingCart/enterPersonalDetails.gsp
by default. Note that the enterPersonalDetails
state defines two events: submit
and return
. The view is responsible for triggering these events. Use the render
method to change the view to be rendered:enterPersonalDetails { render(view: "enterDetailsView") on("submit").to "enterShipping" on("return").to "showCart" }
grails-app/views/book/shoppingCart/enterDetailsView.gsp
. Start the view
parameter with a / to use a shared view:enterPersonalDetails { render(view: "/shared/enterDetailsView") on("submit").to "enterShipping" on("return").to "showCart" }
grails-app/views/shared/enterDetailsView.gsp
Action States
An action state is a state that executes code but does not render a view. The result of the action is used to dictate flow transition. To create an action state you define an action to to be executed. This is done by calling theaction
method and passing it a block of code to be executed:listBooks { action { [bookList: Book.list()] } on("success").to "showCatalogue" on(Exception).to "handleError" }
success
event will be triggered. In this case since we return a Map, which is regarded as the "model" and is automatically placed in flow scope.In addition, in the above example we also use an exception handler to deal with errors on the line:on(Exception).to "handleError"
handleError
in the case of an exception.You can write more complex actions that interact with the flow request context:processPurchaseOrder { action { def a = flow.address def p = flow.person def pd = flow.paymentDetails def cartItems = flow.cartItems flow.clear() def o = new Order(person: p, shippingAddress: a, paymentDetails: pd) o.invoiceNumber = new Random().nextInt(9999999) for (item in cartItems) { o.addToItems item } o.save() [order: o] } on("error").to "confirmPurchase" on(Exception).to "confirmPurchase" on("success").to "displayInvoice" }
Order
object. It then returns the order as the model. The important thing to note here is the interaction with the request context and "flow scope".Transition Actions
Another form of action is what is known as a transition action. A transition action is executed directly prior to state transition once an event has been triggered. A simple example of a transition action can be seen below:enterPersonalDetails { on("submit") { log.trace "Going to enter shipping" }.to "enterShipping" on("return").to "showCart" }
submit
event that simply logs the transition. Transition states are very useful for data binding and validation, which is covered in a later section.
6.5.3 工作流执行事件
In order to transition execution of a flow from one state to the next you need some way of trigger an event that indicates what the flow should do next. Events can be triggered from either view states or action states.Triggering Events from a View State
As discussed previously the start state of the flow in a previous code listing deals with two possible events. Acheckout
event and a continueShopping
event:def shoppingCartFlow = { showCart { on("checkout").to "enterPersonalDetails" on("continueShopping").to "displayCatalogue" } … }
showCart
event is a view state it will render the view grails-app/book/shoppingCart/showCart.gsp
. Within this view you need to have components that trigger flow execution. On a form this can be done use the submitButton tag:<g:form action="shoppingCart"> <g:submitButton name="continueShopping" value="Continue Shopping" /> <g:submitButton name="checkout" value="Checkout" /> </g:form>
shoppingCart
flow. The name attribute of each submitButton tag signals which event will be triggered. If you don't have a form you can also trigger an event with the link tag as follows:<g:link action="shoppingCart" event="checkout" />
Triggering Events from an Action
To trigger an event from anaction
you invoke a method. For example there is the built in error()
and success()
methods. The example below triggers the error()
event on validation failure in a transition action:enterPersonalDetails { on("submit") { def p = new Person(params) flow.person = p if (!p.validate()) return error() }.to "enterShipping" on("return").to "showCart" }
enterPersonalDetails
state.With an action state you can also trigger events to redirect flow:shippingNeeded { action { if (params.shippingRequired) yes() else no() } on("yes").to "enterShipping" on("no").to "enterPayment" }
6.5.4 工作流的作用域
Scope Basics
You'll notice from previous examples that we used a special object calledflow
to store objects within "flow scope". Grails flows have five different scopes you can utilize:
request
- Stores an object for the scope of the current requestflash
- Stores the object for the current and next request onlyflow
- Stores objects for the scope of the flow, removing them when the flow reaches an end stateconversation
- Stores objects for the scope of the conversation including the root flow and nested subflowssession
- Stores objects in the user's session
Grails service classes can be automatically scoped to a web flow scope. See the documentation on Services for more information.Returning a model Map from an action will automatically result in the model being placed in flow scope. For example, using a transition action, you can place objects within
flow
scope as follows:enterPersonalDetails { on("submit") { [person: new Person(params)] }.to "enterShipping" on("return").to "showCart" }
- Moves objects from flash scope to request scope upon transition between states;
- Merges objects from the flow and conversation scopes into the view model before rendering (so you shouldn't include a scope prefix when referencing these objects within a view, e.g. GSP pages).
Flow Scopes and Serialization
When placing objects inflash
, flow
or conversation
scope they must implement java.io.Serializable
or an exception will be thrown. This has an impact on domain classes in that domain classes are typically placed within a scope so that they can be rendered in a view. For example consider the following domain class:class Book {
String title
}
Book
class in a flow scope you will need to modify it as follows:class Book implements Serializable { String title }
class Book implements Serializable { String title Author author }
Author
association is not Serializable
you will also get an error. This also impacts closures used in GORM events such as onLoad
, onSave
and so on. The following domain class will cause an error if an instance is placed in a flow scope:class Book implements Serializable { String title def onLoad = { println "I'm loading" } }
onLoad
event cannot be serialized. To get around this you should declare all events as transient
:class Book implements Serializable { String title transient onLoad = { println "I'm loading" } }
class Book implements Serializable { String title def onLoad() { println "I'm loading" } }
The flow scope contains a reference to the Hibernate session. As a result, any object loaded into the session through a GORM query will also be in the flow and will need to implement Serializable.If you don't want your domain class to be Serializable or stored in the flow, then you will need to evict the entity manually before the end of the state:flow.persistenceContext.evict(it)
6.5.5 数据绑定和验证
In the section on start and end states, the start state in the first example triggered a transition to theenterPersonalDetails
state. This state renders a view and waits for the user to enter the required information:enterPersonalDetails { on("submit").to "enterShipping" on("return").to "showCart" }
<g:form action="shoppingCart"> <!-- Other fields --> <g:submitButton name="submit" value="Continue"></g:submitButton> <g:submitButton name="return" value="Back"></g:submitButton> </g:form>
enterPersonalDetails { on("submit") { flow.person = new Person(params) !flow.person.validate() ? error() : success() }.to "enterShipping" on("return").to "showCart" }
Person
instance within flow
scope. Also interesting is that we perform validation and invoke the error()
method if validation fails. This signals to the flow that the transition should halt and return to the enterPersonalDetails
view so valid entries can be entered by the user, otherwise the transition should continue and go to the enterShipping
state.Like regular actions, flow actions also support the notion of Command Objects by defining the first argument of the closure:enterPersonalDetails { on("submit") { PersonDetailsCommand cmd -> flow.personDetails = cmd !flow.personDetails.validate() ? error() : success() }.to "enterShipping" on("return").to "showCart" }
6.5.6 子流程和会话
Grails' Web Flow integration also supports subflows. A subflow is like a flow within a flow. For example take this search flow:def searchFlow = { displaySearchForm { on("submit").to "executeSearch" } executeSearch { action { [results:searchService.executeSearch(params.q)] } on("success").to "displayResults" on("error").to "displaySearchForm" } displayResults { on("searchDeeper").to "extendedSearch" on("searchAgain").to "displaySearchForm" } extendedSearch { // Extended search subflow subflow(controller: "searchExtensions", action: "extendedSearch") on("moreResults").to "displayMoreResults" on("noResults").to "displayNoMoreResults" } displayMoreResults() displayNoMoreResults() }
extendedSearch
state. The controller parameter is optional if the subflow is defined in the same controller as the calling flow.
Prior to 1.3.5, the previous subflow call would look likeThe subflow is another flow entirely:subflow(extendedSearchFlow)
, with the requirement that the name of the subflow state be the same as the called subflow (minusFlow
). This way of calling a subflow is deprecated and only supported for backward compatibility.
def extendedSearchFlow = { startExtendedSearch { on("findMore").to "searchMore" on("searchAgain").to "noResults" } searchMore { action { def results = searchService.deepSearch(ctx.conversation.query) if (!results) return error() conversation.extendedResults = results } on("success").to "moreResults" on("error").to "noResults" } moreResults() noResults() }
extendedResults
in conversation scope. This scope differs to flow scope as it lets you share state that spans the whole conversation not just the flow. Also notice that the end state (either moreResults
or noResults
of the subflow triggers the events in the main flow:extendedSearch { // Extended search subflow subflow(controller: "searchExtensions", action: "extendedSearch") on("moreResults").to "displayMoreResults" on("noResults").to "displayNoMoreResults" }
6.6 过滤器
Although Grails controllers support fine grained interceptors, these are only really useful when applied to a few controllers and become difficult to manage with larger applications. Filters on the other hand can be applied across a whole group of controllers, a URI space or to a specific action. Filters are far easier to plugin and maintain completely separately to your main controller logic and are useful for all sorts of cross cutting concerns such as security, logging, and so on.
虽然,Grails的控制器支持良好的细粒度拦截器,但它们只是对少数控制器有用,当处理大型应用时就会变得很困难。另一方面,过滤器能横跨整组控制器,一个URI空间或者一种具体的操作。相比插件来说,过滤器更容易、更彻底地维护分离你控制器的主要逻辑,也非常有利于像安全,日志等等这样的横切关注点。
6.6.1 应用过滤器
To create a filter create a class that ends with the convention Each filter you define within the The scope of the filter can be one of the following things:
In addition, the order in which you define the filters within the
要创建一个过滤器,只需要在Filters
in the grails-app/conf
directory. Within this class define a code block called filters
that contains the filter definitions:class ExampleFilters { def filters = { // your filters here } }
filters
block has a name and a scope. The name is the method name and the scope is defined using named arguments. For example to define a filter that applies to all controllers and all actions you can use wildcards:sampleFilter(controller:'*', action:'*') { // interceptor definitions }
- A controller and/or action name pairing with optional wildcards
- A URI, with Ant path matching syntax
controller
- controller matching pattern, by default * is replaced with .* and a regex is compiledcontrollerExclude
- controller exclusion pattern, by default * is replaced with .* and a regex is compiledaction
- action matching pattern, by default * is replaced with .* and a regex is compiledactionExclude
- action exclusion pattern, by default * is replaced with .* and a regex is compiledregex
(true
/false
) - use regex syntax (don't replace '*' with '.*')uri
- a uri to match, expressed with as Ant style path (e.g. /book/**)uriExclude
- a uri pattern to exclude, expressed with as Ant style path (e.g. /book/**)find
(true
/false
) - rule matches with partial match (seejava.util.regex.Matcher.find()
)invert
(true
/false
) - invert the rule (NOT rule)
- All controllers and actions
all(controller: '*', action: '*') {}
- Only for the
BookController
justBook(controller: 'book', action: '*') {}
- All controllers except the
BookController
notBook(controller: 'book', invert: true) {}
- All actions containing 'save' in the action name
saveInActionName(action: '*save*', find: true) {}
- All actions starting with the letter 'b' except for actions beginning with the phrase 'bad*'
actionBeginningWithBButNotBad(action: 'b*', actionExclude: 'bad*', find: true) {}
- Applied to a URI space
someURIs(uri: '/book/**') {}
- Applied to all URIs
allURIs(uri: '/**') {}
filters
code block dictates the order in which they are executed. To control the order of execution between Filters
classes, you can use the dependsOn
property discussed in filter dependencies section.Note: When exclude patterns are used they take precedence over the matching patterns. For example, if action is 'b*' and actionExclude is 'bad*' then actions like 'best' and 'bien' will have that filter applied but actions like 'bad' and 'badlands' will not.
grails-app/conf
目录下创建一个符合规约以Filters
结尾的类即可。在此类中,定义一个名为filters
的代码块,用以包含过滤器的定义:class ExampleFilters { def filters = { // your filters here } }
filters
代码块内的每一个过滤器有一个名称和作用域。名称就是其方法名,作用域是通过命名参数定义的。比如,要定义一个应用于所有控制器和操作的过滤器,你可以使用通配符:sampleFilter(controller:'*', action:'*') { // interceptor definitions }
- 一个控制器或者操作名称,支持可选的通配符
- 一个URI,符合Ant路径(path)匹配语法
controller
- 控制器匹配模式,缺省情况下,其用*可以替代.*,并且被编译为一个正则表达式controllerExclude
- 控制器的排除模式,缺省情况下,其用*可以替代.*,并且被编译为一个正则表达式action
- 操作匹配模式,缺省情况下,其用*可以替代.*,并且被编译为一个正则表达式actionExclude
- 操作排除模式,缺省情况下,其用*可以替代.*,并且被编译为一个正则表达式regex
(true
/false
) - 使用正则表达式语法(不使用'*'代替'.*')uri
- 一个uri匹配,使用Ant风格的路径(path)(比如 /book/**)uriExclude
- 一个uri排除匹配,使用Ant风格的路径(path)(比如 /book/**)find
(true
/false
) - 符合部分匹配的规则匹配(更多请参考java.util.regex.Matcher.find()
)invert
(true
/false
) - 反转规则(不符合此规则的条件)
- 匹配所有的控制器和操作
all(controller: '*', action: '*') {}
- 仅仅匹配
BookController
justBook(controller: 'book', action: '*') {}
- 匹配所有的控制器,除了
BookController
notBook(controller: 'book', invert: true) {}
- 匹配所有操作名包含'save'的操作
saveInActionName(action: '*save*', find: true) {}
- 匹配所有操作字母以'b'开头的操作,不过'bad*'除外
actionBeginningWithBButNotBad(action: 'b*', actionExclude: 'bad*', find: true) {}
- 应用于一个URI
someURIs(uri: '/book/**') {}
- 应用于所有的URIs
allURIs(uri: '/**') {}
filters
代码块中定义的过滤器顺序就是它们被执行的顺序。要控制Filters
类之间的执行顺序,你可以使用dependsOn
属性,更多信息将在过滤器的依赖章节讨论。注意:当使用排除模式的时候,其优先级将高于其他的匹配模式。比如一个作用域,其action是'b*'而actionExclude是'bad*',那么操作名称是'best'和'bien'将应用于此过滤器,而操作名是'bad'和'badlands'却没有。
6.6.2 过滤器的类型
Within the body of the filter you can then define one or several of the following interceptor types for the filter:
Here the In this logging example we just log various request information, but note that the
在过滤器的主体内,你可以定义下列过滤器中拦截器类型的一个或者几个:
before
- Executed before the action. Returnfalse
to indicate that the response has been handled that that all future filters and the action should not executeafter
- Executed after an action. Takes a first argument as the view model to allow modification of the model before rendering the viewafterView
- Executed after view rendering. Takes an Exception as an argument which will be non-null
if an exception occurs during processing. Note: this Closure is called before the layout is applied.
class SecurityFilters { def filters = { loginCheck(controller: '*', action: '*') { before = { if (!session.user && !actionName.equals('login')) { redirect(action: 'login') return false } } } } }
loginCheck
filter uses a before
interceptor to execute a block of code that checks if a user is in the session and if not redirects to the login action. Note how returning false ensure that the action itself is not executed.Here's a more involved example that demonstrates all three filter types:import java.util.concurrent.atomic.AtomicLongclass LoggingFilters { private static final AtomicLong REQUEST_NUMBER_COUNTER = new AtomicLong() private static final String START_TIME_ATTRIBUTE = 'Controller__START_TIME__' private static final String REQUEST_NUMBER_ATTRIBUTE = 'Controller__REQUEST_NUMBER__' def filters = { logFilter(controller: '*', action: '*') { before = { if (!log.debugEnabled) return true long start = System.currentTimeMillis() long currentRequestNumber = REQUEST_NUMBER_COUNTER.incrementAndGet() request[START_TIME_ATTRIBUTE] = start request[REQUEST_NUMBER_ATTRIBUTE] = currentRequestNumber log.debug "preHandle request #$currentRequestNumber : " + "'$request.servletPath'/'$request.forwardURI', " + "from $request.remoteHost ($request.remoteAddr) " + " at ${new Date()}, Ajax: $request.xhr, controller: $controllerName, " + "action: $actionName, params: ${new TreeMap(params)}" return true } after = { Map model -> if (!log.debugEnabled) return true long start = request[START_TIME_ATTRIBUTE] long end = System.currentTimeMillis() long requestNumber = request[REQUEST_NUMBER_ATTRIBUTE] def msg = "postHandle request #$requestNumber: end ${new Date()}, " + "controller total time ${end - start}ms" if (log.traceEnabled) { log.trace msg + "; model: $model" } else { log.debug msg } } afterView = { Exception e -> if (!log.debugEnabled) return true long start = request[START_TIME_ATTRIBUTE] long end = System.currentTimeMillis() long requestNumber = request[REQUEST_NUMBER_ATTRIBUTE] def msg = "afterCompletion request #$requestNumber: " + "end ${new Date()}, total time ${end - start}ms" if (e) { log.debug "$msg \n\texception: $e.message", e } else { log.debug msg } } } } }
model
map in the after
filter is mutable. If you need to add or remove items from the model map you can do that in the after
filter.
before
- 在操作之前执行。返回值false
表示响应已经被符合条件的过滤器处理过,并且其操作不被执行after
- 在操作之后执行。其第一个参数为视图模型(view model),并且允许在渲染视图之前修改此模型afterView
- 在渲染视图之后执行。如果有异常发生,其第一个参数为一个非null
的异常。注意:此闭包在应用布局以前被调用。
class SecurityFilters { def filters = { loginCheck(controller: '*', action: '*') { before = { if (!session.user && !actionName.equals('login')) { redirect(action: 'login') return false } } } } }
loginCheck
过滤器使用了before
拦截器来执行一个代码块,用以检查一个用户是否在会话当中,如果不在,就重定向到login操作。注意:如何通过返回false的方式来确保操作本身不被执行。下面是一个更深入的示例来演示所有的三种过滤器类型:import java.util.concurrent.atomic.AtomicLongclass LoggingFilters { private static final AtomicLong REQUEST_NUMBER_COUNTER = new AtomicLong() private static final String START_TIME_ATTRIBUTE = 'Controller__START_TIME__' private static final String REQUEST_NUMBER_ATTRIBUTE = 'Controller__REQUEST_NUMBER__' def filters = { logFilter(controller: '*', action: '*') { before = { if (!log.debugEnabled) return true long start = System.currentTimeMillis() long currentRequestNumber = REQUEST_NUMBER_COUNTER.incrementAndGet() request[START_TIME_ATTRIBUTE] = start request[REQUEST_NUMBER_ATTRIBUTE] = currentRequestNumber log.debug "preHandle request #$currentRequestNumber : " + "'$request.servletPath'/'$request.forwardURI', " + "from $request.remoteHost ($request.remoteAddr) " + " at ${new Date()}, Ajax: $request.xhr, controller: $controllerName, " + "action: $actionName, params: ${new TreeMap(params)}" return true } after = { Map model -> if (!log.debugEnabled) return true long start = request[START_TIME_ATTRIBUTE] long end = System.currentTimeMillis() long requestNumber = request[REQUEST_NUMBER_ATTRIBUTE] def msg = "postHandle request #$requestNumber: end ${new Date()}, " + "controller total time ${end - start}ms" if (log.traceEnabled) { log.trace msg + "; model: $model" } else { log.debug msg } } afterView = { Exception e -> if (!log.debugEnabled) return true long start = request[START_TIME_ATTRIBUTE] long end = System.currentTimeMillis() long requestNumber = request[REQUEST_NUMBER_ATTRIBUTE] def msg = "afterCompletion request #$requestNumber: " + "end ${new Date()}, total time ${end - start}ms" if (e) { log.debug "$msg \n\texception: $e.message", e } else { log.debug msg } } } } }
after
过滤器中的model
是可变的。如果你需要增加或者移除model的内容,可以在after
过滤器中实现。
6.6.3 变量和作用域
Filters support all the common properties available to controllers and tag libraries, plus the application context:
过滤器支持控制器和标签库的所有公共属性,外加应用环境上下文(application context):
- request - The HttpServletRequest object
- response - The HttpServletResponse object
- session - The HttpSession object
- servletContext - The ServletContext object
- flash - The flash object
- params - The request parameters object
- actionName - The action name that is being dispatched to
- controllerName - The controller name that is being dispatched to
- grailsApplication - The Grails application currently running
- applicationContext - The ApplicationContext object
- request - HttpServletRequest对象
- response - HttpServletResponse对象
- session - HttpSession对象
- servletContext - ServletContext对象
- flash - flash对象
- params - 请求参数对象
- actionName - 正在使用的操作名称
- controllerName - 正在使用的控制器名称
- grailsApplication - 当前正在运行的Grails应用
- applicationContext - ApplicationContext对象
6.6.4 过滤器依赖
In a MyFilters specifically
在一个Filters
class, you can specify any other Filters
classes that should first be executed using the dependsOn
property. This is used when a Filters
class depends on the behavior of another Filters
class (e.g. setting up the environment, modifying the request/session, etc.) and is defined as an array of Filters
classes.Take the following example Filters
classes:class MyFilters { def dependsOn = [MyOtherFilters] def filters = { checkAwesome(uri: "/*") { before = { if (request.isAwesome) { // do something awesome } } } checkAwesome2(uri: "/*") { before = { if (request.isAwesome) { // do something else awesome } } } } }
class MyOtherFilters { def filters = { makeAwesome(uri: "/*") { before = { request.isAwesome = true } } doNothing(uri: "/*") { before = { // do nothing } } } }
dependsOn
MyOtherFilters. This will cause all the filters in MyOtherFilters whose scope matches the current request to be executed before those in MyFilters. For a request of "/test", which will match the scope of every filter in the example, the execution order would be as follows:
- MyOtherFilters - makeAwesome
- MyOtherFilters - doNothing
- MyFilters - checkAwesome
- MyFilters - checkAwesome2
Filters
classes are enabled and the execution order of filters within each Filters
class are preserved.If any cyclical dependencies are detected, the filters with cyclical dependencies will be added to the end of the filter chain and processing will continue. Information about any cyclical dependencies that are detected will be written to the logs. Ensure that your root logging level is set to at least WARN or configure an appender for the Grails Filters Plugin (org.codehaus.groovy.grails.plugins.web.filters.FiltersGrailsPlugin
) when debugging filter dependency issues.
Filters
类中,你可以使用dependsOn
属性来指定其他任意Filters
类先被执行。这经常用在一个Filters
类依赖于另外一个Filters
类的行为的时候(比如,设置环境,修改请求/会话等),并且可以定义为一个Filters
类的数组。以如下所示的Filters
类为例:class MyFilters { def dependsOn = [MyOtherFilters] def filters = { checkAwesome(uri: "/*") { before = { if (request.isAwesome) { // do something awesome } } } checkAwesome2(uri: "/*") { before = { if (request.isAwesome) { // do something else awesome } } } } }
class MyOtherFilters { def filters = { makeAwesome(uri: "/*") { before = { request.isAwesome = true } } doNothing(uri: "/*") { before = { // do nothing } } } }
dependsOn
为MyOtherFilters。这将导致MyOtherFilters中符合当前请求的所有过滤器优先于MyFilters执行。对一个"/test"请求来说,示例中的每一个过滤器都会匹配到,那么其执行的顺序将如下所示:
- MyOtherFilters - makeAwesome
- MyOtherFilters - doNothing
- MyFilters - checkAwesome
- MyFilters - checkAwesome2
Filters
类之间的执行顺序是定制的,并且每个Filters
类内的过滤器顺序是预置的。如果任何循环依赖被检测到的话,那么循环依赖的过滤器将被加到过滤器链最后,并且处理将继续进行。任何循环依赖的信息将被记录到日志当中,不过在调试过滤器依赖问题的时候,要确保你的根日志级别至少是WARN或者为Grails的过滤器插件(org.codehaus.groovy.grails.plugins.web.filters.FiltersGrailsPlugin
)配置一个输出器。
6.7 Ajax
Ajax is the driving force behind the shift to richer web applications. These types of applications in general are better suited to agile, dynamic frameworks written in languages like Groovy and Ruby Grails provides support for building Ajax applications through its Ajax tag library. For a full list of these see the Tag Library Reference.
Ajax是更丰富WEB应用背后的驱动力,这些应该通常都是使用敏捷的,动态的语言来完成的,比如 Groovy 和 Ruby 。 Grails是通过其Ajax标签库来构建Ajax应用的,更完整的列表请参考标签库索引。
6.7.1 Ajax支持
By default Grails ships with the jQuery library, but through the Plugin system provides support for other frameworks such as Prototype, Dojo:http://dojotoolkit.org/, Yahoo UI:http://developer.yahoo.com/yui/ and the Google Web Toolkit.This section covers Grails' support for Ajax in general. To get started, add this line to the You can replace
缺省情况下,Grails采用的是 jQuery 框架,但是通过其插件系统也提供了对其他框架的支持,比如Prototype、 Dojo:http://dojotoolkit.org/、Yahoo UI:http://developer.yahoo.com/yui/和Google Web Toolkit。本节将介绍Grails对Ajax的通用支持。在开始之前,请先在你页面的<head>
tag of your page:<g:javascript library="jquery" />
jQuery
with any other library supplied by a plugin you have installed. This works because of Grails' support for adaptive tag libraries. Thanks to Grails' plugin system there is support for a number of different Ajax libraries including (but not limited to):
- jQuery
- Prototype
- Dojo
- YUI
- MooTools
<head>
标签部分增加如下内容:<g:javascript library="jquery" />
jQuery
。这要感谢Grails的插件系统,有了它才能支持这么多不同的Ajax框架库,包括但不限于如下所提到的:
- jQuery
- Prototype
- Dojo
- YUI
- MooTools
6.7.1.1 异步超链接
Remote content can be loaded in a number of ways, the most commons way is through the remoteLink tag. This tag allows the creation of HTML anchor tags that perform an asynchronous request and optionally set the response in an element. The simplest way to create a remote link is as follows:The above link sends an asynchronous request to the
远程内容可以使用多种方法载入,最常使用的方法是通过remoteLink标签。此标签将创建HTML的锚标记用以执行一个异步请求,并在一个元素中设置响应内容。最简单的创建一个远程连接的方法如下:<g:remoteLink action="delete" id="1">Delete Book</g:remoteLink>
delete
action of the current controller with an id of 1
.
<g:remoteLink action="delete" id="1">Delete Book</g:remoteLink>
delete
操作。
6.7.1.2 更新内容
This is great, but usually you provide feedback to the user about what happened:GSP code:The above example will call the action and set the contents of the Here the
目前都还不错,但一般来说你会提供一些信息反馈给用户,以告诉都发生过什么,比如:def delete() {
def b = Book.get(params.id)
b.delete()
render "Book ${b.id} was deleted"
}
<div id="message"></div> <g:remoteLink action="delete" id="1" update="message"> Delete Book </g:remoteLink>
message
div
to the response in this case "Book 1 was deleted"
. This is done by the update
attribute on the tag, which can also take a Map to indicate what should be updated on failure:<div id="message"></div> <div id="error"></div> <g:remoteLink update="[success: 'message', failure: 'error']" action="delete" id="1"> Delete Book </g:remoteLink>
error
div will be updated if the request failed.
def delete() {
def b = Book.get(params.id)
b.delete()
render "Book ${b.id} was deleted"
}
<div id="message"></div> <g:remoteLink action="delete" id="1" update="message"> Delete Book </g:remoteLink>
delete
操作,并且将响应内容"Book 1 was deleted"
设置到id为message
的div
中,这是通过标签中的update
属性来完成的。此外还可以用Map参数来设定失败时要更新那些,比如:<div id="message"></div> <div id="error"></div> <g:remoteLink update="[success: 'message', failure: 'error']" action="delete" id="1"> Delete Book </g:remoteLink>
error
将会被更新。
6.7.1.3 异步Form提交
An HTML form can also be submitted asynchronously in one of two ways. Firstly using the formRemote tag which expects similar attributes to those for the remoteLink tag:Or alternatively you can use the submitToRemote tag to create a submit button. This allows some buttons to submit remotely and some not depending on the action:
HTML的表单可以通过以下两种方式的一种进行异步提交。其一,使用formRemote标签,它的属性跟remoteLink标签类似,比如:<g:formRemote url="[controller: 'book', action: 'delete']" update="[success: 'message', failure: 'error']"> <input type="hidden" name="id" value="1" /> <input type="submit" value="Delete Book!" /> </g:formRemote >
<form action="delete"> <input type="hidden" name="id" value="1" /> <g:submitToRemote action="delete" update="[success: 'message', failure: 'error']" /> </form>
<g:formRemote url="[controller: 'book', action: 'delete']" update="[success: 'message', failure: 'error']"> <input type="hidden" name="id" value="1" /> <input type="submit" value="Delete Book!" /> </g:formRemote >
<form action="delete"> <input type="hidden" name="id" value="1" /> <g:submitToRemote action="delete" update="[success: 'message', failure: 'error']" /> </form>
6.7.1.4 Ajax事件
Specific JavaScript can be called if certain events occur, all the events start with the "on" prefix and let you give feedback to the user where appropriate, or take other action:The above code will execute the "showProgress()" function which may show a progress bar or whatever is appropriate. Other events include:
当某个事件发生时,特定的JavaScript将会被调用到,所有这些事件都是以"on"为前缀,并且合适地反馈给用户或者其他处理,比如:<g:remoteLink action="show" id="1" update="success" onLoading="showProgress()" onComplete="hideProgress()">Show Book 1</g:remoteLink>
onSuccess
- The JavaScript function to call if successfulonFailure
- The JavaScript function to call if the call failedon_ERROR_CODE
- The JavaScript function to call to handle specified error codes (eg on404="alert('not found!')")onUninitialized
- The JavaScript function to call the a Ajax engine failed to initialiseonLoading
- The JavaScript function to call when the remote function is loading the responseonLoaded
- The JavaScript function to call when the remote function is completed loading the responseonComplete
- The JavaScript function to call when the remote function is complete, including any updates
XmlHttpRequest
object you can use the implicit event parameter e
to obtain it:<g:javascript> function fireMe(e) { alert("XmlHttpRequest = " + e) } } </g:javascript> <g:remoteLink action="example" update="success" onSuccess="fireMe(e)">Ajax Link</g:remoteLink>
<g:remoteLink action="show" id="1" update="success" onLoading="showProgress()" onComplete="hideProgress()">Show Book 1</g:remoteLink>
onSuccess
- 成功时要调用的JavaScript函数onFailure
- 失败时要调用的JavaScript函数on_ERROR_CODE
- 处理特定的错误编码(比如on404="alert('not found!')")时要调用的JavaScript函数onUninitialized
- Ajax引擎初始化失败时要调用的JavaScript函数onLoading
- 远程调用正在加载响应时要调用的JavaScript函数onLoaded
- 远程调用已经加载完响应时要调用的JavaScript函数onComplete
- 远程调用完全结束(包括更新内容)时要调用的JavaScript函数
XmlHttpRequest
对象,你可以使用隐式的事件参数e
来获取它:<g:javascript> function fireMe(e) { alert("XmlHttpRequest = " + e) } } </g:javascript> <g:remoteLink action="example" update="success" onSuccess="fireMe(e)">Ajax Link</g:remoteLink>
6.7.2 用Prototype实现Ajax
Grails features an external plugin to add Prototype support to Grails. To install the plugin type the following command from the root of your project in a terminal window:This will download the current supported version of the Prototype plugin and install it into your Grails project. With that done you can add the following reference to the top of your page:If you require Scriptaculous too you can do the following instead:Now all of Grails tags such as remoteLink, formRemote and submitToRemote work with Prototype remoting.
Grails通过一个外部插件来提供对 Prototype 的支持。要安装此插件,在字符终端的窗口中,进入你工程的根目录,输入下面命令即可:grails install-plugin prototype
<g:javascript library="prototype" />
<g:javascript library="scriptaculous" />
grails install-plugin prototype
<g:javascript library="prototype" />
<g:javascript library="scriptaculous" />
6.7.3 用Dojo实现Ajax
Grails features an external plugin to add Dojo support to Grails. To install the plugin type the following command from the root of your project in a terminal window:This will download the current supported version of Dojo and install it into your Grails project. With that done you can add the following reference to the top of your page:Now all of Grails tags such as remoteLink, formRemote and submitToRemote work with Dojo remoting.
Grails通过一个外部插件来提供对 Dojo 的支持。要安装此插件,在字符终端的窗口中,进入你工程的根目录,输入下面命令即可:grails install-plugin dojo
<g:javascript library="dojo" />
grails install-plugin dojo
<g:javascript library="dojo" />
6.7.4 用GWT实现Ajax
Grails also features support for the Google Web Toolkit through a plugin. There is comprehensive documentation available on the Grails wiki.
Grails通过插件对 Google Web Toolkit 也提供了支持,其复杂的文档请参考官方网站。
6.7.5 服务端的Ajax
There are a number of different ways to implement Ajax which are typically broken down into:
实现Ajax有很多种不同的方式,但大体可分为如下几类:
- Content Centric Ajax - Where you just use the HTML result of a remote call to update the page
- Data Centric Ajax - Where you actually send an XML or JSON response from the server and programmatically update the page
- Script Centric Ajax - Where the server sends down a stream of JavaScript to be evaluated on the fly
- 内容为中心的Ajax - 使用远程调用返回的HTML结果更新页面
- 数据为中心的Ajax - 从服务器端发送接收XML或者JSON,并且以编程的方式更新页面
- 脚本为中心的Ajax - 接收从服务器端发出的JavaScript流,并且运行之
Content Centric Ajax
Just to re-cap, content centric Ajax involves sending some HTML back from the server and is typically done by rendering a template with the render method:def showBook() {
def b = Book.get(params.id) render(template: "bookTemplate", model: [book: b])
}
<g:remoteLink action="showBook" id="${book.id}" update="book${book.id}">Update Book</g:remoteLink><div id="book${book.id}"> <!--existing book mark-up --> </div>
内容为中心的Ajax
重申一下,内容为中心的Ajax主要跟从服务器端返回HTML内容相关,这些内容一般是通过使用render渲染模板的方式得到::def showBook() {
def b = Book.get(params.id) render(template: "bookTemplate", model: [book: b])
}
<g:remoteLink action="showBook" id="${book.id}" update="book${book.id}">Update Book</g:remoteLink><div id="book${book.id}"> <!--existing book mark-up --> </div>
Data Centric Ajax with JSON
Data Centric Ajax typically involves evaluating the response on the client and updating programmatically. For a JSON response with Grails you would typically use Grails' JSON marshalling capability:import grails.converters.JSONdef showBook() {
def b = Book.get(params.id) render b as JSON
}
<g:javascript> function updateBook(e) { var book = eval("("+e.responseText+")") // evaluate the JSON $("book" + book.id + "_title").innerHTML = book.title } <g:javascript> <g:remoteLink action="test" update="foo" onSuccess="updateBook(e)"> Update Book </g:remoteLink> <g:set var="bookId">book${book.id}</g:set> <div id="${bookId}"> <div id="${bookId}_title">The Stand</div> </div>
JSON实现的数据为中心的Ajax
数据为中心的Ajax通常是在客户端以编程的方式处理返回结果和内容更新。在Grails中,一个JSON响应通常是使用JSON编组(marshalling)来处理的:import grails.converters.JSONdef showBook() {
def b = Book.get(params.id) render b as JSON
}
<g:javascript> function updateBook(e) { var book = eval("("+e.responseText+")") // evaluate the JSON $("book" + book.id + "_title").innerHTML = book.title } <g:javascript> <g:remoteLink action="test" update="foo" onSuccess="updateBook(e)"> Update Book </g:remoteLink> <g:set var="bookId">book${book.id}</g:set> <div id="${bookId}"> <div id="${bookId}_title">The Stand</div> </div>
Data Centric Ajax with XML
On the server side using XML is equally simple:import grails.converters.XMLdef showBook() {
def b = Book.get(params.id) render b as XML
}
<g:javascript> function updateBook(e) { var xml = e.responseXML var id = xml.getElementsByTagName("book").getAttribute("id") $("book" + id + "_title") = xml.getElementsByTagName("title")[0].textContent } <g:javascript> <g:remoteLink action="test" update="foo" onSuccess="updateBook(e)"> Update Book </g:remoteLink> <g:set var="bookId">book${book.id}</g:set> <div id="${bookId}"> <div id="${bookId}_title">The Stand</div> </div>
XML实现的数据为中心的Ajax
在服务器端,处理XML是很容易的:import grails.converters.XMLdef showBook() {
def b = Book.get(params.id) render b as XML
}
<g:javascript> function updateBook(e) { var xml = e.responseXML var id = xml.getElementsByTagName("book").getAttribute("id") $("book" + id + "_title") = xml.getElementsByTagName("title")[0].textContent } <g:javascript> <g:remoteLink action="test" update="foo" onSuccess="updateBook(e)"> Update Book </g:remoteLink> <g:set var="bookId">book${book.id}</g:set> <div id="${bookId}"> <div id="${bookId}_title">The Stand</div> </div>
Script Centric Ajax with JavaScript
Script centric Ajax involves actually sending JavaScript back that gets evaluated on the client. An example of this can be seen below:def showBook() { def b = Book.get(params.id) response.contentType = "text/javascript" String title = b.title.encodeAsJavascript() render "$('book${b.id}_title')='${title}'" }
contentType
to text/javascript
. If you use Prototype on the client the returned JavaScript will automatically be evaluated due to this contentType
setting.Obviously in this case it is critical that you have an agreed client-side API as you don't want changes on the client breaking the server. This is one of the reasons Rails has something like RJS. Although Grails does not currently have a feature such as RJS there is a Dynamic JavaScript Plugin that offers similar capabilities.
JavaScript实现的脚本为中心的Ajax
脚本为中心的Ajax主要在客户端处理从后台返回的JavaScript,并且运行它们。比如如下示例:def showBook() { def b = Book.get(params.id) response.contentType = "text/javascript" String title = b.title.encodeAsJavascript() render "$('book${b.id}_title')='${title}'" }
contentType
为text/javascript
。如果你在客户端使用的是Prototype,它会根据contentType
的设置而自动执行。很明显,这种情况下,有一个很严重的前提,那就是你必须认可服务器端将依赖客户端的API,这也是Rails(Grails就是受其启发而来的--译者注)存在RJS的一个原因。尽管Grails并没有类似于RJS的功能,但是有一个动态JavaScript插件提供了类似的功能。Responding to both Ajax and non-Ajax requests
It's straightforward to have the same Grails controller action handle both Ajax and non-Ajax requests. Grails adds theisXhr()
method to HttpServletRequest
which can be used to identify Ajax requests. For example you could render a page fragment using a template for Ajax requests or the full page for regular HTTP requests:def listBooks() { def books = Book.list(params) if (request.xhr) { render template: "bookTable", model: [books: books] } else { render view: "list", model: [books: books] } }
响应Ajax和非Ajax请求
使用同一个控制器和操作来处理Ajax和非Ajax请求是非常直截了当的。Grails为HttpServletRequest
增加了一个isXhr()
用以标识是否为Ajax请求。比如你可以使用模板为Ajax请求渲染一个页面片段,否则就渲染一个完整的页面:def listBooks() { def books = Book.list(params) if (request.xhr) { render template: "bookTable", model: [books: books] } else { render view: "list", model: [books: books] } }
6.8 内容协商
Grails has built in support for Content negotiation using either the HTTP
Grails通过HTTP的Accept
header, an explicit format request parameter or the extension of a mapped URI.
Accept
报头(显式的参数请求方式)或者扩展的URI映射来提供对内容协商的支持。Configuring Mime Types
Before you can start dealing with content negotiation you need to tell Grails what content types you wish to support. By default Grails comes configured with a number of different content types withingrails-app/conf/Config.groovy
using the grails.mime.types
setting:grails.mime.types = [ xml: ['text/xml', 'application/xml'], text: 'text-plain', js: 'text/javascript', rss: 'application/rss+xml', atom: 'application/atom+xml', css: 'text/css', csv: 'text/csv', all: '*/*', json: 'text/json', html: ['text/html','application/xhtml+xml'] ]
配置Mime类型
在你开始处理内容协商之前,你必须告诉Grails需要支持什么样的内容类型。缺省情况下,Grails将根据grails-app/conf/Config.groovy
中的grails.mime.types
设置来配置相关的内容类型:grails.mime.types = [ xml: ['text/xml', 'application/xml'], text: 'text-plain', js: 'text/javascript', rss: 'application/rss+xml', atom: 'application/atom+xml', css: 'text/css', csv: 'text/csv', all: '*/*', json: 'text/json', html: ['text/html','application/xhtml+xml'] ]
Content Negotiation using the Accept header
Every incoming HTTP request has a special Accept header that defines what media types (or mime types) a client can "accept". In older browsers this is typically:*/*
Accept
header):text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8, image/png, */*;q=0.5
property
to the response object that outlines the preferred response format. For the above example the following assertion would pass:assert 'html' == response.format
text/html
media type has the highest "quality" rating of 0.9, therefore is the highest priority. If you have an older browser as mentioned previously the result is slightly different:assert 'all' == response.format
import grails.converters.XMLclass BookController { def list() { def books = Book.list() withFormat { html bookList: books js { render "alert('hello')" } xml { render books as XML } } } }
html
then Grails will execute the html()
call only. This causes Grails to look for a view called either grails-app/views/books/list.html.gsp
or grails-app/views/books/list.gsp
. If the format is xml
then the closure will be invoked and an XML response rendered.How do we handle the "all" format? Simply order the content-types within your withFormat
block so that whichever one you want executed comes first. So in the above example, "all" will trigger the html
handler.
When using withFormat make sure it is the last call in your controller action as the return value of the withFormat
method is used by the action to dictate what happens next.
使用Accept报头的内容协商
每一个发送的HTTP请求都有个特别的Accept报头,它定义了客户端能“接受”什么样的媒体类型(或mime类型)。这个在旧的浏览器中通常是:*/*
Accept
报头):text/xml, application/xml, application/xhtml+xml, text/html;q=0.9, text/plain;q=0.8, image/png, */*;q=0.5
property
,比如上述示例,如下的断言将会通过:assert 'html' == response.format
text/html
媒体类型拥有的最高"质量"等级是0.9,因此,具有最高优先权。上述同样的示例如果在旧浏览器结果会有些稍微不同:assert 'all' == response.format
import grails.converters.XMLclass BookController { def list() { def books = Book.list() withFormat { html bookList: books js { render "alert('hello')" } xml { render books as XML } } } }
html
,那么Grails将仅仅执行html()
的调用。这将导致Grails查找名称为grails-app/views/books/list.html.gsp
或者grails-app/views/books/list.gsp
视图。 如果是xml
格式,那么响应的必包将会被调用,并且渲染为一个XML响应。那么我们该如何处理那个"all"格式呢?这要看你withFormat
代码块中内容类型(content-types)的顺序了。以上述代码为例,"all"将触发html
的处理。在使用withFormat的时候,请确保它是控制器操作的最后一个调用,如此控制器才能知道下一步要做什么。
Request format vs. Response format
As of Grails 2.0, there is a separate notion of the request format and the response format. The request format is dictated by theCONTENT_TYPE
header and is typically used to detect if the incoming request can be parsed into XML or JSON, whilst the response format uses the file extension, format parameter or ACCEPT header to attempt to deliver an appropriate response to the client.The withFormat available on controllers deals specifically with the response format. If you wish to add logic that deals with the request format then you can do so using a separate withFormat
method available on the request:request.withFormat { xml { // read XML } json { // read JSON } }
请求格式和响应格式
从Grails 2.0以来,就单独提出了 request 和 response 格式的概念。对于请求格式,通常是由CONTENT_TYPE
报头决定的,并且用以检测收入的请求是否可以被解析为XML或者JSON。而响应格式通常是由文件扩展名、参数格式或者ACCEPT报头决定,并且尝试以合适的响应返回给客户端。控制器的withFormat方法是针对响应格式而言的。如果你想增加请求格式的逻辑处理,需要单独使用request对象的withFormat
方法:request.withFormat { xml { // read XML } json { // read JSON } }
Content Negotiation with the format Request Parameter
If fiddling with request headers if not your favorite activity you can override the format used by specifying aformat
request parameter:/book/list?format=xml
"/book/list"(controller:"book", action:"list") { format = "xml" }
请求参数格式的内容协商
如果不喜欢摆弄这些请求报头,你可以通过指定请求参数的format
来覆盖这些格式:/book/list?format=xml
"/book/list"(controller:"book", action:"list") { format = "xml" }
Content Negotiation with URI Extensions
Grails also supports content negotiation using URI extensions. For example given the following URI:/book/list.xml
/book/list
instead whilst simultaneously setting the content format to xml
based on this extension. This behaviour is enabled by default, so if you wish to turn it off, you must set the grails.mime.file.extensions
property in grails-app/conf/Config.groovy
to false
:grails.mime.file.extensions = false
URI扩展的内容协商
Grails还提供了对扩展URI的内容协商的支持。比如下面URI示例:/book/list.xml
/book/list
。与此同时,设置此内容格式为xml
。缺省情况下,此行为是开启的。如果你想要关闭它,只需要设置grails-app/conf/Config.groovy
中的grails.mime.file.extensions
属性为false
即可:grails.mime.file.extensions = false
Testing Content Negotiation
To test content negotiation in a unit or integration test (see the section on Testing) you can either manipulate the incoming request headers:void testJavascriptOutput() { def controller = new TestController() controller.request.addHeader "Accept", "text/javascript, text/html, application/xml, text/xml, */*" controller.testAction() assertEquals "alert('hello')", controller.response.contentAsString }
void testJavascriptOutput() { def controller = new TestController() controller.params.format = 'js' controller.testAction() assertEquals "alert('hello')", controller.response.contentAsString }
测试内容协商
要在单元或者集成测试(请参考测试章节)中测试内容协商,你可以操作输入请求的报头方式进行,比如:void testJavascriptOutput() { def controller = new TestController() controller.request.addHeader "Accept", "text/javascript, text/html, application/xml, text/xml, */*" controller.testAction() assertEquals "alert('hello')", controller.response.contentAsString }
void testJavascriptOutput() { def controller = new TestController() controller.params.format = 'js' controller.testAction() assertEquals "alert('hello')", controller.response.contentAsString }