2012-02-07 54 views
0

我有以下控制器代碼。這將顯示一個在jQuery選項卡式窗格中的前端表中的銀行列表。 /bankList.do在用戶點擊該選項卡時被調用。 banksList.jsp將在成功標籤中呈現。彈簧控制器異常/錯誤頁面呈現

@RequestMapping(value = "/banksList", method = RequestMethod.GET) 
public ModelAndView banksList() throws Exception { 
    BankList banksList = bankService.list(); 
    return new ModelAndView("banksList", "banksList", banksList.getBanks()); 
} 

我不知道如何處理在bankend中拋出的錯誤/異常。 當銀行存在異常情況時,我想向用戶顯示以下文字:「與後端通信時出錯,請稍後再試。」而不在標籤窗格中顯示錶格。如何在上述控制器中進行更改以實現錯誤功能。

+0

可能重複? http://stackoverflow.com/questions/2538031/spring-mvc-best-practice-handling-unrecoverable-exceptions-in-controller – seanhodges 2012-02-07 11:22:20

回答

1

這是一種方式,加上這樣的事情你的控制器類...

@ExceptionHandler({SomeException.class, SomeOtherException.class}) 
public String doException(final Exception e, final HttpSession session) { 
    LOGGER.error("something failed", e); 
    session.setAttribute(UPLOAD_STATUS, false); 
    session.setAttribute(ERROR_MESSAGE, e.getMessage()); 
    return "redirect:" + VIEW_NAME; 
} 
+0

\t \t <屬性名= 「exceptionMappings」> \t \t \t \t \t \t \t <條目鍵= 「com.myapp.simulator.service.impl.ServiceConnectionException」 值= 「connectionError」/> \t \t \t \t \t \t \t <屬性名=「defaultErrorView」值=「generalErrorPage」 /> \t在context.xml中配置和添加connectionError.jsp頁到我的webapp – nagendra 2012-02-07 12:04:37

+0

總是有做的事情在Spring MVC的方法不止一種。 – 2012-02-07 16:17:26