在springboot中,当使用接口访问controller时,处理完成,并且需要进行页面重定向,并且需要将此次的数据带到重定向的页面中去,需要在进行页面重定向的controller中添加属性RedirectAttributes。
@RequestMapping(value = "up",method = RequestMethod.POST)
public String up(MultipartFile uploadFile, RedirectAttributes redirectAttributes) throws IOException {
redirectAttributes.addAttribute("name","ls");
redirectAttributes.addAttribute("file",LocalDate.now().toString() );
return "redirect:index";
}
然后在被跳转的controller中将参数进行接收。
@RequestMapping("/index")
public ModelAndView index(@ModelAttribute("name") String name,@ModelAttribute("file") String file){
ModelAndView modelAndView = new ModelAndView();
if(name != null){
modelAndView.addObject("name", name);
modelAndView.addObject("file", file);
}
// 封装返回结果到Model中
// 设置返回的视图名称
modelAndView.setViewName("index");
return modelAndView;
}
因篇幅问题不能全部显示,请点此查看更多更全内容