具有许多输入+上传的弹簧启动表单



我在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>

你能告诉我如何发送我的对象并在@ModelAttribute中获取它并从文件输入中获取文件吗?

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

您必须

Product参数添加@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注释之后出现。

最新更新