2013-02-19 68 views
11

我想有以下方法執行重定向:從Spring MVC的@ExceptionHandler方法

@ExceptionHandler(MyRuntimeException.class) 
public String myRuntimeException(MyRuntimeException e, RedirectAttributes redirectAttrs){//does not work 
    redirectAttrs.addFlashAttribute("error", e); 
    return "redirect:someView"; 
} 

我得到一個:

java.lang.IllegalStateException: No suitable resolver for argument [1] type=org.springframework.web.servlet.mvc.support.RedirectAttributes] 

是否有從@ExceptionHandler執行重定向的方式?或者,也許有某種方法來規避這種限制?

編輯:

我已經修改了我的異常處理程序如下:

@ExceptionHandler(InvalidTokenException.class) 
public ModelAndView invalidTokenException(InvalidTokenException e, HttpServletRequest request) { 
RedirectView redirectView = new RedirectView("signin"); 
return new ModelAndView(redirectView , "message", "invalid token/member not found");//TODO:i18n 
} 

這是可能拋出異常的方法:用我的修改

@RequestMapping(value = "/activateMember/{token}", method = RequestMethod.GET, produces = "text/html") 
public String activateMember(@PathVariable("token") String token) { 
    signupService.activateMember(token); 
    return "redirect:memberArea/index"; 
} 

問題異常處理程序是它系統地將我重定向到以下URL:

http://localhost:8080/bignibou/activateMember/signin?message=invalid+token%2Fmember+not+found 

相反的:

http://localhost:8080/bignibou/signin?message=invalid+token%2Fmember+not+found 

編輯2

這裏是我的修飾處理方法:

@ExceptionHandler(InvalidTokenException.class) 
public String invalidTokenException(InvalidTokenException e, HttpSession session) { 
session.setAttribute("message", "invalid token/member not found");// TODO:i18n 
return "redirect:../signin"; 
} 

的問題我現在是該消息被卡住在會議...

+0

你有沒有找到一個不添加查詢字符串參數的解決方案? – mericano1 2013-04-10 12:17:23

+0

不幸的是沒有。 – balteo 2013-04-10 14:08:42

回答

24

請注意,這實際上由Spring 4.3.5+支持開箱即用(有關更多詳細信息,請參閱SPR-14651)。

我設法讓它使用RequestContextUtils類來工作。我的代碼看起來像這樣

@ExceptionHandler(MyException.class) 
public RedirectView handleMyException(MyException ex, 
          HttpServletRequest request, 
          HttpServletResponse response) throws IOException { 
    String redirect = getRedirectUrl(currentHomepageId); 

    RedirectView rw = new RedirectView(redirect); 
    rw.setStatusCode(HttpStatus.MOVED_PERMANENTLY); // you might not need this 
    FlashMap outputFlashMap = RequestContextUtils.getOutputFlashMap(request); 
    if (outputFlashMap != null){ 
     outputFlashMap.put("myAttribute", true); 
    } 
    return rw; 
} 

然後在JSP頁面中,我只是訪問屬性

<c:if test="${myAttribute}"> 
    <script type="text/javascript"> 
     // other stuff here 
    </script> 
</c:if> 

希望它能幫助!

+1

如果你不關心這種屬性的可見性,你可以簡單地將它作爲查詢參數傳遞給'RedirectView',然後將它添加到內部的閃存映射中。例如'返回新的RedirectView(「/ home?param = 1」,true);'。 – Robin 2013-04-18 08:16:16

+0

是的,這是簡單的情況。我有一個特定的要求,以避免搜索引擎優化的原因參數顯然,搜索引擎會把這些URL當作兩個單獨的頁面。 – mericano1 2013-04-18 09:15:59

+0

mericano:非常抱歉,我花了很多時間來看看你的答案。它只是完美的作品!謝謝,接受! – balteo 2013-04-19 12:34:46

2

我正在查看JavaDoc,我看不到RedirectAttributes是接受的有效類型。

+0

這正是我的觀點。 RedirectAttributes不被接受(見我的帖子上面),我正在尋找一種方法來規避這個問題... – balteo 2013-02-19 19:52:05

+0

我沒有看到一個解決方案,它提供了使用Flash屬性的能力。您唯一的選擇是返回一個ModelAndView並將該視圖設置爲一個RedirectView並根據需要設置屬性。我相信這會導致屬性在問題上傳遞,但這可能不是你想要的。 – CodeChimp 2013-02-19 21:23:27

+0

我上面修改了我的帖子。我有重定向問題。我不明白爲什麼我沒有重定向到:_/signin?message = invalid + token%2Fmember + not + found_ – balteo 2013-02-20 13:28:36

6

您可以隨時轉發然後重定向(或重定向兩次)。 首先到另一個請求映射,您可以正常訪問RedirectAttributes,然後再次到達最終目標。

@ExceptionHandler(Exception.class) 
    public String handleException(final Exception e) { 

     return "forward:/n/error"; 
    } 

    @RequestMapping(value = "/n/error", method = RequestMethod.GET) 
    public String error(final RedirectAttributes redirectAttributes) { 

     redirectAttributes.addAttribute("foo", "baz"); 
     return "redirect:/final-destination"; 
    } 
+1

這是一個非常優雅的解決方案,但在此期間,異常信息會丟失。 – polaretto 2015-02-17 16:06:56

+0

什麼信息丟失?異常信息?使用類型爲Exception.class的@ExceptionHandler應該是最後的手段。您應該通過更具體的Exception類來處理已知的異常情況。在這種情況下,您可以轉發更具體的方法,在添加最終目標所需的任何異常特定信息後重定向。在這個簡單的例子中,我們只想知道什麼地方出了問題,是的,沒有任何關於例外的細節使它成爲重定向方法。 – 2015-02-26 19:16:41

相關問題