tags:

views:

183

answers:

5

Hi,

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.

+3  A: 

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"]
}
Michael Borgwardt
A: 

Hi Mike,

This works great and thanks for the help. The performance is excellent since I only have a few records to export. I did a research on how much data a session can store and looks like there is no limit as long as the sessionid length allows. Not sure what role the sessionId plays and how it works, but it's a different story. Hopefully this does not affect the performance when exporting large amount data (1000+ records, each with 30 columns) to a file. Any thoughts on this? Thanks again.

CD

User sessions are part of the Java Servlets specification - the data is kept on the server, and the session ID just serves as a key to find it in some map - its size is irrelevant. The data is usually kept in memory on the server, so if it's large and you have many concurrent users, you may run into problems. I think the spec also explicitly allows session data to be passivated (written to disk), but I don't know how well various servlet containers support this.
Michael Borgwardt
A: 

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.
Daniel Rinser
It's only more scalable as long as your DB server can take the extra load...
Michael Borgwardt
A: 

Daniel,

You advice is what I implemented initially, but a question came to my mind that why not reusing the list instead of running the same query again. The reason I chose reusing the object is to find out how to pass it to view and control and to gain further understanding about session. More likely I will back to the initial route to avoid unexpected issues. Before finding the pros & cons of each case, I need to get the project done first. I use Hibernate Criteria to get the results and now having a problem with getting the id and date from them, e.g, BookId and ReleaseDate. The Hibernate Criteria example from Grails website does not show me how to this and I have not found any solution yet. So far I can export all fields, excepts for id and date, to a file. Any help is greatly appreciated. Thank you all for the insight.

A: 

Hi,

I have a similar problem here. I have a page which accepts 3 parameters. i enter the parameters and the control goes to the list action. The list action has the code to query db and get the data on list.gsp page. Now when i hit "download to excel" on the same page, i want the same list action to get triggered and the same query to be run. However, when I click on "export to Excel", control goes to the list action, but the data that i had entered for my query to run is now null. The form elements have not been retained. Please help.

Megs