mjf
mjf

Reputation: 301

ExceptionHandling with Spring 3

I have this controller:

@RequestMapping(value = "*.xls", method = RequestMethod.GET)
public String excel(Model model) {

    return "excel";

The excel wiew opens actually a ExcelViewer, which is build in method

 protected void buildExcelDocument(Map<String, Object> map, WritableWorkbook ww, HttpServletRequest hsr, HttpServletResponse hsr1) throws Exception {

Class.writecontent
Class.writeMoreContent

Called methods write content to the Excel sheet and they can throw e.g biffException. How can I show a certain error page when Exception is occured?

I tried this

@Controller
public class ExcelController
{


    @ExceptionHandler(BiffException.class)
     public String handleException(BiffException ex) {

    return "fail";
    }


   @RequestMapping(value = "*.xls", method = RequestMethod.GET)
    public String excel(Model model) {

        return "excel";
    }

    }

But I'm getting the server's error message about Exceptions. Maybe a bean definition missing?

Upvotes: 2

Views: 1509

Answers (1)

skaffman
skaffman

Reputation: 403591

@ExceptionHandler-annotated methods only handle exceptions thrown by handler methods in the same class. Your exception, on the other hand, is being thrown from within the View's render method, at which point it's left the controller/handler layer.

Handling exceptions from within the view layer isn't well handled in Spring, mainly because it's hard to get it to work reliably with the servlet API, so I recommend you create a subclass of ExcelView and handle the exception in there.

Upvotes: 2

Related Questions