【问题标题】:ExceptionHandling with Spring 3Spring 3 的异常处理
【发布时间】:2011-01-03 11:25:56
【问题描述】:

我有这个控制器:

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

    return "excel";

excel 视图实际上打开了一个 ExcelViewer,它是内置方法

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

Class.writecontent
Class.writeMoreContent

被调用的方法将内容写入 Excel 工作表,它们可以抛出例如 biffException。发生异常时如何显示某个错误页面?

我试过了

@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";
    }

    }

但是我收到了服务器关于异常的错误消息。也许缺少 bean 定义?

【问题讨论】:

    标签: java spring exception-handling spring-mvc


    【解决方案1】:

    @ExceptionHandler-annotated 方法仅处理同一类中的处理程序方法抛出的异常。另一方面,您的异常是从Viewrender 方法中抛出的,此时它离开了控制器/处理程序层。

    在 Spring 中处理视图层内的异常并不能很好地处理,主要是因为很难让它与 servlet API 可靠地工作,所以我建议你创建一个 ExcelView 的子类并在那里处理异常.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-17
      • 2023-03-28
      • 2016-10-31
      • 2019-06-02
      • 2017-01-27
      • 2020-12-09
      • 1970-01-01
      相关资源
      最近更新 更多