• Grails


    Summary

    • 处理 controler 的一些规则。
    • 注意:如果配合 spring core 使用,某些路径记得权限控制。

    默认配置

    package cn.duchaoqun
    
    class UrlMappings {
        static mappings = {
            // 默认的
            // 直接访问 /controller 根据约定会调用 index action。
            // 直接方法 /controller/action1 根据约定会调用 action1。
            // 直接访问 /controller/action1/123 一般会取当前 controller 的对象作为参数,id就是123。
            // format 参数一般是在开发 API 的时候用,例如 .xml .json 的时候用。
            "/$controller/$action?/$id?(.$format)?"{
                constraints {
                    // apply constraints here
                }
            }
    
            "/"(controller:'login', action:'auth')     // 访问指定 controller 的指定 action
            "500"(view:'/error')
            "404"(view:'/notFound')
        }
    }
    

    自定义 controller 和 action

    package cn.duchaoqun
    class UrlMappings {
        static mappings = {
            // 根 url
            "/"(view:"/index")                       // 访问 /views/index
            "/"(controller:'login')                  // 访问指定 controller 的指定默认 action(index)
            "/"(controller:'login', action:'auth')   // 访问指定 controller 的指定 action
            "/"(controller:'login', view:'about')    // 访问指定 controller 下的指定 view 页面。
        }
    }
    

    传递 URL 参数

    package cn.duchaoqun
    class UrlMappings {
        static mappings = {
            // Grails会将URL中$name位置的内容当成参数放在params里面,然后再 index action 里面通过 params.name 获取该值。
            "/character/$name"(controller:"character")
        }
    }
    

    处理不同类型的 HTTP 请求

    package cn.duchaoqun
    class UrlMappings {
        static mappings = {
    
            // 处理不同类型的 HTTP 请求
            "/character/$name" {
                controller = "character"
                action = [GET:'getAction', POST:'postAction']
            }
    
            // 使用通配符
            // "/character/*.html"(controller:"character")
            // "/character/$name.html"(controller:"character")
        }
    }

    使用通配符

    package cn.duchaoqun
    class UrlMappings {
        static mappings = {
            // 使用通配符
            "/character/*.html"(controller:"character")
            "/character/$name.html"(controller:"character")
        }
    }

    Reference

    http://docs.grails.org/latest/guide/theWebLayer.html#applyingConstraints
    https://guides.grails.org/grails_url_mappings/guide/index.html

  • 相关阅读:
    第十三周助教作业
    【西北师大-19软工】第五次作业成绩汇总
    第十二周助教工作总结
    第十一周助教工作总结
    第九周助教工作总结
    第八周助教工作总结
    第七周助教工作总结
    SQLAlchemy 学习笔记(二):ORM 基础
    WebSocket 与 HTTP/2
    Chrome 与 Firefox-Dev 的 DevTools
  • 原文地址:https://www.cnblogs.com/duchaoqun/p/13151104.html
Copyright © 2020-2023  润新知