一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

Spring MVC重定向跳转传递参数的实例

时间:2022-06-29 02:54:59 编辑:袖梨 来源:一聚教程网

在很多时候我们在代码中执行完业务以后需要带参数重定向到某一页面,比如我们在SpringMVC框架中执行完成业务后需要跳转列表,并且需要传递业务结果,我使用的是RedirectAttributes来实现的

代码如下 复制代码

@RequestMapping("/child")
public ModelAndView childChangeClass(String childId, String classId,RedirectAttributes attr) {
ModelAndView mv = new ModelAndView("redirect:childList.htm");
b_child c = (b_child) oc.findObjById(childId, b_child.class);
c.setClass_id(classId);
oc.updateObj(c);
attr.addAttribute("notice", c.getChild_name()+"处理完成");
return mv;
}

attr.addAttribute("notice", c.getChild_name()+"处理完成");


这块是为RedirectAttributes赋值的,那我们的问题是,我如何获取该参数呢?

代码如下 复制代码

@RequestMapping("/childList")
public ModelAndView childList(String srh_classId, String srh_name,
String srh_phone,@RequestParam(value="notice",required=false) String notice) {
ModelAndView mv = new ModelAndView("/jsp_bs/school/child/childList");
List> list = null;

HttpSession session = req.getSession();
String schoolId = session.getAttribute("schoolId").toString();
if (ChkTools.isNull(srh_classId)) {
list = childService.childListBySrhSchool(schoolId, srh_name,
srh_phone, page);
}
mv.addObject("classList", classList);
mv.addObject("notice",notice);
return mv;
}

我们使用

代码如下 复制代码

@RequestParam(value="notice",required=false) String notice

来获得参数的值,然后通过请求传递给页面。


controller间跳转重定向 传参


(1)我在后台一个controller跳转到另一个controller,为什么有这种需求呢,是这样的。我有一个列表页面,然后我会进行新增操作,新增在后台完成之后我要跳转到列表页面,不需要传递参数,列表页面默认查询所有的。

方式一:使用ModelAndView

代码如下 复制代码

return new ModelAndView("redirect:/toList");

这样可以重定向到toList这个方法

方式二:返回String
return "redirect:/ toList ";
其它方式:其它方式还有很多,这里不再做介绍了,比如说response等等。这是不带参数的重定向。

(2)第二种情况,列表页面有查询条件,跳转后我的查询条件不能丢掉,这样就需要带参数的了,带参数可以拼接url

方式一:自己手动拼接url

代码如下 复制代码
new ModelAndView("redirect:/toList?param1="+value1+"&param2="+value2);

这样有个弊端,就是传中文可能会有乱码问题。
方式二:用RedirectAttributes,这个是发现的一个比较好用的一个类
这里用它的addAttribute方法,这个实际上重定向过去以后你看url,是它自动给你拼了你的url。
使用方法:

代码如下 复制代码
attr.addAttribute("param", value);
return "redirect:/namespace/toController";

这样在toController这个方法中就可以通过获得参数的方式获得这个参数,再传递到页面。过去的url还是和方式一一样的。

(3)带参数不拼接url页面也能拿到值(重点是这个)
一般我估计重定向到都想用这种方式:

代码如下 复制代码

@RequestMapping("/save")
public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr)
throws Exception {

String code = service.save(form);
if(code.equals("000")){
attr.addFlashAttribute("name", form.getName());
attr.addFlashAttribute("success", "添加成功!");
return "redirect:/index";
}else{
attr.addAttribute("projectName", form.getProjectName());
attr.addAttribute("enviroment", form.getEnviroment());
attr.addFlashAttribute("msg", "添加出错!错误码为:"+rsp.getCode().getCode()+",错误为:"+rsp.getCode().getName());
return "redirect:/maintenance/toAddConfigCenter";
}
}

@RequestMapping("/index")

public String save(@ModelAttribute("form") Bean form,RedirectAttributes attr)
throws Exception {
return "redirect:/main/list";
}

页面取值不用我说了吧,直接用el表达式就能获得到,这里的原理是放到session中,session在跳到页面后马上移除对象。所以你刷新一下后这个值就会丢掉。

3. 总结
最底层还是两种跳转,只是spring又进行了封装而已,所以说跳转的方式其实有很多很多种,你自己也可以封一个,也可以用最原始的response来,也没有问题。好了,就到这儿。

热门栏目