URL 与 Controller 的命名约定:
Controller 即 TeamController.groovy 文件,可以看到如下内容:
class TeamController {
def index = { redirect(action:list,params:params) }
// the delete, save and update actions only accept POST requests
def allowedMethods = [delete:'POST', save:'POST', update:'POST']
def list = {
if(!params.max) params.max = 10
[ teamList: Team.list( params ) ]
}
def show = {
[ team : Team.get( params.id ) ]
}
…
其中 def xxx = {xxx} 的结构在 Groovy 语言中叫闭包(Closure),在 Controller 中,每个闭包对应为一个 Action,即处理一个特定的 Web 请求。然后看看 Grails URL 的命名原则:
http(s)://host:pot/ProjectName/ControllerName/ActionName/Parameters
如果请求的 URL 是 http://localhost:8080/Contact/team/list,即调用了 team 控制器的 list Action,对应将会触发 TeamController 的 list 闭包的执行。每个 Action 被执行完毕后,会默认跳转去执行它在 View 中同名的 GSP 页面。并把 Action 返回的数据传递给 GSP。比如这个例子中,list Action 执行完毕后,会跳转到 grails-app\views\team\list.gsp 视图,并把 Team.list(params) 的结果传递给 list.gsp,在 list.gsp 中可以通过 teamList 访问到 Controller 传递过来的数据。
Controller 即 TeamController.groovy 文件,可以看到如下内容:
class TeamController {
def index = { redirect(action:list,params:params) }
// the delete, save and update actions only accept POST requests
def allowedMethods = [delete:'POST', save:'POST', update:'POST']
def list = {
if(!params.max) params.max = 10
[ teamList: Team.list( params ) ]
}
def show = {
[ team : Team.get( params.id ) ]
}
…
其中 def xxx = {xxx} 的结构在 Groovy 语言中叫闭包(Closure),在 Controller 中,每个闭包对应为一个 Action,即处理一个特定的 Web 请求。然后看看 Grails URL 的命名原则:
http(s)://host:pot/ProjectName/ControllerName/ActionName/Parameters
如果请求的 URL 是 http://localhost:8080/Contact/team/list,即调用了 team 控制器的 list Action,对应将会触发 TeamController 的 list 闭包的执行。每个 Action 被执行完毕后,会默认跳转去执行它在 View 中同名的 GSP 页面。并把 Action 返回的数据传递给 GSP。比如这个例子中,list Action 执行完毕后,会跳转到 grails-app\views\team\list.gsp 视图,并把 Team.list(params) 的结果传递给 list.gsp,在 list.gsp 中可以通过 teamList 访问到 Controller 传递过来的数据。