springboot 重定向redirect 并隱藏參數
在做全局異常處理的時候,碰到重定向到全局錯誤頁面
所謂隱藏參數無非是把參數放到了session中,再重定向后將該值清除
1、全局異常處理方法
1
2
3
4
5
6
|
@ExceptionHandler (value = Exception. class ) public ModelAndView exceptionHandle(RedirectAttributes redirectAttributes) { ModelAndView modelAndView = new ModelAndView( "redirect:/systemError" ); redirectAttributes.addFlashAttribute( "error" , "錯誤信息" ); return modelAndView; } |
2、重定向方法
1
2
3
4
5
6
|
@GetMapping ( "/systemError" ) public ModelAndView systemError( @ModelAttribute ( "error" ) String error){ ModelAndView modelAndView = new ModelAndView( "error" ); modelAndView.addObject( "error" , error); return modelAndView; } |
springboot redirect 傳參問題
眾所周知:
redirect表示重定向,相比于請求轉發,無法將添加的參數繼續保留,傳遞給下一個處理對象,但springboot給我們提供了一個方法,redirectattributes的addflashattribute方法將參數,即使通過重定向也能傳遞出去,底層原理使用的是緩存臨時保存 重定向所攜帶的參數
具體案例
controller
前端
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/wgxaszc8/article/details/79494286