Spring 具有多个输入的弹簧靴形式+;上传

Spring 具有多个输入的弹簧靴形式+;上传,spring,forms,spring-boot,Spring,Forms,Spring Boot,我的电子商务应用程序中有一个Spring Boot表单。它工作得很好。 我的控制器部分如下所示: @RequestMapping(value = "/admin/add",method = RequestMethod.POST) public String adminAddProductSubmit(@ModelAttribute(value = "product") Product product){ productServiceJpa.addProduct(product);

我的电子商务应用程序中有一个Spring Boot表单。它工作得很好。 我的控制器部分如下所示:

@RequestMapping(value = "/admin/add",method = RequestMethod.POST)
public String adminAddProductSubmit(@ModelAttribute(value = "product") Product product){
    productServiceJpa.addProduct(product);
    return "/admin/added";
}
现在我想添加上传输入到上传图像。有一个问题。 我试过这个:

@RequestMapping(value = "/admin/add",method = RequestMethod.POST)
public String adminAddProductSubmit(final @ModelAttribute(value = "product") Product product, final @RequestAttribute(value = "image") MultipartFile uploadingFile){
    File file = new File(uploadingdir + uploadingFile.getOriginalFilename());

    try {
        uploadingFile.transferTo(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    productServiceJpa.addProduct(product);
}
不幸的是,它不起作用

我的表格:

<form th:action="@{/admin/add}" th:object="${product}"  class="form-horizontal" method="post" enctype="multipart/form-data">
    <h2>Nazwa przedmiotu</h2>
    <div class="form-group">
        <label for="title" class="col-sm-3 control-label">Tytuł</label>
        <div class="col-sm-9">
            <input type="text" th:field="*{title}"  class="form-control" />

        </div>
    </div>
    <div class="form-group">
        <label for="category" class="col-sm-3 control-label">Kategoria</label>
        <div class="col-sm-9">
            <input type="text" th:field="*{category}"  class="form-control" />

        </div>
    </div>
    <div class="form-group">
        <label for="amount" class="col-sm-3 control-label">Ilość</label>
        <div class="col-sm-9">
            <input type="text" th:field="*{amount}" class="form-control" />

        </div>
    </div>

    <div class="form-group">
        <label for="shortDesc" class="col-sm-3 control-label">Krótki opis</label>
        <div class="col-sm-9">
            <input type="text" th:field="*{shortDesc}" class="form-control" />

        </div>
    </div>
    <div class="form-group">
        <label for="description" class="col-sm-3 control-label">Opis</label>
        <div class="col-sm-9">
            <input type="text" th:field="*{description}" class="form-control" />

        </div>
    </div>

    <div class="form-group">
        <label for="price" class="col-sm-3 control-label">Cena</label>
        <div class="col-sm-9">
            <input type="text" th:field="*{price}" class="form-control" />

        </div>
    </div>

    <div class="form-group">
        <label for="image" class="col-sm-3 control-label">
            <div class="col-sm-9">
                <input type="file" th:field="*{image}" class="custom-file-input"/>

                <span class="custom-file-control"></span>
            </div>
        </label>
    </div>
    <input type="submit" class="btn btn-info" value="Dodaj"/>
</form>

纳兹瓦·普泽德米奥图
蒂图什
卡蒂戈里亚
伊洛舍维奇
克洛茨基·奥皮斯
奥皮斯
塞纳
您能告诉我如何发送对象并在@modeldattribute中获取它,以及如何从文件输入中获取文件吗


有很多教程,例如Spring Boot文档中的f.e,但只有上传表单。我想要一个包含许多文本输入和文件输入的表单。

您必须为您的
产品
参数添加
@Valid
注释,下一个参数必须是
BindingResult
。所以你的方法是:

@RequestMapping(value = "/admin/add",method = RequestMethod.POST)
public String adminAddProductSubmit(final @ModelAttribute(value = "product") @Valid Product product, BindingResult bindingResult, final @RequestParam(value = "image") MultipartFile uploadingFile){
    File file = new File(uploadingdir + uploadingFile.getOriginalFilename());

    try {
        uploadingFile.transferTo(file);
    } catch (IOException e) {
        e.printStackTrace();
    }
    productServiceJpa.addProduct(product);
}

解释了为什么
BindingResult
必须位于
@Valid
注释之后。

@RequestAttribute
!=<代码>@RequestParam。。。使用正确的注释。@请阅读: