【发布时间】:2016-07-18 09:42:34
【问题描述】:
我想知道如何将异常handling method绑定到url mapping方法:
@Controller
public class UserController {
@Autowired
UserDao userDao;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public String users(@ModelAttribute("model") ModelMap model) {
model.addAttribute("userList", userDao.getAll());
String[] b = new String[0];
String a = b[1];
return "user";
}
@ExceptionHandler(Exception.class)
public String handleAllException(Exception ex, @ModelAttribute("model") ModelMap model) {
model.addAttribute("error", "Exception happened");
return "error_screen";
}
}
我故意在users方法中挑衅java.lang.ArrayIndexOutOfBoundsException。但是我看到 handleAllException 方法没有被执行。
问题:
我忘记做什么才能使Exception Handling 正常工作?
【问题讨论】:
-
我认为您尝试错误地使用注释
@ModelAttribute。请尝试类似的方法: ` @ExceptionHandler(Exception.class) public ModelAndView handleError(Exception exception) { //这里的代码 return new ModelAndView("error"); }` -
谢谢你,@NguaCon!这是正确且有用的。
标签: java spring spring-mvc exception-handling