防止 Flash 消息出现两次(在有错误的页面上和在下一页上)

发布于 2024-09-01 01:52:19 字数 87 浏览 3 评论 0原文

如果我的 grails 应用程序的表单中存在错误,则会出现一条闪现消息。如果我然后转到另一个页面,(旧的)闪存消息会再次显示在新页面上。 我该如何防止这种情况?

If there is an error in a form my grails application this results in a flash message. If I then go to another page, the (old) flash message shows up again on the new page.
How do I prevent this?

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

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

发布评论

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

评论(5

游魂 2024-09-08 01:52:19

尝试使用 request.message,它的行为方式与 flash.message 相同,但只会停留足够长的时间以显示一次。

控制器:

request.message = message;

在 GPS 上,您可以像使用 flash.message 一样使用它:

<g:if test="${request.message }"><br>
  <div class="message">${request.message}</div> </g:if>

try using request.message, it act the same way as flash.message but will only stay on for on long enough to be displayed once.

controller:

request.message = message;

and on gps you use it like you wold with flash.message:

<g:if test="${request.message }"><br>
  <div class="message">${request.message}</div> </g:if>
金兰素衣 2024-09-08 01:52:19

我想说,如果消息仅用于请求,请使用 request.message。如果可能涉及重定向,请使用 flash,然后在 gsp 中显示后清除 flash 消息:

<div class="message">
    ${flash?.message}
</div>

<%
// Prevent flash messages displaying twice if there is no redirect
flash.message = null
%>

我会将所有这些内容放在用于显示消息的标准模板中。

I would say if the message is for request only, use request.message. If a redirect could be involved, use flash and then clear the flash message after displaying it in the gsp:

<div class="message">
    ${flash?.message}
</div>

<%
// Prevent flash messages displaying twice if there is no redirect
flash.message = null
%>

I would have all this in a standard template that you use for displaying messages.

-柠檬树下少年和吉他 2024-09-08 01:52:19

一种想法是在检测到错误时清除第一页上的闪存消息。

One thought would be to clear the flash message on the first page when an error is detected.

无风消散 2024-09-08 01:52:19

不确定 request.message 路由是否仍然是较新版本的 grails 中的一个选项,因为我尝试了这个,但它对我不起作用。

我发现避免两次显示消息的方法是使用 flash 中更具体的键设置消息,例如:

Controller:

flash.specificKeyForControllerAndAction = "Some message"

GSP:

<g:if test="${flash.specificKeyForControllerAndAction}">
  <div class="message">${flash.specificKeyForControllerAndAction}</div>
</g:if>

显然该键可以是您想要的任何内容,但请确保您的其他视图没有检查相同的键,否则该消息将再次显示。

Not sure if the request.message route is still an option in the newer version of grails as I tried this and it didn't work for me.

A method I found to avoid showing the message twice is to set a message using a more specific key in flash such as:

Controller:

flash.specificKeyForControllerAndAction = "Some message"

GSP:

<g:if test="${flash.specificKeyForControllerAndAction}">
  <div class="message">${flash.specificKeyForControllerAndAction}</div>
</g:if>

Obviously the key could be anything you would like, but make sure your other views aren't checking for the same key or else the message will display again.

菩提树下叶撕阳。 2024-09-08 01:52:19

引用自 Grails 文档
http://docs.grails.org/3.1.1/ref/控制器/flash.html

flash 对象是一个 Map(哈希),可用于存储键值对。这些值透明地存储在会话中,然后在下一个请求结束时清除。

此模式允许您使用 HTTP 重定向(这对于发布后的重定向很有用)并保留可从 Flash 对象检索的值。

您必须记住重定向的工作原理 http://grails.asia/grails-redirect-vs-forward< /a>

当你进行重定向时,会返回到客户端浏览器,浏览器会调用它收到的 url,那么这个调用就是添加 flash 消息后的“下一个”请求。

那么您应该在重定向之前添加一条提示消息。因为这个闪现消息在下一个请求结束时被清除。或者,如果您不使用重定向,而只是执行简单的转发(例如,gsp 的简单返回渲染),则在添加 flash 消息后不会有其他请求。因此,下一个请求将是当您访问另一个链接时。
只有在下一个请求结束时,然后在 gsp 渲染之后,grails 框架才会从会话中清除 Flash 消息。

结论:

  • 如果您想在重定向时显示消息,请使用 Flash 消息。它由框架自动清除。
  • 如果您想在转发时显示一条消息,您可以使用 fonger 所述的解决方案(在请求中添加一条消息)。

quote from grails documentation
http://docs.grails.org/3.1.1/ref/Controllers/flash.html

The flash object is a Map (a hash) which you can use to store key value pairs. These values are transparently stored inside the session and then cleared at the end of the next request.

This pattern lets you use HTTP redirects (which is useful for redirect after post) and retain values that can be retrieved from the flash object.

You must remember how works redirect http://grails.asia/grails-redirect-vs-forward

When you do a redirect, there is a go back to the client browser, and the browser call the url it received, then this call is the "next" request after adding flash message.

Then you should add a flash message before a redirect. Because this flash message is cleared at the end of the next request. Or if you do not use a redirect, and just do a simple forward (a simple return render of your gsp for example) there is no other request after adding the flash message. Thus the next request will be when you access another link.
It is only at the end of this next request, then after the gsp rendering, that the flash message will be cleared from session by grails framework.

To conclude :

  • if you want to display a message when you do a redirect use flash message. It's automatically cleared by the framework.
  • if you want to display a message when you do a forward you can use a solution as stated by fonger (add a message in the request).
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文