【问题标题】:Upload files to the @ModelAttribute using Thymeleaf使用 Thymeleaf 将文件上传到 @ModelAttribute
【发布时间】:2018-05-08 16:09:06
【问题描述】:

如何使用 Thymeleaf 将文件上传到 @ModelAttribute? 我正在做的事情是:

上传.html

<form method="POST" action="#" th:action="@{/sending}" th:object="${collage}" enctype="multipart/form-data" >
            <input type="file" th:field="*{picture}" />
            <input type="file" th:field="*{picture}"  />
            <input type="submit" value="upload" />
</form>

我的控制器:

@Controller
public class MainController {

@GetMapping(value = { "/" })
public String index(){
    return "upload";
}

@GetMapping("/collage")
public String paintPicture(Model model){        
    return "collage";
}

@PostMapping("/sending")
public String redirect(@ModelAttribute(value="collage") Collage collage, RedirectAttributes redirectAttr) {

        Collections.shuffle(Arrays.asList(collage.getCollage()));
        redirectAttr.addFlashAttribute("pictures",collage.getCollage());
        return "redirect:/collage"; 
}
}

拼贴类:

public class Collage {

private MultipartFile[] pictures;

public Collage(){}

public MultipartFile[] getCollage() {
    return pictures;
}

public void setCollage(MultipartFile[] pictures) {
    this.pictures = pictures;
}
}

我在控制台中得到:java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'collage' available as request attribute 和“/”页面上的文本:

【问题讨论】:

  • @Jorge L. Morla 谢谢。但我认为,我的主要问题是 时,我无法显示主页。当我只使用 时,我可以显示主页,选择要上传的图像,但最后我从拼贴对象中得到 null。

标签: java spring-boot thymeleaf


【解决方案1】:

一张图胜过1000字:

现在代码示例在实体内上传一个多部分文件数组:

<form action="#" th:action="@{/distribution/save}" class="form-horizontal"
                              role="form" method="post" th:object="${news}" enctype="multipart/form-data">

    <input type="hidden" name="id" value="id" th:field="*{id}"> <div class="form-group has-label"> <label for="inputTitle" th:text="#{news.title}">Título</label>
    <input type="text" class="form-control" id="inputTitle"  th:placeholder="#{news.title}" th:field="*{title}"></div>
    <input type="file" name = "multipartFilesDocument" value="multipartFilesDocument"  th:field="*{multipartFilesDocument}" multiple="multiple"/>
<button type="submit" class="btn btn-default"><span th:text="#{common.save}"></span></button>
</div>
</form>

控制器代码:

 @PostMapping("/save")
    public String saveMultiparthFile(Model model,@ModelAttribute NewsDTO eventDTO){

        eventDTO.getId();
        return getrDetail(model);
    }

实体代码:

public class NewsDTO {
private List<MultipartFile> multipartFilesDocument;
  public List<MultipartFile> getMultipartFilesDocument() {
        return multipartFilesDocument;
    }

    public void setMultipartFilesDocument(List<MultipartFile> multipartFilesDocument) {
        this.multipartFilesDocument = multipartFilesDocument;
    }
}

在这段代码中真的很重要enctype="multipart/form-data"name = "multipartFilesDocument" value="multipartFilesDocument"在表单中

【讨论】:

    【解决方案2】:

    您可以应用此更改

    1) 将@ModelAttibute 更改为@RequestParam

    2) 使用 MultipartFile[] 作为参数并且只使用单个输入文件 html

    //name of input html should be collage
    @PostMapping("/sending")
    public String redirect(@RequestParam("collage") MultipartFile[] files, RedirectAttributes redirectAttr) {
    
            Collections.shuffle(Arrays.asList(files));
            redirectAttr.addFlashAttribute("pictures",files);
            return "redirect:/collage"; 
    }
    

    和你的html页面

    <form method="POST" th:action="@{/sending}" enctype="multipart/form-data" >
                <input type="file" name="collage" multiple="multiple"/>
                <input type="submit" value="upload" />
    </form>
    

    【讨论】:

    • 那不行。即使我尝试:@PostMapping("/sending") public String redirect(...) { System.out.println(collage.getCollage()); //这里已经是NULL了
    • 我得到 collage.length == 0 :(
    • 对不起,我错了,我忘记更改 @RequestParam 并添加 html 更改。
    • 它只适用于@ModelAttribute("obj") MultipartFile 文件,所以只适用于一个文件。
    • 它有效。谢谢! :) (但是我仍然很好奇如何使用 @ModelAttribute 和模型类来做到这一点。)
    猜你喜欢
    • 2019-05-22
    • 2022-06-14
    • 1970-01-01
    • 2014-07-17
    • 2020-12-08
    • 1970-01-01
    • 2020-05-01
    • 2014-05-01
    • 2017-08-17
    相关资源
    最近更新 更多