Question

In my webapp I have a form that basically adds data to the database. Since the database has a few constraints like unique columns I need to cleanly handle database exceptions.

However almost every guide I read they display exceptions in a dedicated error page. I however want all exceptions from submitting the form to be displayed on the same page as before, and with the form already filled out with their previously submitted values.

My current non-working code (just gives a blank page):

@ControllerAdvice
public class PostExceptionHandler {
    @ExceptionHandler(Exception.class)
    public void handleAllException(Exception ex, HttpServletRequest request) {
        List<String> errors = (List<String>) request.getAttribute("alertsError");
        Throwable root = Throwables.getRootCause(ex);
        errors.add("Could not submit form to database"
                + "<br>" + root.getClass().getSimpleName() + ": " + root.getMessage()
                + "<pre>" + Throwables.getStackTraceAsString(ex) + "</pre>");
    }
}

How can I make this work on the current page? Spring-Tx also needs to rollback any open transactions during this

No correct solution

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top