grails 中的请求变量
编辑:根据反馈,完全删除原始问题并以更好的语言重新发布
我希望访问请求或参数变量并将其在控制器和gsp之间传递。 我知道 params 对象包含查询字符串所具有的所有内容。
我看到的所有例子都是模型驱动的。 我在网上查找了文档,我有两本书 - 《grails 入门》和《grails 权威指南》,这两本书都有关于参数的数据库驱动示例。 我想了解如何设置和访问参数。 我到处读到的只是它是请求变量的映射。
我的场景如下: 我有一个控制器,它将列表(不是来自数据库)发送到 GSP。 我想在 GSP 和控制器之间传递一个“params”变量。
重申一下,我遇到的场景不是模型驱动的场景。 我希望迭代一个项目列表(没有已知的数据库计数)并由用户点击驱动。 我想实现类似 Twitter 上的“底部更多按钮”的功能。 页面底部有一个简单的远程链接,其中有一个新的页面计数器,我在控制器中访问该计数器并将其传递给我的服务类以获取列表的新部分。
控制器代码:
//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(非实际)代码
<%= params.get("pagecount") %>
<%= nxtPage = pagecount++ %>
...
<%params["myId"] = nxtPage%>
<g:remoteLink action="list" id="${nxtPage}">More</g:remoteLink>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
params 对象仅适用于从当前请求的查询字符串中获取值。 设置 GSP 中的 params 值不会执行任何操作。 Params 是一个请求范围对象,每个新请求都是一个全新的对象。 要将值从 GSP 传递到列表控制器,该值必须位于控制器的新请求的查询字符串中。 就您而言,您似乎想将其放入“更多”链接中。 按照编写 RemoteLink 标记的方式,nxtPage 的值应位于列表控制器的 params.id 中,以便您可以通过这种方式访问它。 如果您想位于 params.pagecount 中,则必须将其放入 RemoteLink 标记的 params 属性中。 像这样的事情:
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: