开发者

Prevent Flash-Message showing up twice (on page with error and on next page)

开发者 https://www.devze.com 2022-12-29 23:59 出处:网络
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.

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?


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>


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.


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


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.


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).
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号