Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8146423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T13:59:32+00:00 2026-06-06T13:59:32+00:00

I have a Spring MVC application which uses FreeMarker as View technology (But maybe

  • 0

I have a Spring MVC application which uses FreeMarker as View technology (But maybe the view technology doesn’t really matter for my question). I need to intercept all exceptions which may get thrown during a request.

I have implemented a HandlerExceptionResolver but this resolver is only executed when the exception occurs within a controller. But when a controller returns a ModelAndView and the exception occurs while rendering the view (Because a variable was not found or something like this) then the exception resolver is not called and instead I get a stack trace in the browser window.

I also tried using an exception handler method within the controller which returns the view and annotated it with @ExceptionHandler but this also doesn’t work (Most likely again because the exception is not thrown in the controller but in the view).

So is there some Spring mechanism where I can register an exception handler which captures view errors?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T13:59:34+00:00Added an answer on June 6, 2026 at 1:59 pm

    A word upfront: if you just need a “static” error page without much logic and model preparation, it should suffice to put a <error-page>-Tag in your web.xml (see below for an example).

    Otherwise, there might be better ways to do this, but this works for us:

    We use a servlet <filter> in the web.xml that catches all Exceptions and calls our custom ErrorHandler, the same we use inside the Spring HandlerExceptionResolver.

    <filter>
       <filter-name>errorHandlerFilter</filter-name>
       <filter-class>org.example.filter.ErrorHandlerFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>errorHandlerFilter</filter-name>
      <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    The implementation looks essentially like this:

    public class ErrorHandlerFilter implements Filter {
    
      ErrorHandler errorHandler;
    
      @Override
      public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        try {
          filterChain.doFilter(request, response);
        } catch (Exception ex) {
          // call ErrorHandler and dispatch to error jsp
          String errorMessage = errorHandler.handle(request, response, ex);
          request.setAttribute("errorMessage", errorMessage);
          request.getRequestDispatcher("/WEB-INF/jsp/error/dispatch-error.jsp").forward(request, response);
        }
    
      @Override
      public void init(FilterConfig filterConfig) throws ServletException {
        errorHandler = (ErrorHandler) WebApplicationContextUtils
          .getRequiredWebApplicationContext(filterConfig.getServletContext())
          .getBean("defaultErrorHandler");
      }
    
      // ...
    }
    

    I believe this should work pretty much the same for FreeMarker templates. Of course if your error view throws an error, you’re more or less out of options.

    To also catch errors like 404 and prepare the model for it, we use a filter that is mapped to the ERROR dispatcher:

    <filter>
       <filter-name>errorDispatcherFilter</filter-name>
       <filter-class>org.example.filter.ErrorDispatcherFilter</filter-class>
    </filter>
    <filter-mapping>
      <filter-name>errorDispatcherFilter</filter-name>
      <url-pattern>/*</url-pattern>
      <dispatcher>ERROR</dispatcher>
    </filter-mapping>
    
    <error-page>
      <error-code>404</error-code>
      <location>/WEB-INF/jsp/error/dispatch-error.jsp</location>
    </error-page>
    <error-page>
      <exception-type>java.lang.Exception</exception-type>
      <location>/WEB-INF/jsp/error/dispatch-error.jsp</location>
    </error-page>
    

    The doFilter-Implementation looks like this:

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
    
      final HttpServletRequest request = (HttpServletRequest) servletRequest;
    
      // handle code(s)
      final int code = (Integer) request.getAttribute("javax.servlet.error.status_code");
      if (code == 404) {
        final String uri = (String) request.getAttribute("javax.servlet.error.request_uri");
        request.setAttribute("errorMessage", "The requested page '" + uri + "' could not be found.");
      }
    
      // notify chain
      filterChain.doFilter(servletRequest, servletResponse);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Spring-MVC application with Freemarker as the view component. In my templates,
I have an application which are using Hibernate / Spring and Spring MVC, but
I have a Library application which is already implemented in spring MVC. I need
I have an asp.net mvc application, which uses the default user database. It all
I have a Spring MVC application which has a public front-end WAR, an admin
I have a Spring MVC application which communicates with the frontend with AJAX /
I am working on a Spring MVC application in which I have recently been
I have a Spring MVC web application which provides RESTful web services via a
I have an MVC application which uses Windsor everywhere and it works fine everywhere
I have a spring mvc application which make massive use of beans which are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.