views:

2579

answers:

1

EDIT: based on feedback, erased original Q. completely and reposting in better language

I wish to access a request or params variable and pass it between the controller and the gsp. i understand that the params object contains everything that a querystring has.

All the examples I see are all Model driven. I have looked up docs online and I have two books - beginning-grails and definitive guide to grails, both have database driven examples on params. I want to understand how params can be set and accessed. All I read everywhere is that it is a map of request variables.

My scenario is as follows: I have a controller which sends a list (not from a database) to the GSP. I thought of passing a "params" variable between GSP and the controller.

To reiterate, the scenario I have is not a Model driven one. I am looking to iterate thru a list of items (with no database count known) and is driven by user click. I thought of implementing something like what twitter has "the-more-button-on-the-bottom". have a simple remotelink at the bottom of the page with a new page counter, which i access in the controller and pass to my service class for the new part of list.

controller code:

//access params from request
int pageInt =params["pagecount"] // *i always get null here*

callMyList(pagecount) //calls my service method to get next set of list for next page

GSP (not actual) code

 <%= params.get("pagecount") %>
 <%= nxtPage = pagecount++ %>
  ...
 <%params["myId"] = nxtPage%>


<g:remoteLink action="list" id="${nxtPage}">More</g:remoteLink>
+2  A: 

The params object is only useful for getting values from the query string of the current request. Setting a value in params in a GSP won't do anything. Params is a request scope object, with each new request it is an entirely new object. To pass a value from your GSP to your List controller, that value must be in the query string of the new request to the controller. In your case, it looks like you want to put it in your "More" link. The way your remoteLink tag is written, the value of nxtPage should be in params.id in your List controller, so you could access it that way. If you want to be in params.pagecount you would have to put it in the params attribute of you remoteLink tag. Something like this:

<g:remoteLink action="list" params="[pagecount: nxtPage]">More</g:remoteLink>
Ben Williams
Thanks Ben, trying this out, can you tell me if I can do the following ( I am getting error: Cannot cast object '2' with class 'java.lang.String' to class 'java.lang.Integer')this is what I have on my GSP int pageInt = params["pagecount"]
Mak
int pageInt = params["pagecount"]
Mak
Request parameters come out of the params object as String objects. If you want the integer value of the string, you'll have to parse it like so:int pageInt = Integer.parseInt(params.pagecount)
Ben Williams
thanks, Ben managed to work it out. Would you care to let me know if this is the way I should do some "pagination" for non-database lists? eg: some lists coming off the web or a third party app like facebook/twitter etc? ........Also how do you get better at these kind of logic? Please advice - is it natural to feel so dumb when starting?
Mak