Skip to content

Form Post Exception Handling

İlhan Subaşı edited this page Nov 26, 2019 · 9 revisions

This article is not relevant if FORM_POST methods are written in the 1.2.1 style.

FORM_POST methods written with the 1.1.x style are handled by the Springframework. That means ExtDirectSpring cannot catch and manage exceptions that are thrown in such methods. For all the other method types ExtDirectSpring catches the exceptions, converts them into json messages and sends them to the client.

To enable the same behaviour for FORM_POST methods it's necessary to add a HandlerExceptionResolver to the application. The code for such a resolver has to contain this line: ExtDirectResponseBuilder.create(request, response).setException(ex).buildAndWrite()

Here a complete example

    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    
    import org.springframework.stereotype.Component;
    import org.springframework.web.servlet.HandlerExceptionResolver;
    import org.springframework.web.servlet.ModelAndView;
    
    import ch.ralscha.extdirectspring.bean.ExtDirectResponseBuilder;
    
    @Component
    public class FormPostExceptionHandler implements HandlerExceptionResolver {
    
      @Override
      public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response,
    			final Object handler, Exception ex) {
        ExtDirectResponseBuilder.create(request, response)
                                .setException(ex)
                                .buildAndWrite();
        return null;
      }
    }