request variables in grails

我怕爱的太早我们不能终老 提交于 2019-12-13 01:54:02

问题


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>

回答1:


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>



回答2:


def urlString =  request.scheme + "://" + request.serverName + ":" 
+ request.serverPort + "/" + grailsApplication.metadata.'app.name' 
+ "/" + controllerName + "/" 


来源:https://stackoverflow.com/questions/985839/request-variables-in-grails

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!