The Web Layer
一个控制器要"处理请求(handles request)","创建或准备响应(response)","判断请求的范围"。
从另外一个方面讲,每个响应都会创建一个新的控制器。一个控制器能够产生响应(response)或者代理生成一个页面(view)。
创建一个控制器很简单。他的名称以Controller 结尾,还有把他放在grails/controller的目录下。
理解Controllers和Actions
创建Controller
在项目的根目录使用create-controller [name]命令创建。
grails create-controller book
class BookController { … }
创建一个actions
一个控制器的属性每一个都是代码块。每个属性的名称映射一个uri。
class BookController {
def list = { // do controller logic
// create model return model
}
}
URI是/root/book/list
默认的Action
有几个规则:
当action中没有定义明确的uri,你定义一个index属性,所有的请求将由它处理。
要么你就明确定义defaultAction 属性
def defaultAction = "list"
控制器和范围(scopes)
*servletContext javax.servlet.ServletContext
*session HttpSession
*request HttpServletRequest
*params 从CGI引入的一个参数
*flash
访问这些变量
def find = {
def findBy = params["findBy"]
def appContext = request["foo"]
def loggedUser = session["logged_user"] }
class BookController {
def find = {
def findBy = params.findBy
def appContext = request.foo
def loggedUser = session.logged_user }
}
使用Flash scope
flash是一个临时的存储变量,仅仅在两个request之间传递。
Models和视图
模型本质就是一个map,在返回时候,给view层使用。
有以下几种方式放回一个map的实例
def show = {
[ book : Book.get( params.id ) ]
}
class BookController {
List books
List authors
def list = {
books = Book.list()
authors = Author.list()
}
}
上面这些代码是线程不安全的。
使用Spring ModeAndView
import org.springframework.web.servlet.ModelAndViewdef index = {
def favoriteBooks = … // get some books just for the index page, perhaps your favorites // forward to the list view to show them
return new ModelAndView("/book/list", [ bookList : favoriteBooks ])
}
选择视图:
class BookController {
def show = {
[ book : Book.get( params.id ) ]
}
}
上面的代码并没有明确指定返回的视图,Grails怎么知道的了?是因为grails使用了约定。
grails将会自动查找grails-app/views/book/show.gsp(而实际上它会先查找jsp,因为grails 能够使用jsp)
如果你喜欢指定返回的页面和模型(render):
def show = {
def map = [ book : Book.get( params.id ) ]
render(view:"display", model:map)
}
定位在grails-app/views/book/display.gsp
这里默认的会查找book/display.gsp
如果想让它返回其他页面,看下面代码
def show = {
def map = [ book : Book.get( params.id ) ]
render(view:"/shared/display", model:map)
}
定位在grails-app/views/shared/display.gsp
返回一个简单的response
render "Hello World!"
// write some markup
render {
for(b in books) {
div(id:b.id, b.title)
}
}
// render a specific view
render(view:'show')
// render a template for each item in a collection
render(template:'book_template', collection:Book.list())
// render some text with encoding and content type
render(text:"<xml>some xml</xml>",contentType:"text/xml",encoding:"UTF-8")
Redirects 和 chaining
class OverviewController {
def login = {} def find = {
if(!session.user)
redirect(action:login)
…
}
}
redirect使用的是HttpServeltResponse
在同一个控制类中:
redirect(action:login)
在不同的action中
redirect(controller:'home',action:'index')
URI
redirect(uri:"/login.html")
url
redirect(url:"http://grails.org")
分享到:
相关推荐
Then you'll learn how to build microservices in Python using REST API's in an event-driven approach and you will build the web layer. Next, you'll learn about interacting with data services and ...
2. Building the Web Layer with Spring Web MVC 3. Accessing Data with Spring 4. Understanding WebSocket 5. Securing Your Applications 6. Building a Single-Page Spring Application 7. Integrating with ...
Over 75% of network attacks are targeted at the web application layer. This book provides explicit hacks, tutorials, penetration tests, and step-by-step demonstrations for security professionals and ...
Groovy and Grails Recipes ... CHAPTER 11 The Web Layer CHAPTER 12 The Data Layer CHAPTER 13 Scaffolding CHAPTER 14 Security CHAPTER 15 Testing CHAPTER 16 Miscellaneous Recipes
基于两层HMM的Web流模型,谢逸,,本文提出一种新的两层HMM用于描述网络流的到达率过程。模型的宏状态过程用于描述网络流的大尺度趋势,子状态过程用于描述指定宏状
Learn tools and techniques for working with responsive web design, images, the network layer, and many other ingredients—plus the metrics to check your progress. Ideal for web developers and web ...
Chapter 7: Queries and the Database Layer Chapter 8: Function- and Class-Based Views Chapter 9: Best Practices for Function-Based Views Chapter 10: Best Practices for Class-Based Views Chapter 11: ...
The development of the Semantic Web proceeds in steps, each step building a layer on top of another. The pragmatic justification for this approach is that it is easier to achieve ( 71 ) on small ...
23. The five layers in the Internet protocol stack are – from top to bottom – the application layer, the transport layer, the network layer, the link layer, and the physical layer. The principal ...
After building the data application layer using Entity Framework Core and a RESTful service using ASP.NET Core, you will then build the client side web application three ways: first, using ASP.NET ...
from manage the life cycle in a proper way to use dynamic binding, fields' validations and custom service layer. You will integrate our application to Google OAuth Service and learn best practices ...
Progressive enhancement is an approach to web development that aims to deliver the best possible experience to the widest possible audience, and simplifies coding and testing as well. Whether users ...
作为web开发人员,我们的生活围绕着数据操作。我们建立数据库来存储数据,写编码来访问和修改数据,设计网页来采集和汇总数据。本文是研究在ASP.NET 2.0中实现这些常见的数据访问模式之技术的长篇系列教程的第一篇。...
08 Creating the Persistence Layer with TopLink 09 Refining the TopLink Model Objects 10 Implementing Business Tasks with Session EJBs 11 Binding Data in an Application 12 Using ADF Faces Components ...
Microsoft® .NET: Architecting Applications for the Enterprise Paperback: 304 pages Publisher: Microsoft Press (October 15, ...engineering and deliver more robust results Get code samples on the Web.
Flask integrates Werkzeug (a web programming toolkit) and Jinja (a templating engine) and adds a layer that lets you build web applications and services in a compact way, while letting you choose ...
After building the data application layer using Entity Framework Core and a RESTful service using ASP.NET Core, you will then build the client side web application three ways: first, using ASP.NET ...
Finally, you'll go on to learn how to obtain smooth transitioning from Bootstrap to Angular and then how to hook up with a server and use Firebase as the persistence layer. Once you're done with ...
After creating your own map, you will explore how to add geographic layers from a variety of sources including tiled and dynamic map...and stream geographic features to the browser using a FeatureLayer....
Review of ArcGIS Server and architecture ...Programming with the graphics layer Programming with resource specific APIs Programming with the ArcObjects API Developing custom attributes