JavaSpringMVC:正确的异常处理
我想知道如何将异常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";
}
}
我故意在{handleAllException
方法没有被执行
问题:
我忘了做什么才能使Exception Handling
正常工作
# 1 楼答案
试着做这样的事情:
# 2 楼答案
原因是它在尝试调用handleAllException()方法时失败,出现以下异常:
调试[http-nio-8080-exec-6]-ExceptionHandlerExceptionResolver:未能调用@ExceptionHandler方法:公共java。字符串控制器。handleAllException(java.lang.Exception,org.springframework.ui.ModelMap) JAVAlang.IllegalStateException:参数[1][type=org.springframework.ui.ModelMap]没有合适的解析器 HandlerMethod详细信息:
更改方法如下:
# 3 楼答案
试试下面的方法