- 浏览: 173460 次
- 性别:
- 来自: 郴州
文章分类
最新评论
-
craengjava:
很好的,如果可以,希望分享一下详细过程,谢谢
grails and tinymce -
Iwishyou:
汗 这错别字多的...终于面试了下载抵制
Grails中文文档beta发布 -
lauphai:
什么时候有中文文档呀
grails1.2中文文档即将发布 -
Javabengou:
现在从办公室调到建行新开的营业厅所以比较忙没时间做,如果谁想接 ...
grails1.2中文文档即将发布 -
by5739:
现在出了吗?
grails1.2中文文档即将发布
h4. Returning the Model
h4. 返回模型(Model)
A model is essentially a map that the view uses when rendering. The keys within that map translate to variable names accessible by the view. There are a couple of ways to return a model, the first way is to explicitly return a map instance:
模型(Model)本质上是个map类型,当视图(view)被渲染时使用。map中的keys转变成变量名让view(视图)访问。这里有一对方式来返回模型(Model),第一种方式是明确返回map实体:
{code:java}
def show = {
[ book : Book.get( params.id ) ]
}
{code}
If no explicit model is returned the controller's properties will be used as the model thus allowing you to write code like this:
假如没有明确的模型(Model)被返回,控制器的属性将被当做模型(Model)来使用,因此允许你的代码写成下面这样:
{code:java}
class BookController {
List books
List authors
def list = {
books = Book.list()
authors = Author.list()
}
}
{code}
{note}
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.
这可能是由于事实上控制器(Controllers)是prototype(原型)范围。换句话说,每次请求,一个新的控制器(Controllers)会被创建。否则上面的代码将不是线程安全的。
{note}
In the above example the @books@ and @authors@ properties will be available in the view.
在上面示例中的 @books@ 和 @authors@属性将在view(视图)中被使用。
A more advanced approach is to return an instance of the Spring [ModelAndView|api:org.springframework.web.servlet.ModelAndView] class:
一个更加高级的方法是返回Spring [ModelAndView|api:org.springframework.web.servlet.ModelAndView] 类。
{code:java}
import org.springframework.web.servlet.ModelAndView
...
def index = {
def favoriteBooks = ... // get some books just for the index page, perhaps your favorites
// forward to the list view to show them
return new ModelAndView("/book/list", [ bookList : favoriteBooks ])
}
{code}
h4. Selecting the View
h4.选择视图( View)
In both the previous two example there was no code that specified which [view:guide:GSP] to render. So how does Grails know which view to pick? The answer lies in the conventions. For the action:
在先前的两个示例里没有代码指定哪个 [view:guide:GSP]去渲染。那么Grails怎么知道哪个视图( View)被选取了?答案在于规约。观察这个Actions(操作):
{code:java}
class BookController {
def show = {
[ book : Book.get( params.id ) ]
}
}
{code}
Grails will automatically look for a view at the location @grails-app/views/book/show.gsp@ (actually Grails will try to look for a JSP first, as Grails can equally be used with JSP).
Grails 将自动在 @grails-app/views/book/show.gsp@ 位置寻找一个视图( View)(事实上 Grails将首先尝试寻找JSP页面, 因为Grails 可以等同的用于JSP).
If you wish to render another view, then the [render|controllers] method there to help:
假如你希望渲染另一个视图( View),那么[render|controllers]方法可以帮助你:
{code:java}
def show = {
def map = [ book : Book.get( params.id ) ]
render(view:"display", model:map)
}
{code}
In this case Grails will attempt to render a view at the location @grails-app/views/book/display.gsp@. Notice that Grails automatically qualifies the view location with the @book@ folder of the @grails-app/views@ directory. This is convenient, but if you have some shared views you need to access instead use:
在这种情况下Grails将尝试渲染 @grails-app/views/book/display.gsp@位置上的视图( View)。注意,Grails自动描述位于@book@文件夹中的 @grails-app/views@ 路径位置的视图( View)。很便利,但是假如你拥有一些共享的视图( View)用来存取,作为替代使用:
{code:java}
def show = {
def map = [ book : Book.get( params.id ) ]
render(view:"/shared/display", model:map)
}
{code}
In this case Grails will attempt to render a view at the location @grails-app/views/shared/display.gsp@.
在这种情况下Grails将尝试渲染@grails-app/views/shared/display.gsp@位置上的视图( View)。
Rendering a Response
渲染Response(响应)
Sometimes its easier (typically with Ajax applications) to render snippets of text or code to the response directly from the controller. For this the highly flexible "render" method can be used:
有时它很容易的渲染来自创建控制器(Controllers)小块文本或者代码的响应(通常使用Ajax应用程序)。因为使用了高度灵活的 "render"方法。
{code:java}
render "Hello World!"
{code}
The above code writes the text "Hello World!" to the response, other examples include:
上面的代码打印出"Hello World!" 响应,其他的示例包括:
{code:java}
// 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")
{code}
h4. 返回模型(Model)
A model is essentially a map that the view uses when rendering. The keys within that map translate to variable names accessible by the view. There are a couple of ways to return a model, the first way is to explicitly return a map instance:
模型(Model)本质上是个map类型,当视图(view)被渲染时使用。map中的keys转变成变量名让view(视图)访问。这里有一对方式来返回模型(Model),第一种方式是明确返回map实体:
{code:java}
def show = {
[ book : Book.get( params.id ) ]
}
{code}
If no explicit model is returned the controller's properties will be used as the model thus allowing you to write code like this:
假如没有明确的模型(Model)被返回,控制器的属性将被当做模型(Model)来使用,因此允许你的代码写成下面这样:
{code:java}
class BookController {
List books
List authors
def list = {
books = Book.list()
authors = Author.list()
}
}
{code}
{note}
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.
这可能是由于事实上控制器(Controllers)是prototype(原型)范围。换句话说,每次请求,一个新的控制器(Controllers)会被创建。否则上面的代码将不是线程安全的。
{note}
In the above example the @books@ and @authors@ properties will be available in the view.
在上面示例中的 @books@ 和 @authors@属性将在view(视图)中被使用。
A more advanced approach is to return an instance of the Spring [ModelAndView|api:org.springframework.web.servlet.ModelAndView] class:
一个更加高级的方法是返回Spring [ModelAndView|api:org.springframework.web.servlet.ModelAndView] 类。
{code:java}
import org.springframework.web.servlet.ModelAndView
...
def index = {
def favoriteBooks = ... // get some books just for the index page, perhaps your favorites
// forward to the list view to show them
return new ModelAndView("/book/list", [ bookList : favoriteBooks ])
}
{code}
h4. Selecting the View
h4.选择视图( View)
In both the previous two example there was no code that specified which [view:guide:GSP] to render. So how does Grails know which view to pick? The answer lies in the conventions. For the action:
在先前的两个示例里没有代码指定哪个 [view:guide:GSP]去渲染。那么Grails怎么知道哪个视图( View)被选取了?答案在于规约。观察这个Actions(操作):
{code:java}
class BookController {
def show = {
[ book : Book.get( params.id ) ]
}
}
{code}
Grails will automatically look for a view at the location @grails-app/views/book/show.gsp@ (actually Grails will try to look for a JSP first, as Grails can equally be used with JSP).
Grails 将自动在 @grails-app/views/book/show.gsp@ 位置寻找一个视图( View)(事实上 Grails将首先尝试寻找JSP页面, 因为Grails 可以等同的用于JSP).
If you wish to render another view, then the [render|controllers] method there to help:
假如你希望渲染另一个视图( View),那么[render|controllers]方法可以帮助你:
{code:java}
def show = {
def map = [ book : Book.get( params.id ) ]
render(view:"display", model:map)
}
{code}
In this case Grails will attempt to render a view at the location @grails-app/views/book/display.gsp@. Notice that Grails automatically qualifies the view location with the @book@ folder of the @grails-app/views@ directory. This is convenient, but if you have some shared views you need to access instead use:
在这种情况下Grails将尝试渲染 @grails-app/views/book/display.gsp@位置上的视图( View)。注意,Grails自动描述位于@book@文件夹中的 @grails-app/views@ 路径位置的视图( View)。很便利,但是假如你拥有一些共享的视图( View)用来存取,作为替代使用:
{code:java}
def show = {
def map = [ book : Book.get( params.id ) ]
render(view:"/shared/display", model:map)
}
{code}
In this case Grails will attempt to render a view at the location @grails-app/views/shared/display.gsp@.
在这种情况下Grails将尝试渲染@grails-app/views/shared/display.gsp@位置上的视图( View)。
Rendering a Response
渲染Response(响应)
Sometimes its easier (typically with Ajax applications) to render snippets of text or code to the response directly from the controller. For this the highly flexible "render" method can be used:
有时它很容易的渲染来自创建控制器(Controllers)小块文本或者代码的响应(通常使用Ajax应用程序)。因为使用了高度灵活的 "render"方法。
{code:java}
render "Hello World!"
{code}
The above code writes the text "Hello World!" to the response, other examples include:
上面的代码打印出"Hello World!" 响应,其他的示例包括:
{code:java}
// 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")
{code}
发表评论
-
grails1.2中文文档即将发布
2009-11-10 11:25 2012grails1.2中文文档即将发布 发布日期可能与官方同步, ... -
我的桌面很酷吧
2009-03-31 17:19 1803我的桌面很酷吧 哈哈!!! 晒晒我的系统 -
Grails1.1中文文档——2009.3.25版本发布
2009-03-25 19:50 1532Grails1.1中文文档——2009.3.25版本修正了网友 ... -
对于Groovy&&Grails的问题大家可以在此提问
2009-03-23 12:06 1139如果大家碰到什么关于Groovy&&Grail ... -
Grails1.1中文文档正式发布
2009-03-19 22:58 1489经过大家的努力 Grails1.1中文文档正式发布 ... -
Grails 1.1中文文档即将发布
2009-03-11 16:50 1724Grails 1.1中文文档即将发布 SVN:http:/ ... -
现在支持对Groovy的代码高亮(有需要的可以找我)
2009-03-05 11:59 1412有需要的可以找我 -
groovy闭包 (PK) Java IOC
2008-10-17 15:03 2413N久没写东西,今天突发想法,写下来,废话少说,干活,先看几段代 ... -
grails and tinymce
2008-05-30 16:44 2270最近弄了个grails tinymce下的代码高亮插件: 界面 ... -
Grails中文文档beta发布
2008-05-08 21:32 2290经过我们大家的努力Grails中文文档终于面试了 下载抵制 b ... -
关于使用IDEA开发Grails项目乱码的问题
2008-04-10 02:40 3829关于Grails乱码的问题 个人测试了以后 还是属于编辑器的问 ... -
6.1.2 Controllers and Scopes
2008-03-18 23:39 1568h4. Available Scopesh4.可用范围(Sco ... -
6.1.1 Understanding Controllers and Actions
2008-03-18 23:16 1315h4. Creating a controllerh4. 创建 ... -
6.1 Controllers
2008-03-18 22:52 1269A controller handles requests a ... -
Groovy(Java笨狗)系列-Working with closures(1)
2008-03-17 22:28 1975翻译自《Groovy in action》 ... -
6.1.7 XML and JSON Responses
2008-03-09 15:19 3627h4. Using the render method to ... -
Grails(Java笨狗)系列-更好的理解闭包(closure)
2008-01-20 15:39 1916用现实生活中的示例来说明闭包 现在,让我们来考虑放在信封里的信 ... -
可能Grails的翻译不会在发布到BLOG中来了
2008-01-18 17:03 3263很荣幸能参加满江红的Grails的翻译 所以 Grails的翻 ... -
Grails(Java笨狗)系列--Configuration
2008-01-18 12:42 5171首先,Grails是一个信奉“规约重于配置”的框架,这一点是来 ... -
Groovy(Java笨狗)系列--fields and local variables
2008-01-14 16:28 4862使用最简单的术语,a variable(变量): 变量名引用的 ...
相关推荐
Vue devtools插件 6.1.3
6.1.3固件SHSH
苹果6.1.3SHSH文件
在标题"dnSpy6.1.3_DECOMPILE_dnspy_dnSpy6.1.3_VB_"中,我们可以看到关键词"DECOMPILE",表明这款工具的核心功能是反编译,"dnspy"是工具的名称,而"dnSpy6.1.3"则是其特定版本号,"VB"则提示我们该工具支持Visual ...
标题中的“iPhone 3.1 iOS 6.1.3”指的是苹果公司的iPhone操作系统的一个特定版本。iPhone 3.1是iOS系统的一个早期版本,它主要用于iPhone 3G和iPhone 3GS型号。而iOS 6.1.3是iOS 6系列的一个更新,这个更新在iOS 6...
6.1.3 shsh
iPhone4 6.1.3shsh
SecureCRT 6.1.3使用说明 官方手册,详细描述SecureCRT 6.1.3,建议大家认真学习,会有新收获!!!
iphone 4s 的shsh文件,可以更改后将iOS9.3.5的iphone4s降级到iOS6.1.3
【secureFX 6.1.3】是一款专业的FTP(文件传输协议)客户端软件,由Vandyke Software公司开发,主要用于安全、可靠地进行文件传输。它支持多种协议,包括FTP、SFTP(SSH文件传输协议)和FTPS(FTP over SSL/TLS),...
4s 6.1.3shsh
标题中的“IOS6.1.3降级.rar”指的是一个针对苹果iOS设备的降级操作,特别是将系统版本从更新的版本回降到iOS 6.1.3的过程。这一过程通常发生在用户对新版本不满意或者想要体验旧版系统特性时。iOS 6.1.3是苹果在...
ipad2 ios6.1.3(23K)不完美shsh
shsh 6.1.3 3.1
《iOS 6.1.3越狱工具:深入解析与应用指南》 在移动设备的世界里,越狱一词对于许多iOS用户来说并不陌生。它指的是通过特定的工具和技术手段,解除苹果公司对iOS设备的封闭式管理,使得用户可以自定义系统、安装未...
《CCS 6.1.3安装指南及深入理解》 CCS,全称为Code Composer Studio,是由Texas Instruments(TI)公司开发的一款集成开发环境,主要用于编写、调试和优化基于TI微控制器和数字信号处理器(DSP)的应用程序。在本文...
iphone4s 6.1.3 shsh文件
ios6.1.3 shsh
LED-ECS编辑控制系统6.1.3是一款专为LED显示屏幕设计的专业控制软件,由官方提供的安装版本。这款系统能够广泛应用于多种类型的控制卡,确保用户可以高效、精确地管理和编辑LED显示屏的内容。 首先,LED-ECS 6.1.3...
ios6.1.3