• 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 域类

  • 相关阅读:
    Linux常用命令整理
    Linux脚本无法进入目录
    mysql5.7.初始化后,临时密码过期
    通过scp 命令向远程Linux服务器传输文件
    Linux进入单用户模式修改root密码
    Kali Linux安装谷歌输入法
    kali 安装pip命令
    CentOS 7 防火墙设置
    CentOS7 安装python 3.7
    CentOS 7安装完成之后无法联网
  • 原文地址:https://www.cnblogs.com/liuyou/p/2230636.html
Copyright © 2020-2023  润新知