15

次のメソッドが必要です。

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

私が今抱えている問題は、メッセージがセッションでスタックしていることです...

4

4 に答える 4

26

これは、実際には 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>

それが役に立てば幸い!

于 2013-04-10T17:18:36.860 に答える
3

私はJavaDocを見ていますが、RedirectAttributesが受け入れられる有効なタイプである場所がわかりません。

于 2013-02-19T18:26:04.927 に答える