'Redirect user to same page and keep form data

I'm trying to do some custom validation using this code:

Spring endpoint:

@PostMapping(value = "/validate_pairs")
public String validatePair(Model model, RedirectAttributes attributes) {

    // Do here some SQL queries and checks
    attributes.addFlashAttribute("validation_message", "Successful validation!");

    return "redirect:/pairs";
}
    <div class="row g-3 align-items-center mb-1 mt-1">
        <div class="col-auto">
            <input id="pair" class="form-control" name="pair" type="text"
                   placeholder="Pair to be added."
                   th:field="*{pair}"
                   required
                   autofocus>
        </div>
        <div class="col-auto">
            <input id="passthrough_active" type="checkbox"
                   name="active"
                   th:field="*{active}">
        </div>
    </div>

    <br>
    <div class="form-group mb-1 mt-1">
        <button type="submit" name="action" value="validatePair" th:formaction="@{/validate_pairs}" class="btn btn-primary">Validate</button>
    </div>

</form>

As you can see I send the form data to Spring Endpoint and I redirect again the user to the same page. The problem is that the form is empty. I need some way to preserve the data again when the user is redirected. Is there some solution?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source