2017-11-25 71 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");