1

我知道很多人都遇到过类似的问题。抱歉再次发布,但我相信有些事情我可能做得不好。

我正在Spring 3.0.5使用freemarker 2.3.14. 基本上我想向用户显示一个友好的错误消息。

@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;
 }
}

因此,每个案例handleFileNotFoundExceptionhandleException操作都可以调用,但它们不能向error.ftl视图发送任何消息以显示给用户。有什么我需要配置的吗?

感谢您提前帮助

4

0 回答 0