https://github.com/mthmulders/spark-flash
Flash support for Spark, the micro framework for creating web applications with minimal effort.
https://github.com/mthmulders/spark-flash
microframework spark-java
Last synced: 5 months ago
JSON representation
Flash support for Spark, the micro framework for creating web applications with minimal effort.
- Host: GitHub
- URL: https://github.com/mthmulders/spark-flash
- Owner: mthmulders
- License: apache-2.0
- Created: 2020-08-24T08:03:20.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2025-12-14T13:56:40.000Z (6 months ago)
- Last Synced: 2025-12-16T20:22:45.061Z (6 months ago)
- Topics: microframework, spark-java
- Language: Java
- Homepage:
- Size: 432 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Flash support for Spark Java ⚡

[](https://sonarcloud.io/dashboard?id=mthmulders_spark-flash)
[](https://sonarcloud.io/dashboard?id=mthmulders_spark-flash)
[](https://sonarcloud.io/dashboard?id=mthmulders_spark-flash)
[](https://dependabot.com)
[](https://dashboard.stryker-mutator.io/reports/github.com/mthmulders/spark-flash/master)
[](https://search.maven.org/artifact/it.mulders.spark-flash/spark-flash)
[](./LICENSE)
## TL;DR
This module adds a "flash scope" to [Spark Java](http://sparkjava.com/).
## 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](https://en.wikipedia.org/wiki/Post/Redirect/Get) 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:
```xml
it.mulders.spark-flash
spark-flash
0.1.11
```
**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.
```java
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:
```java
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.