我想知道如何绑定例外handling method
到url mapping
方法:Spring MVC的:正确的异常处理
@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";
}
}
我故意挑起java.lang.ArrayIndexOutOfBoundsException
在users
方法。但我看到handleAllException
方法没有执行。
问题: 我忘了做什么使Exception Handling
工作适当吗?
我认为你试图使用注解'@ ModelAttribute'不正确。请尝试类似于: '@ExceptionHandler(Exception.class) public ModelAndView handleError(Exception exception){ //代码 return new ModelAndView(“error”); }' – NguaCon
谢谢@NguaCon!这是正确的和有益的。 –