Spring MVC ExceptionHandling:注释为@ExceptionHandling的操作无法将变量传递给错误视图
我知道很多人都有类似的问题。很抱歉再次发布,但我相信有些事情我可能做得不好 我正在使用Spring3.0.5和freemarker 2.3.14。基本上,我想向用户显示一条友好的错误消息Spring MVC ExceptionHandling:注释为@ExceptionHandling的操作无法将变量传递给错误视图,spring,jakarta-ee,web-applications,spring-mvc,exception-handling,Spring,Jakarta Ee,Web Applications,Spring Mvc,Exception Handling,我知道很多人都有类似的问题。很抱歉再次发布,但我相信有些事情我可能做得不好 我正在使用Spring3.0.5和freemarker 2.3.14。基本上,我想向用户显示一条友好的错误消息 @Controller("exceptioncontroller") public class ExceptionController { private static Logger logger = Logger.getLogger(ExceptionController.class); @RequestM
@Controller("exceptioncontroller")
public class ExceptionController {
private static Logger logger = Logger.getLogger(ExceptionController.class);
@RequestMapping(value = "/site/contentnofoundexception")
public String throwContentFileNotFound(){
boolean exception = true;
if(exception){
throw new ContentFileNotFoundException("content ZZZ123 not found");
}
return "errortest";
}
@ExceptionHandler(value = ContentFileNotFoundException.class)
public String handleFileNotFoundException(ContentFileNotFoundException ex, Model model) {
model.addAttribute("msg",ex.getErrorMessage());//this message is never passed to the error view. msg is always null
return "error";
}
}
//same issue for handleException action which uses ModelAndView
@ExceptionHandler(value = Exception.class)
public ModelAndView handleException(Exception ex){
logger.error(ex);
ModelAndView mv = new ModelAndView();
mv.setViewName("error");
String message = "Something Broke. Please try again later";
mv.addObject("msg", message);
return mv;
}
// Custom Exception class
public class ContentFileNotFoundException extends RuntimeException {
private String errorMessage;
public ContentFileNotFoundException(String message) {
this.setErrorMessage(message);
}
public String getErrorMessage() {
return errorMessage;
}
public void setErrorMessage(String errorMessage) {
this.errorMessage = errorMessage;
}
}
因此,每种情况下,handleFileNotFoundException或handleException操作都被称为OK,但它们无法向error.ftl视图发送任何消息以显示给用户。有什么我需要配置的吗
谢谢你提前帮忙