Skip to content

mthmulders/spark-flash

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Flash support for Spark Java ⚡

Run tests SonarCloud quality gate SonarCloud vulnerability count SonarCloud technical debt Dependabot Status Mutation testing badge Maven Central

TL;DR

This module adds a "flash scope" to Spark Java.

Flash scope?

Imagine a server-rendered web application. When a user submits a form with some data - typically using HTTP POST - and hits the "refresh" button, the form shouldn't be submitted twice. The Post-Redirect-Get pattern makes this possible: it answers the POST with a redirect. The browser will follow that redirect, and perform a GET on the specified location.

The question is: how to convey information about the form processing to that new page? That's where the "flash scope" comes in. It allows the developer to temporarily store some information and retrieve it in the next request.

So when form processing is done, you store a bit of information in the flash scope. It can be a simple message, like "order placed" or "validation failed", or even be more complex. You redirect the user, and in the next page, you access the flash scope again to retrieve that information you stored earlier.

How to use?

First, add this module to your POM:

<dependency>
    <groupId>it.mulders.spark-flash</groupId>
    <artifactId>spark-flash</artifactId>
    <version>0.1</version>
</dependency>

Important: register the cleanup filter. If you omit this step, your application will use a lot more memory and users may see odd results because the flash scope is kept as long as their session lives.

import spark.flash.CleanFlashScopeFilter;
import static spark.Spark.after;

public class App {
  public static void main(final String... args){
    after(new CleanFlashScopeFilter());    
  }
}

Finally, use the flash scope in your routes to implement user interface logic:

import static spark.Spark.get;
import static spark.Spark.post;

public class App {
  public static void main(final String... args){
    post("/order", (req, res) -> {
      // Do complex order processing
      flash(req, "status", "Order placed successfully");
      redirect("/confirmation");
      return null;
    });
    get("/confirmation", (req, res) -> {
      return "Your order status is " + flash(req, "status");
    });
  }
}

License

This module is licensed to you under the terms of the Apache License, version 2.0. See the file LICENSE for the full text of this license.

About

Flash support for Spark, the micro framework for creating web applications with minimal effort.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages