Grails 控制器传递参数

发布于 2024-11-10 03:23:33 字数 744 浏览 6 评论 0原文

我的控制器如下:

def participated = {
  def temp = ConferenceUser.get(params.temp)

  def prizes = Prizes.findAllByConferenceUser(temp) // find all rooms where current computer is
  def subms = Submissions.findAllByConferenceUser(temp) // find all rooms where current computer is

  [temp: temp, priz: prizes, subm: subms]
}

但不知何故,当我成功更新会议值时,我想返回到初始页面(已参与),但我不知道如何传回 params.temp 。 (如果我做一个简单的重定向,因为控制器需要 params.temp,它会给我一个错误,因为我无法使用空对象作为参数搜索奖品。所以,想象我的更新控制器是以下:

def update = {
  def saveParamshere = params.temp
  ...
  ...
  (code here)
  ...
  ...

  redirect(action: "participated", params: [temp: saveParamshere])
}

此代码不起作用。如何成功返回主页并传入 params.temp

My controller is the folowing:

def participated = {
  def temp = ConferenceUser.get(params.temp)

  def prizes = Prizes.findAllByConferenceUser(temp) // find all rooms where current computer is
  def subms = Submissions.findAllByConferenceUser(temp) // find all rooms where current computer is

  [temp: temp, priz: prizes, subm: subms]
}

But somehow, when I successfully update a conference value, I wanna go back to the initial page (participated) but I don't know how to pass back the params.temp. (if I do a simple redirect, as the controller is expecting params.temp, it will give me an error because I cannot search prizes with a null object as parameter. So, imagine my update controller is the following:

def update = {
  def saveParamshere = params.temp
  ...
  ...
  (code here)
  ...
  ...

  redirect(action: "participated", params: [temp: saveParamshere])
}

This code isn't working. How can I successfully go back to my main page and pass in params.temp ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦行七里 2024-11-17 03:23:33

我认为问题可能是,您正在通过提交表单来调用 update 操作(我想)。也许您没有从该表单传递 temp 值?您可以通过将 temp 作为隐藏输入字段嵌入到表单中来实现此目的,或者通过表单标记上的 param 属性将其应用到 url。

使用隐藏字段可能是这样的(在您的视图文件中):

<g:form controller="somecontroller" action="update">
  (...)
  <g:hiddenField name="temp" value="${temp}" />
  (...)
</g:form>

使用 params 属性:

<g:form controller="somecontroller" action="update" params="[temp : temp]">
  (...)
</g:form>

我没有测试其中任何一个,因此可能会出现一些问题,特别是在第二种方法中。

I think the problem may be, that you are calling update action by submitting form (I suppose). Maybe you are not passing temp value from that form? You can do it by embedding temp as hidden input field into form, or apply it to url by param attribute on form tag.

Using hidden field it might be something like this (in your view file):

<g:form controller="somecontroller" action="update">
  (...)
  <g:hiddenField name="temp" value="${temp}" />
  (...)
</g:form>

Using params attribute:

<g:form controller="somecontroller" action="update" params="[temp : temp]">
  (...)
</g:form>

I didn't test any of these so there might be some issues, especially in the second approach.

美羊羊 2024-11-17 03:23:33

您可以将参数放入闪存范围内,该作用域适用于两个请求,或者将它们放入会话中并以这种方式检索它们。

以下是有关 flash 范围使用的 grails 文档的链接:

Grails - 控制器 - 控制器范围

You could put the params in the flash scope, which lives for two requests, or put them in the session and retrieve them that way.

Here is a link to the grails docs on usage of flash scope:

Grails - Controllers - Controller Scopes

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文