我有这个 Controller :
@RequestMapping(value = "*.xls", method = RequestMethod.GET)
public String excel(Model model) {
return "excel";
excel wiew打开的其实是一个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 定义?
最佳答案
@ExceptionHandler
- 注释方法仅处理由同一类中的处理程序方法抛出的异常。另一方面,您的异常是从 View
的 render
方法中抛出的,此时它离开了 Controller /处理程序层。
在 View 层中处理异常在 Spring 中没有得到很好的处理,主要是因为很难让它与 servlet API 可靠地工作,所以我建议你创建一个 ExcelView
的子类并且在那里处理异常。
关于java - Spring 3 的异常处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4584032/