1. 先补充一些第五章有关标签的:
GSP 模板:利用GSP template进行GSP的复用,并简化比较繁杂的页面处理。
g:render:在GSP中使用这个标签来引用GSP模板,相当于把GSP模板中的代码粘贴
到g:render标签的地方,对应的controller 需要提供GSP中使用的model数据。
测试定制的标签:扩展grails.test.TagLibUnitTestCase类,grails内置的tagLib
属性包含了所有的定制标签(如 tagLib.repeat() ) ,tagLib.out.toString()可
以得到模拟的页面输出。
2. 缺省的URLMapping:在UrlMappings类中,定义static mappings = {
"/$controller/$action?/id?",“500”(view:'/error') }
3. 增加静态文本:/myProject/$controller/$action?/id?
4. 去掉controller和action:上面的URL类似于这样:/artist/show/42,这很不
优雅,所以我们希望把controller和 action合并起来,变成一个更加直观易懂的
文字如:showArtist/42,grails为这种需求提供了用包含controller和 action赋
值的closure定制方法:
static mappings = {
"/showArtist/$id" {
controller='artist'
action='show'
}
}
或更简洁的方法调用方式:
static mappings = {
"/showArtist/$id"(controller: 'artist', action: 'show')
}
5. 有人会觉得showArtist/42中的42很不优雅,其实id=42对应的是著名歌星刘德
华,于是他打算把这个URL换成更直观的 /showArtist?artistName=liudehua,在
UrlMappings类里改成:
static mappings = { "/showArtist"(controller:'artist', action:'show') ... }
这样看上去好多了!artistName=LiuDeHua作为request参数传递给controller的
action,不属于 UrlMapping。这是可行的,但是grails达人还是觉得这个url的样
子很丑,而且如果参数很多,url会变得很长,这样是非常不优雅的。哪 grails是
怎么做的呢?可以这样:
static mappings = { "/showArtist/$artistName" (controller:'artist', action:'show') ...}
这样简单一改,最后生成的url会是这样的:/showArtist/liudehua,完美了!
当然,这么改了之后,controller也得改,不然怎么利用$artistName筛选出刘德
华的记录呢?所以,在artistController 的show这个action里需要这么写:
def show = { def artist= Artist.findByName(params.artistName) ... }
这样就对应来自url的参数进行了数据的筛选。
还有一个细节问题需要考虑,url里不是什么字符都可以放的,比如空格就不行,
如果刘德华在Artist类里表达的是"liu de hua",用URL encode可以把这个url表
达成 /showArtist/liu%20de%20hua,但是grails达人怎么可以弄出这么难看的东
西?达人做出来的url应该是: /showArtist/liu_de_hua,这样就美观了。其实,
这个问题很容易解决,就是把上面的那个def artist后面加点替换语句就可以了:
def artist= Artist.findByName(params.artistName.replaceAll('_',' '))
6. 增加更多的参数:只要mappings里定义的内容和controller里的action配合
好,在UrlMappings里可以添加任意多的参数。
7. 直接mapping到view:有些url不需要经过controller的action处理,比如”500
“,”/“,可以直接对应到一个view,例如:
”/“(view:'/home'),也可以指定到某个controller所拥有的view,如:“/find"
(view:'query', controller:'search')
8. URL Mappings的约束:mappings里都有一个constraints方法调用,具体条件是
一个closure作为该方法的参数(这个与 domain类里的constraints不同,虽然名
字一样,domain类里的constraints本身是一个闭包),可以用来对request提交的
url进行合法性验证,避免所有的url都要到action里才能发现错误,例如:
static mappings = {
"/showLog/$year/$month/$day/$module?" {
controller ='log'
action = 'display'
constraints {
year mathches: /[0-9]{4}/
month matches: /[0-9]{2}/
day matches: /[0-9]{2}/
}
}
}
9. 在mappings中使用通配符*:*可以在mappings中作为占位符替换任何动态内
容,例如:“/images/*.jpg" (controller:'image'),这样所有images目录下
的.jpg文件的请求都会被imageController处理,如果要处理所有子目录包含的内
容,则可以用两个*来表示。还可以在通配符之前放置一个变量来作为request参数
传递给controller,如“/images /$pathToFile**.jpg"(controller:'image'),会
把image下到相应.jpg文件的路径作为pathToFile 的值传递给imageController。
10. mapping到HTTP request方法:RESTful API需要把URL mapping根据request的
不同自动映射到不同的action,比如对同一个url,GET request对应show,DELETE
request对应delete,POST request对应save,等等,这在grails里也不是问题:
static mappings = {
"/artist/$artistName" {
controller = 'artist'
action = [GET: 'show', PUT: 'update', POST: 'save', DELETE: 'delete']
}
}
11. 反向URL Mapping:通过<g:link..../>生成的链接也需要保持优雅,因此
<g:link action='show'
controller='artist'
id="${artist.id}">${artist.name}
</g:link>
生成出来的链接<a href="/artist/show/42">liu de hua</a>也是grails达人所无
法接受的。既然咱们已经有了很优雅的正向mappings定义
"/showArtist/$artistName"(controller:'artist', action:'show')
就应该好好利用,g:link标签应该写成这样:
<g:link action='show'
controller='artist'
params="[artistName:${artist.name.replaceAll(' ','_')}" >
${artist.name}
</g:link>
12. 如果应用里的URLMapping很多,可以把它们拆分成多个class,各自有不同的
命名,只要都放在grails-app/conf下,名称最后都是 UrlMappings结尾就可以。
13. 测试URL Mappings:利用grails.test.GrailsUrlMappingsTestCase类,测试
正向的映射可以用 assertForwardUrlMapping()方法,反向的用
assertReverseUrlMapping(),或者用 assertUrlMapping()同时测试两个方向的映射。
14. 国际化:Java里的java.util.ResourceBundle类可以用于读取.properties中
的信息,grails也使用这个类进行国际化,并提供GSP标签message来根据不同条件
从messages.properties中提取相应的信息。URL Mappings也可以用于国际化,例
如"/store/$lang"(controller:'store'),其中的lang是一个request参数。
message里也可以使用占位符实现参数化的动态信息,在messages.proerties中的
{0},{1}就可以作为变量,在<g:message.../>中再使用args="xxx"的方式对变量进
行赋值,在validate()中对错误信息的渲染就采用了这种方式。例如:
<g:message code="gtunes.purchased.songs" args="[97]"/>
15. 使用messageSource:有时候message不是用于渲染GSP,而是用来发送邮件,
或者生成log,这时需要提取出message的内容进行处理,这时候message标签就用
不上了。该怎么办呢?当然还是要相信grails是无所不能的。grails提供了一个
bean叫 messageSource,它可以被注入到任何grails artifact,包括
controller,taglib,其他的bean等等。比如在一个controller里使用它,只需在
controller 里定义,就可以使用:
class StoreController {
def messageSource
def index = {
def msg = messageSource.getMessage('gtunes.my.music', null , locale.ITALIAN)
}
}
分享到:
相关推荐
The definitive guide to grails 2 英文版 书和随书代码, 学习grails必备
The definitive guide to Grails是一部Grails开发技术的经典书籍。这是网上关于该书的一些学习笔记,一共八篇,比较精炼,值得一读。
详细介绍grails框架的奥秘,英文版你值得拥有
The Definitive Guide to Grails 2nd Edition.pdf
The definitive guide to grails_2 随书 源代码
The Definitive Guide to the ARM® Cortex®-M0 and Cortex-M0+ Processors, Second Edition explains the architectures underneath ARM’s Cortex-M0 and Cortex-M0+ processors and their programming ...
The Definitive Guide to Java Swing Third Edition
The Definitive Guide to Django 2nd Edition 本书分为两部分 第一部分可以视为入门 第二部分深入 对使用Django系统及想要使用的很有帮助 英文
The Definitive Guide to Jython, written by the official Jython team leads, covers the latest Jython 2.5 (or 2.5.x) from the basics to the advanced features. This book begins with a brief introduction ...
The Definitive Guide to Spring Batch takes you from the “Hello, World!” of batch processing to complex scenarios demonstrating cloud native techniques for developing batch applications to be run on...
The Definitive Guide to NetBeans Platform 7
书由Apress出版社出版发行,“Definitive Guide”系列丛书之一。这本书是介绍MySQL 5的权威手册,初学者可以以此开始学习MySQL数据库技术。
The Definitive Guide to Windows Installer Introduction Chapter 1 - Installations Past, Present, and Future Chapter 2 - Building an Msi File: Visual Studio and Orca Chapter 3 - COM in the ...
The Definitive Guide to ARM Cortex-M3 and Cortex-M4 Processors-3rd Ed 纯文字版,带封面,带目录,纯正PDF
The most up-to-date comprehensive guide to use Grails for rapid web development. 最新最全版本使用Grails进行快速web应用程序开发。英文版。原汁原味,通俗易懂。由Grails项目组组长亲自编写,涉及Grails的每...
And because SQLite's databases are completely file based, privileges are granted at the operating system level, allowing for easy and fast user management., The Definitive Guide to SQLite is the ...
The Definitive Guide to Django - Web Development Done Right(2nd) 英文无水印pdf 第2版 pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 ...
该资料的全称是<<The Definitive Guide to Arm Cortex-M3 and Cortex-M4 Processors>>,全书1015页。详细描述了ARM CortexM3 和M4的架构以及内核,如NVIC,FPU等,是从事ARM Cortex架构嵌入式工程师的良好手册。
The Definitive Guide to ARM Cortex-M3 and Cortex-M4 Processors, 3rd Edition,英文,非扫描
The Definitive Guide to SQLite (Second Edition)