문제

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

올바른 솔루션이 없습니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top