0

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.

Th 00 mÄ s
  • 3,776
  • 1
  • 27
  • 46

1 Answers1

1

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

serejja
  • 22,901
  • 6
  • 64
  • 72
  • Thanks for the clarification. So the value set via flashing will not be available in the view immediately but only on the next request? – Th 00 mÄ s Oct 11 '13 at 08:45
  • 1
    Right. And this value will then be available until you redirect somewhere – serejja Oct 11 '13 at 08:54
  • 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? – Th 00 mÄ s Oct 11 '13 at 14:48