Q:

SpringMVC using @ExceptionHandler HTTP Status 500 - Expected session attribute

I would like to use @exceptionhandler to capture an HTTP Status 500 - Expected session attribute. I would like to return a message to the same page i am on showing the user the error.
Can someone point me to an example on how i can handle this exception and return a message to the view instead of redirecting to another page.
This is what i have so far however the item in the view is not getting set with the error message;
@ExceptionHandler(HttpSessionRequiredException.class)
    public RedirectView handleHttpSessionRequiredException(Exception ex, HttpServletRequest request) throws Exception
    {
        logger.info("In the handleHttpSessionRequiredException Handler Method");
        String referrer = request.getHeader("referer");
        RedirectView redirectView = new RedirectView(referrer);
        redirectView.addStaticAttribute("errorMessage","Execute A Query Then Retry");
        return redirectView;
    }

View
<label id="errorMessage" name="errorMessage">${errorMessage}</label>

A:

You can get the referer and forward or redirect to it. E.g.
@ExceptionHandler(HttpSessionRequiredException.class)
public String (HttpServletRequest request) {
    String referrer = request.getHeader("referer");
    ...
    FlashMap flashMap = RequestContextUtils.getOutputFlashMap(request);
flashMap.put("errorMessage","Execute A Query Then Retry");
    return "redirect:/my/url";
}

The redirect URL is relative to the application path. You can extract it from the referer.

