grails范围问题 - 页面,请求,flash [英] grails scope questions - page, request, flash

查看:85
本文介绍了grails范围问题 - 页面,请求,flash的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

grails手册显示了以下示例:

 < g:set var =nowvalue =$ { new Date()}scope =request/> 

,同时也表示默认由集合定义的变量是页面范围(在页面之外,请求,闪光灯,会话和应用程序选项)。我想知道页面和请求范围之间的区别是什么,以及这种区别的一个示例用法。

另外,对于flash作用域,手动指示:Grails支持flash范围的概念作为属性的临时存储,这些属性只需要这个请求和下一个请求,然后清除属性,这对于在重定向之前直接设置消息很有用。



我不明白重定向是如何与请求和下一个请求相关的,因为他们给出的重定向的例子是从一个控制器动作重定向到另一个控制器动作在发送给客户端的两页/ http响应中没有响应?



希望这两个问题有意义 - 即页面和请求范围之间的高级别差异,以及如何在动作之间重定向对flash范围有用?

解决方案

重定向troller:foo,action:bar)等于一个新请求(至少在servlet的上下文中)。这就是为什么你需要将Flash作为'两个请求范围'的一种,你发送的动作将你的重定向作为一个新的请求。你可以通过使用chain()来明确地避免这种情况。



至于页面和请求范围之间的区别,我的理解是页面范围或多或少是模型给定的视图/渲染过程操作,而请求是针对整个请求周期。也就是说,无论你在动作返回中传递给视图(或者你放入 model:[] 中的 render()

至于手动例子,我不知道他们为什么会在视图中显示任何范围:g:设置操作,通常应该避免在视图中设置变量(分离关注点和爵士乐的所有内容)。

The grails manual shows the following example:

<g:set var="now" value="${new Date()}" scope="request" />

and also indicates by default variables defined by the set are page scope (out of the page, request, flash, session, and application choices). I'm wondering what the difference between page and request scope is, and what an example use of the difference might be.

Also, with the flash scope, the manual indicates: "Grails supports the concept of flash scope as a temporary store for attributes which need to be available for this request and the next request only. Afterwards the attributes are cleared. This is useful for setting a message directly before redirection."

It isn't immediately apparent to me how redirection relates to "this request and the next request", since the example of redirection they give is redirecting from one controller action to another, which doesn't respond in two pages/http responses being sent to the client?

Hopefully those two questions make sense -- i.e. high level difference between page and request scope, and how redirecting between actions is useful for flash scope?

解决方案

A redirect(controller: "foo", action:"bar") equals a new request (in the context of a servlet at least). Which is why you need flash to be a sort of 'two requests scope', the action you get sent to treats your redirection as a new request. You can explicitly avoid this by using chain().

As for the difference between the page and request scope, my understanding is that the page scope is more or less the model a given view / render process operates on whereas the request is for the entire request cycle. Meaning that whatever you pass off to the view in an action return (or the stuff you put in model: [] of a render()) is the 'page scope'.

As for the manual example I have no clue why they would show any scoping at all in a view g:set operation, setting variables in the view should generally be avoided anyways (separation of concerns and all that jazz).

这篇关于grails范围问题 - 页面,请求,flash的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆