Grails: Reuse a predefined list

Tag: grails Author: tttfly Date: 2009-08-26

I am new to Grails and currently using Grails 1.1.1. I don't know how to pass a list from a control/action to a view and then pass the same list from that view to another action. The reason I do this is to reuse the predefined object (a "list" in this case). Here is my scenario:

I have a search view (search.gsp) that calls the "search" action, which queries a database, stores the results in a list (using Hibernate Criteria), and renders that list on a result view (results.gsp). On the result.gsp, I have the export bar (using the grails export plugin) to export that list to an EXCEL file. I am having trouble with passing that list to the result view so that I can pass it to the "export" action.

I really appreciate the help if someone can give an advice on this. Thanks.

Other Answer1

Since you want to pass this object across several requests, you'll have to store it in the user session, like this:

def search = {
        session["results"] = queryDatabase(params)
}

def export = {
        def results = session["results"]
}

Other Answer2

As Michael pointed out, your scenario involves different HTTP requests, so you can't just "pass" the results to the second action the same way you can pass a model from a controller action to a view. While Michael's suggestion (store the result in the user session) will certainly work, an alternative (and IMHO more common) approach would be to pass the same request parameters (eg. your search query) to the export action, and re-fetch the data from DB in your export action.

There are a few advantages with this stateless approach:

  1. It's more scalable, since you don't have to worry about session "pollution" or replicating the session in distributed environments
  2. You won't run into problems if the user issues more than one concurrent search requests (eg. in different browser tabs). This is a serious problem with the session approach.

comments:

It's only more scalable as long as your DB server can take the extra load...