有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java addFlashAttribute和保存数据

使用addFlashAttribute时,如何保存POST请求中的数据

@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"; 
}

共 (1) 个答案

  1. # 1 楼答案

    RequestMappingHandlerAdapterFlashMap(包含flash变量)存储为请求属性。重定向之后,会有一个全新的请求,因此flash变量将丢失。看来flash Var在这里不起作用

    您可以使用会话:

    session.setAttribute("picture", imgAsBase64);
    

    然后

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