Grails教程

 
该部分主要介绍了Grails的内部组织结构,工作流程,以及各个组件的语法特点.
 
2       Grails内部组织结构
3       Grails工作流程
1.       输入 http://127.0.0.1:8080/HelloWorld
2.       HelloWorldController 接受到这个请求后根据
def index = { redirect(action:list,params:params) }将请求转到了list action
3.       list action最后没有render元素,默认将请求定向到list.gsp这个view,同时携带参数helloList, helloList是一个HashMap类型.
4.       list.gsp接受helloList参数,并取出值
4.1    Controller
一个完整的controller如下:
 
class HelloController {
    def index = { redirect(action:list,params:params) }
      
    def list = {
        if(!params.max) params.max = 10
        [ helloList: Hello.list( params ) ]
    }
 
    def show = {
        [ hello : Hello.get( params.id ) ]
    }
 
    def delete = {
        def hello = Hello.get( params.id )
        if(hello) {
            hello.delete()
            flash.message = "Hello ${params.id} deleted."
            redirect(action:list)
        }
        else {
            flash.message = "Hello not found with id ${params.id}"
            redirect(action:list)
        }
    }
 
    def edit = {
        def hello = Hello.get( params.id )
 
        if(!hello) {
                flash.message = "Hello not found with id ${params.id}"
                redirect(action:list)
        }
        else {
            return [ hello : hello ]
        }
    }
 
    def update = {
        def hello = Hello.get( params.id )
        if(hello) {
             hello.properties = params
            if(hello.save()) {
                redirect(action:show,id:hello.id)
            }
            else {
                render(view:'edit',model:[hello:hello])
            }
        }
        else {
            flash.message = "Hello not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }
 
    def create = {
        def hello = new Hello()
        hello.properties = params
        return ['hello':hello]
    }
 
    def save = {
        def hello = new Hello()
        hello.properties = params
        if(hello.save()) {
            redirect(action:show,id:hello.id)
        }
        else {
            render(view:'create',model:[hello:hello])
        }
    }
private String getChina (){
       return “[Chinese]”
}
}
这个controller的action有create,update,list,show,delete,save,每一个action类似与一个c的函数,为了实现一个功能.
Controller在被调用时会根据” def index = { redirect(action:list,params:params) }”自动重定向到一个action.例如在IE里输入URL: http://127.0.0.1:8080/hello等同与 http://127.0.0.1:8080/hello/list
 
Redirect:重定向到一个action
Render:重定向到一个view
在action中间调用redirect或者render,程序还会继续往下执行,除非你在前加上return
4.1.3   Action里的方法
可以象java里一样调用getChina方法
参见<<Grails环境部署与开发>>
参见<<Grails环境部署与开发>>
 
 
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值