2017-11-25 222 views
0

当我使用addFlashAttribute时,应该如何保存来自POST请求的数据?addFlashAttribute和保存数据

@GetMapping("/collage") 
public String paintPicture(@ModelAttribute(value="picture") String img){ 

    //How to hold 'img' here? 
    //When I send GET I want to see the image again (not only after the redirect). 

    return "collage"; 
} 

@PostMapping(value="/sending") 
public String redirect(@RequestParam(value="image") MultipartFile img, RedirectAttributes redirectAttr) throws IOException { 

    String imgAsBase64 = Base64.encodeBase64String(img.getBytes()); 
    redirectAttr.addFlashAttribute("picture",imgAsBase64); 
     return "redirect:/collage"; 
} 

回答

0

RequestMappingHandlerAdapter存储FlashMap(包含闪存变量)作为请求属性。重定向后,会有一个全新的请求,所以闪存变量将会丢失。看来闪光变量在这里不起作用。

你可以使用会话来代替:

session.setAttribute("picture", imgAsBase64); 

然后

String imgAsBase64 = (String) session.getAttribute("picture");