41580

grails scope questions - page, request, flash

Question:

The <a href="http://www.grails.org/doc/latest/guide/6.%20The%20Web%20Layer.html#6.2%20Groovy%20Server%20Pages" rel="nofollow">grails manual</a> 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?

Answer1:

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).

Recommend

  • Technical error of measurement in between two columns
  • JAXB unmarshal won't work with JDK 1.6
  • how to store and retrieve data in angular js?
  • Using C# LINQ to write the function with two dimensional List
  • Grails 2.1.1 - How to develop a plugin with an AstTransformer?
  • JSF: weird behavior when clear inputTextArea with ajax
  • DataGrid show an empty row when DataTable is empty
  • request follow redirection without post data
  • Office365 authentication without login redirection
  • Grails 3 - How to publish to Artifactory
  • Raise Session_OnStart event from custom ASP.NET SessionStateProvider class
  • How to only store 3 values for a key in a dictionary? Python
  • Execute JavaScript code once a browser window/tab is opened for the first time
  • Does SmartGit support git-svn?
  • CSS Grid, position absolute an element in a css grid item: IMPOSSIBLE
  • Javascript unload page condition
  • Asynchronous Sockets - Handling false socket.AcceptAsync values
  • custom string delimiters stringtemplate-4
  • Create registry key in 32-bit hive on x64 PC using Installshield 2012 LE - Avoid redirection
  • SIP API media codecs
  • How do I import an existing Grails 3 (3.0.12) project in IntelliJ 15
  • how to get data attributes of dynamically generated element
  • Getting unused unique values on a SQL table
  • ListItem.Attributes.Add not working
  • WPF - CanExecute dosn't fire when raising Commands from a UserControl
  • How to change the font size of a single index for UISegmentedControl?
  • How to define and use opencv mat of user type
  • NHibernate Validation Localization with S#arp Architecture
  • How do I change content of ComboFieldEditor?
  • How can I send an e-mail from a vbs script
  • Projection media query: browser support and workarounds?
  • Accessing IRQ description array within a module and displaying action names
  • Resize panoramic image to fixed size
  • 'TypeError' while using NSGA2 to solve Multi-objective prob. from pyopt-sparse in OpenMDAO
  • Weird JavaScript statement, what does it mean?
  • Do I've to free mysql result after storing it?
  • SQL merge duplicate rows and join values that are different
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Getting Messege Twice Using IMvxMessenger
  • Linking SubReports Without LinkChild/LinkMaster