GithubHelp home page GithubHelp logo

mthmulders / spark-flash Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 0.0 243 KB

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

License: Apache License 2.0

Java 100.00%
spark-java microframework

spark-flash's Introduction

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.

spark-flash's People

Contributors

dependabot-preview[bot] avatar dependabot[bot] avatar mthmulders avatar

Stargazers

 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.