- 浏览: 2552666 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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
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
发表评论
-
NodeJS12 and Zlib
2020-04-01 07:44 476NodeJS12 and Zlib It works as ... -
Traefik 2020(1)Introduction and Installation
2020-03-29 13:52 337Traefik 2020(1)Introduction and ... -
Private Registry 2020(1)No auth in registry Nginx AUTH for UI
2020-03-18 00:56 436Private Registry 2020(1)No auth ... -
Buffer in NodeJS 12 and NodeJS 8
2020-02-25 06:43 385Buffer in NodeJS 12 and NodeJS ... -
NodeJS ENV Similar to JENV and PyENV
2020-02-25 05:14 479NodeJS ENV Similar to JENV and ... -
Prometheus HA 2020(3)AlertManager Cluster
2020-02-24 01:47 424Prometheus HA 2020(3)AlertManag ... -
Serverless with NodeJS and TencentCloud 2020(5)CRON and Settings
2020-02-24 01:46 337Serverless with NodeJS and Tenc ... -
GraphQL 2019(3)Connect to MySQL
2020-02-24 01:48 248GraphQL 2019(3)Connect to MySQL ... -
GraphQL 2019(2)GraphQL and Deploy to Tencent Cloud
2020-02-24 01:48 452GraphQL 2019(2)GraphQL and Depl ... -
GraphQL 2019(1)Apollo Basic
2020-02-19 01:36 328GraphQL 2019(1)Apollo Basic Cl ... -
Serverless with NodeJS and TencentCloud 2020(4)Multiple Handlers and Running wit
2020-02-19 01:19 314Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(3)Build Tree and Traverse Tree
2020-02-19 01:19 319Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(2)Trigger SCF in SCF
2020-02-19 01:18 294Serverless with NodeJS and Tenc ... -
Serverless with NodeJS and TencentCloud 2020(1)Running with Component
2020-02-19 01:17 312Serverless with NodeJS and Tenc ... -
NodeJS MySQL Library and npmjs
2020-02-07 06:21 288NodeJS MySQL Library and npmjs ... -
Python Library 2019(1)requests and aiohttp
2019-12-18 01:12 261Python Library 2019(1)requests ... -
NodeJS Installation 2019
2019-10-20 02:57 574NodeJS Installation 2019 Insta ... -
Monitor Tool 2019(2)Monit on Multiple Instances and Email Alerts
2019-10-18 10:57 266Monitor Tool 2019(2)Monit on Mu ... -
Sqlite Database 2019(1)Sqlite3 Installation and Docker phpsqliteadmin
2019-09-05 11:24 368Sqlite Database 2019(1)Sqlite3 ... -
Supervisor 2019(2)Ubuntu and Multiple Services
2019-08-19 10:53 371Supervisor 2019(2)Ubuntu and Mu ...
相关推荐
Grails框架基于Groovy语言,是一种高度动态、敏捷的Java应用开发框架,它简化了Web应用程序的构建过程,同时保持了Java平台的强大功能和稳定性。 ### Grails框架简介 Grails框架是建立在Groovy编程语言之上的一个...
Grails Cometed. Bin 1 The best web push
Grails Cometed. Bin 3 The best web push
Grails Cometed. Bin 1 The best web push
《The Definitive Guide to Grails 2》是Grails框架深入学习的重要参考资料,由业界专家撰写,旨在为开发者提供全面、详尽的Grails 2技术指导。这本书结合了理论与实践,不仅介绍了Grails的基本概念,还涵盖了高级...
### Grails 快速开发 Web 应用程序 #### 一、Grails 概述 Grails 是一种基于 Groovy 的开源应用框架,用于简化 Web 应用程序的开发过程。它采用约定优于配置的原则,这使得开发者可以更快地创建功能丰富的 Web ...
### Grails快速开发Web应用:知识点详解 #### Grails框架概览 Grails是一个基于Groovy语言构建的开源MVC(Model-View-Controller)Web开发框架,以其高效的开发速度和简洁的代码著称。其核心优势在于: 1. **快速...
Grails 采用了约定优于配置的原则,简化了 Web 应用程序的开发过程,使得开发者能够快速构建出功能完备且易于维护的 Web 应用。 ### Groovy 语言 Groovy 是一种运行于 Java 平台上的动态编程语言,它融合了 Python...
《The Definitive Guide to Grails 2》是关于Grails框架的一本权威指南,它为读者提供了深入理解和使用Grails 2开发Web应用程序所需的知识。Grails是一种基于Groovy语言的开源全栈式Web应用框架,它借鉴了Ruby on ...
**Grails CometD:最佳Web推送技术** 在现代Web开发中,实时通信是不可或缺的一部分,它使得用户可以即时获取服务器端的数据更新,无需频繁刷新页面。Grails CometD框架就是为了实现这种实时交互而设计的。本文将...
《The definitive Guide To Grails学习笔记》是一份深入探讨Grails框架的重要资源,它源于经典书籍《The Definitive Guide to Grails》的精华总结。Grails是一种基于Groovy语言的开源Web应用框架,旨在提高开发效率...
《Grails企业Web应用开发与部署》 在现代软件开发领域,Grails作为一个基于Groovy语言的开源Web应用框架,以其高效、灵活和强大的特性深受开发者喜爱。它提供了丰富的插件系统,使得企业级Web应用的开发变得快速而...
《Grails技术精解与Web开发实践2-10章》是针对Grails框架的一份珍贵资源,适合初学者及有经验的开发者深入理解并掌握Grails技术。这本书的章节涵盖了从基础到进阶的多个方面,旨在帮助读者全面了解和运用Grails进行...
The Definitive Guide to Grails 2nd Edition.pdf
《Grails技术精解与Web开发实践11-20章》是一本专注于Grails框架的深度解析书籍,尤其适合初学者和希望提升Grails开发技能的IT从业者。Grails是一种基于Groovy语言的开源Web应用框架,它以其高效、灵活和强大的特性...
自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。 我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。
自己买的书,然后用扫描机扫描的,整个文件太大了,不能一次性上传上来,所以拆成3个part。...我自己学grails很想看这本书,结果网上没有,就自己去买了,然后共享给需要的人。 如果有什么问题请联系我下架。
### Grails快速开发Web应用程序知识点解析 #### 一、Grails框架概述 - **定义**:Grails是一个基于Groovy语言构建的开源Model-View-Controller (MVC) Web开发框架。它旨在简化Web应用程序的开发流程,提高开发效率...