URL映射同样支持Grails统一 验证规约 机制, 它允许你更进一步"约束"一个URL是怎么被匹配的。
例如,如果我们回到早前的blog示例代码,这个映射当前看上去会像这样 :static mappings = {
"/$blog/$year?/$month?/$day?/$id?"(controller:"blog", action:"show")
}
允许URLs像这样:
/graemerocher/2007/01/10/my_funky_blog_entry
不过,它也允许这样:
/graemerocher/not_a_year/not_a_month/not_a_day/my_funky_blog_entry
当它强迫你在控制器代码中做一些聪明的语法分析时会有问题。
幸运的是,URL映射能进一步的约束验证URL标记:"/$blog/$year?/$month?/$day?/$id?" {
controller = "blog"
action = "show"
constraints {
year(matches:/d{4}/)
month(matches:/d{2}/)
day(matches:/d{2}/)
}
}
在这种情况下,约束能确保 year, month 和 day参数匹配一个具体有效的模式,从而在稍后来减轻你的负担 .