Is flashmessage the appropiate way to get messages from controller to view?

Tag: scala , playframework-2.0 , flash-scope Author: wks1112 Date: 2013-09-25

Suppose a scala play view is supposed to render a range of items if available. If not we want to have a message output instead. It has been suggested to me to get the message from controller to view via "flashmessage" instead of a parameter of the view.

Is using flashmessage the appropriate way to get text from controller to view?

Iam having doubts because flashmessage is primarily used to get data across redirects. Also its subject to race conditions if another page is loaded at the same time and values are overwritten.

Best Answer

You are right. A flash message is not supposed to get data from the controller to view.

Suppose you have the following code:

Ok(something.render()).flashing("myitem" -> "item1")

And your view has something like this:

flash.get("myitem").getOrElse("")

You will get an empty value. Instead you would be forced to use Redirects, so I think this is a bad idea

comments:

Thanks for the clarification. So the value set via flashing will not be available in the view immediately but only on the next request?
Right. And this value will then be available until you redirect somewhere
That concludes my question. Can you quickly comment on what a viable alternative is? Maybe Http.Context or ist defining a parameter for every string to display in the view usually the best way?