`
sillycat
  • 浏览: 2552666 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Grails(6)Guide Book Chapter 7 The Web Layer

 
阅读更多
Grails(6)Guide Book Chapter 7 The Web Layer
7 The Web Layer
7.1 Controllers
A controller can generate the response directly or delegate to a view.
To create a controller, simply create a class whose name ends with Controller in the grails-app/controllers directory.

The default URL Mapping configuration ensures that the first part of your controller name is mapped to a URI and each action defined within your controller maps to the URIs within the controller name URI.

7.1.1 Understanding Controllers and Actions
Creating a controller
There are 2 commands create-controller and generate-controller
>grails create-controller book
To create a file /grails-app/controllers/myapp/BookController.groovy

Creating Actions
A controller can have multiple public action methods, each one maps to a URI
class BookController {
     def list(){
          //do controller logic, get a model
          return model
     }
}
This method will map to the URI /book/list

Public Methods as Actions

The Default Action
The default action of the Controller, for example, if /book is hit, the action that is called when the default URI is requested is dictated by the following rules.
1. If there is only one action, it is the default
2. If you have an action named index, it is the default
3. Alternatively you can set it explicitly with the defaultAction property.
static defaultAction = "list"

7.1.2 Controllers and Scopes
Available Scopes
Scopes are hash-like objects where you can store variables.

servletContext - application scope, instance of SevletContent

session - an instance of HttpSession

request - an instance of HttpServletRequest

params - mutable map of incoming request query string or POST parameters

flash -

Accessing Scopes
class BookController {
     def find(){
          def findBy = params["findBy"]
          def appContext = request["foo"]
          def loggedUser = session["logged_user"]
     }
}

Or we can visit the parameters like this
def find(){
     def findBy = params.findBy
     def appContext = request.foo
     def loggedUser = session.logged_user
}

Using Flash Scope
Flash scope as a temporary store to make attributes available for this request and the next request only. Afterwards the attributes are cleared.

This is really useful for setting a message directly before redirecting.

def delete(){
     def b = Book.get(params.id)
     if(!b){
          flash.message = "User not found for id ${params.id}"
          redirect(action:list)
     }    
     …snip...
}

Scoped Controllers
There are several scopes supported by grails:
prototype (default) - A new controller will be created for each request.
session - One controller is created for the scope of a user session
singleton - Only one instance of the controller ever exists

To configure that in controller
static scope = "singleton"

To configure that for all the controllers in Config.groovy
grails.controllers.defaultScope = "singleton"

7.1.3 Models and Views
Returning the Model
A model is a Map that the view uses when rendering.

def show(){
     [book: Book.get(params.id)]
}

class BookController {
     List books
     List authors
     def list(){
          books = Book.list()
          authors = Author.list()
     }
}

These are for default settings, we can use ModelAndView directly.

def index(){
     def favoriteBooks = …
     return new ModelAndView("/book/list", [bookList: favoriteBooks])
}

One thing to bear in mind is that certain variable names can not be used
attributes, application

Selecting the View
For the show Action,
class BookController {
     def show(){
          [book: Book.get(params.id)]
     }
}
Grails will look for a view at the location grails-app/views/book/show.gsp

If we want to control that, we need to render to a different view, we use render method:
def show(){
     def map = [book: Book.get(params.id)]
     render(view: "display", model:map) //grails-app/views/book/display.gsp
}

def show(){
     def map = [book: Book.get(params.id)]
     render(view: "/shared/display", model:map) // grails-app/views/shared/display.gsp
}

Rendering a Response
Render snippets of text or codes to the response directly from the controller.

render "Hello Sillycat!"

render(text:"<xml>Some Xml</xml>", contentType: "text/xml", encoding: "UTF-8")

References:
http://grails.org/doc/latest/guide/index.html
http://grails.org/doc/latest/guide/theWebLayer.html



分享到:
评论

相关推荐

    the definitive guide to grails 2

    Grails框架基于Groovy语言,是一种高度动态、敏捷的Java应用开发框架,它简化了Web应用程序的构建过程,同时保持了Java平台的强大功能和稳定性。 ### Grails框架简介 Grails框架是建立在Groovy编程语言之上的一个...

    Grails Cometed. Bin 1 The best web push

    Grails Cometed. Bin 1 The best web push

    Grails Cometed. Bin 3 The best web push

    Grails Cometed. Bin 3 The best web push

    Grails Cometed. Bin 2 The best web push

    Grails Cometed. Bin 1 The best web push

    The definitive guide to grails 2 英文版 书 代码

    《The Definitive Guide to Grails 2》是Grails框架深入学习的重要参考资料,由业界专家撰写,旨在为开发者提供全面、详尽的Grails 2技术指导。这本书结合了理论与实践,不仅介绍了Grails的基本概念,还涵盖了高级...

    grails快速开发web

    ### Grails 快速开发 Web 应用程序 #### 一、Grails 概述 Grails 是一种基于 Groovy 的开源应用框架,用于简化 Web 应用程序的开发过程。它采用约定优于配置的原则,这使得开发者可以更快地创建功能丰富的 Web ...

    使用Grails快速开发Web应用

    ### Grails快速开发Web应用:知识点详解 #### Grails框架概览 Grails是一个基于Groovy语言构建的开源MVC(Model-View-Controller)Web开发框架,以其高效的开发速度和简洁的代码著称。其核心优势在于: 1. **快速...

    The definate guide to Grails

    Grails 采用了约定优于配置的原则,简化了 Web 应用程序的开发过程,使得开发者能够快速构建出功能完备且易于维护的 Web 应用。 ### Groovy 语言 Groovy 是一种运行于 Java 平台上的动态编程语言,它融合了 Python...

    The definitive guide to grails_2 源代码

    《The Definitive Guide to Grails 2》是关于Grails框架的一本权威指南,它为读者提供了深入理解和使用Grails 2开发Web应用程序所需的知识。Grails是一种基于Groovy语言的开源全栈式Web应用框架,它借鉴了Ruby on ...

    Grails Cometed. The best web push

    **Grails CometD:最佳Web推送技术** 在现代Web开发中,实时通信是不可或缺的一部分,它使得用户可以即时获取服务器端的数据更新,无需频繁刷新页面。Grails CometD框架就是为了实现这种实时交互而设计的。本文将...

    The definitive Guide To Grails学习笔记

    《The definitive Guide To Grails学习笔记》是一份深入探讨Grails框架的重要资源,它源于经典书籍《The Definitive Guide to Grails》的精华总结。Grails是一种基于Groovy语言的开源Web应用框架,旨在提高开发效率...

    Grails企业web应用开发与部署

    《Grails企业Web应用开发与部署》 在现代软件开发领域,Grails作为一个基于Groovy语言的开源Web应用框架,以其高效、灵活和强大的特性深受开发者喜爱。它提供了丰富的插件系统,使得企业级Web应用的开发变得快速而...

    Grails技术精解与web开发实践2-10章

    《Grails技术精解与Web开发实践2-10章》是针对Grails框架的一份珍贵资源,适合初学者及有经验的开发者深入理解并掌握Grails技术。这本书的章节涵盖了从基础到进阶的多个方面,旨在帮助读者全面了解和运用Grails进行...

    The Definitive Guide to Grails 2nd Edition

    The Definitive Guide to Grails 2nd Edition.pdf

    Grails技术精解与web开发实践11-20章

    《Grails技术精解与Web开发实践11-20章》是一本专注于Grails框架的深度解析书籍,尤其适合初学者和希望提升Grails开发技能的IT从业者。Grails是一种基于Groovy语言的开源Web应用框架,它以其高效、灵活和强大的特性...

    Grails技术精解与Web开发实践.part2

    自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。 我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。

    Grails技术精解与Web开发实践.part1

    自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。...我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。

    Grails+快速开发+Web+应用程序.pdf

    ### Grails快速开发Web应用程序知识点解析 #### 一、Grails框架概述 - **定义**:Grails是一个基于Groovy语言构建的开源Model-View-Controller (MVC) Web开发框架。它旨在简化Web应用程序的开发流程,提高开发效率...

Global site tag (gtag.js) - Google Analytics