springboot重定向redirect隐藏参数 springboot 怎样重定向redirect 并隐藏参数

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

springboot重定向redirect隐藏参数 springboot 怎样重定向redirect 并隐藏参数

wgxaszc8   2021-09-02 我要评论
想了解springboot 怎样重定向redirect 并隐藏参数的相关内容吗,wgxaszc8在本文为您仔细讲解springboot重定向redirect隐藏参数的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:springboot,重定向redirect,隐藏参数,下面大家一起来学习吧。

springboot 重定向redirect 并隐藏参数

在做全局异常处理的时候,碰到重定向到全局错误页面

所谓隐藏参数无非是把参数放到了session中,再重定向后将该值清除

1、全局异常处理方法

@ExceptionHandler(value = Exception.class)
public ModelAndView exceptionHandle(RedirectAttributes redirectAttributes) {
    ModelAndView modelAndView = new ModelAndView("redirect:/systemError");
    redirectAttributes.addFlashAttribute("error", "错误信息");
    return modelAndView;
}

2、重定向方法

@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

在这里插入图片描述

前端

在这里插入图片描述

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们