Java Session Flash - Play Framework 2.6.2

Viewed 45

I am currently having an issue getting a flash value from a Java controller to a Scala view.

As a simplification of the code base and an example of the controller:

public Result viewAccount(final String accountNo)
  {
    return ok(
        ViewAccount.render(
            accountNo));
  }

public Result adjustment(final String accountNo)
  {

    try
    {
      // do things
      flash("success", "Successfully updated account.");
    }
    catch (NumberFormatException e)
    {
      flash("error", "Could not update the account");
    }

    return redirect(routes.AccountsController.viewAccount(accountNo));
  }

I then try to use flash.containsKey("success") within the view.

@if(flash.containsKey("success")) {
    <div class="alert alert-success" role="alert">
      <b>Success:</b> @flash.get("success")
      <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
    </div>
 }

I have attempted various other ways of doing it with no success:

@if(flash.get("success") != null) {
     <div class="alert alert-success" role="alert">
       <b>Success:</b> @flash.get("success")
       <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
     </div>
  }

From what I have found online, I potentially need to return the request param inside the viewAccount method. Then add @()(implicit request: Http.Request) (not really sure how this is added).

After that I can call

@if(request.flash.get("success") != null) {
     <div class="alert alert-success" role="alert">
       <b>Success:</b> @flash.get("success")
       <button type="button" class="close" data-dismiss="alert"><span>&times;</span></button>
     </div>
  }
0 Answers
Related