如何在 Grails 中隐藏 URL 参数
当用户创建新的 Load
对象时,如果用户选中“Paid On Delivery”复选框,那么他们将在新的 Load
对象之后立即重定向到 Payment
控制器。 code>Load 已创建。创建新的 Load
所需的几个参数也用于创建新的 Payment
,因此我只需在重定向中传递参数,如下所示:
redirect(controller: "payment", action: "create", params: params)
这工作正常,但是它给了我一个真正令人讨厌的 URL,其中包含所有参数。 如何将我的参数传递给另一个控制器并防止它们出现在 URL 中?
更新:
我应该说,我很感谢大家对这样一个小问题的建议。即使有所有建议,最好的方法似乎仍然是我想要避免的方法,即在 redirect
调用中手动构建参数映射。这没什么大不了的,特别是因为只有几个参数,我只是不相信没有更干净、更自动化的方法来解决这个问题。
def loadInstance = new Load(params)
if (loadInstance.save(flush: true)) {
Account.get(params.account.id).balance -= new BigDecimal(params.transactionAmount)
flash.message = "${message(code: 'default.created.message', args: [message(code: 'load.label', default: 'Load'), loadInstance.id])}"
if(params.paidOnDelivery){
redirect(
controller: "payment",
action: "create",
//There has to be a better way than this. Just writing "params:params" results in the values being wrapped in double quotes once they get to the Payment controller. If it wasn't for that then "params:params" would work great and I would not of had to ask this question :)
params: [
"account.id":params.account.id,
"dateOfTransaction":params.dateOfTransaction,
"dateOfTransaction_year":params.dateOfTransaction_year,
"dateOfTransaction_month":params.dateOfTransaction_month,
"dateOfTransaction_day":params.dateOfTransaction_day,
"dateOfTransaction_hour":params.dateOfTransaction_hour,
"dateOfTransaction_minute":params.dateOfTransaction_minute,
"transactionAmount":params.transactionAmount
]
)
return
}
redirect(action: "show", id: loadInstance.id)
}
else {
render(view: "create", model: [loggedByUsers:loggedByUsers, accounts:accounts, cargoProviders:cargoProviders, deliveredByUsers:deliveredByUsers, loadInstance:loadInstance])
}
When a user is creating a new Load
object, if the user checks the "Paid On Delivery" check box then they will be redirected to the Payment
controller immediately after the new Load
has been created. Several of the parameters needed to create a new Load
are also used to create a new Payment
, so I just pass the parameters in the redirect like this:
redirect(controller: "payment", action: "create", params: params)
This works fine, but it gives me a real nasty URL with all the parameters in it. How can I pass my parameters to another controller and keep them from appearing in the URL?
UPDATE:
I should say that I appreciate everyone's suggestions for such a little problem. Even with all the suggestions, it still seems the best way of doing this is the way I wanted to avoid, building the parameter map manually in the redirect
call. It isn't that big of a deal, especially since there is only a few params, I just don't believe there is isn't a cleaner more automated way of fixing this.
def loadInstance = new Load(params)
if (loadInstance.save(flush: true)) {
Account.get(params.account.id).balance -= new BigDecimal(params.transactionAmount)
flash.message = "${message(code: 'default.created.message', args: [message(code: 'load.label', default: 'Load'), loadInstance.id])}"
if(params.paidOnDelivery){
redirect(
controller: "payment",
action: "create",
//There has to be a better way than this. Just writing "params:params" results in the values being wrapped in double quotes once they get to the Payment controller. If it wasn't for that then "params:params" would work great and I would not of had to ask this question :)
params: [
"account.id":params.account.id,
"dateOfTransaction":params.dateOfTransaction,
"dateOfTransaction_year":params.dateOfTransaction_year,
"dateOfTransaction_month":params.dateOfTransaction_month,
"dateOfTransaction_day":params.dateOfTransaction_day,
"dateOfTransaction_hour":params.dateOfTransaction_hour,
"dateOfTransaction_minute":params.dateOfTransaction_minute,
"transactionAmount":params.transactionAmount
]
)
return
}
redirect(action: "show", id: loadInstance.id)
}
else {
render(view: "create", model: [loggedByUsers:loggedByUsers, accounts:accounts, cargoProviders:cargoProviders, deliveredByUsers:deliveredByUsers, loadInstance:loadInstance])
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您可以执行服务器端转发而不是重定向。只需将:替换
为:
更新
要解决您在评论中描述的刷新问题,请使您转发的操作发送重定向(而不是呈现视图),例如
You could do a server-side forward instead of a redirect. Simply replace:
with:
Update
To fix the refresh problem you described in the comments, make the action that you forward to sends a redirect (instead of rendering a view), e.g.
或
or
flash 对象可用于实现此目的(尽管我将按原样保留实现)。
将所有参数复制到闪存。
在“创建”操作中使用 flash 映射而不是参数映射。
The flash object can be used to achieve this (although I would leave the implementation as is).
Copy all your params to flash.
Use the flash map instead of params map in the 'create' action.
我没有尝试过,但我认为它会解决你的问题。
i didn't try it but i think it will solve your problem.
这是另一个建议:-)
保持从加载到付款的重定向不变。如果您收到带有参数的调用,请检查付款操作,然后将其保存在会话或闪存中,并重定向到不带参数的相同操作。使用 putAll 从会话/闪存恢复参数并继续执行操作。
有点像付款中的 Post-Redirect-Get,但实际上是 GetWithParams-Redirect-Get。产生干净的 URL 并且没有刷新问题。
我在应用程序中使用它来获取干净的 URL 并保持应用程序不同部分的状态(但由于您不需要后者,因此您应该将其删除或将其保留在闪存中)。
Here's another suggestion :-)
Keep the redirect from Load to Payment as is. Check in Payment action if you receive a call with params, then save those in session or flash and redirect to same action without params. Restore params from session/flash using putAll and proceed with action.
Kind of like a Post-Redirect-Get in Payment, but actually a GetWithParams-Redirect-Get. Results in clean URL and no Refresh problems.
I use it in a app for clean URLs and for keeping states in different parts of the app (but since you don't need the latter you should remove it or keep it in flash).