zoukankan      html  css  js  c++  java
  • Grails简单教程

    1 内容简介

    该部分主要介绍了Grails的内部组织结构,工作流程,以及各个组件的语法特点.

    2 Grails内部组织结构

    3 Grails工作流程

    参考4.1的controller原型

    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 Grails组件

    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]”

    }

    }

    4.1.1 Action

    这个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

    4.1.2 页面转换元素

    Redirect:重定向到一个action

    Render:重定向到一个view

    在action中间调用redirect或者render,程序还会继续往下执行,除非你在前加上return

    4.1.3 Action里的方法

    可以象java里一样调用getChina方法

    4.2 服务

     

    4.3 域类

  • 相关阅读:
    js-link下载文件
    sql-优化建议
    Studio-环境变量设置
    Studio
    Docker下安装ElasticSearch和Kibana
    sklearn 中的 r2_score
    R语言将所有列数据正交化/缩放
    R语言 random forests out-of-bag prediction
    R语言 coalesce 函数
    R语言 case_when 函数
  • 原文地址:https://www.cnblogs.com/liuyou/p/2230636.html
Copyright © 2011-2022 走看看