提问者:小点点

enctype=“multipart/form-data”破坏了我的应用程序


我在做一个简单的Spring MVC应用程序,我也在读Spring in Action第三版。我来到了文件上传这一章。为此,我需要将enctype="multipart/form-data "添加到

必需的字符串参数'name'不存在

我的表格(省略介绍) :

 <div class="center">
 <h1>Add a book!</h1>
  <fieldset>
    <form method="post" action="/books"  acceptCharset="utf-8" 
     enctype="multipart/form-data">
    <table>
        <tr>
            <td>Name</td>
            <td><input type="text" name="name"> <sf:errors path="name" cssClass="isa_error"/></td>
        </tr>
        <tr>
            <td>Desc</td>
<td><textarea cols="50" rows="11" name="desc">Опис</textarea></td>
        </tr>
        <tr>
            <td>Author</td>
            <td><input name="aut" type="text"> <sf:errors path="aut" cssClass="isa_error"/></td>

        </tr>
        <tr>
            <td>Year</td>
            <td><input name="year" type="text"><sf:errors path="year" cssClass="isa_error"/></td>
        </tr>
        <tr>
            <td>Price</td>
            <td><input name="price" type="text"> <sf:errors path="price" cssClass="isa_error"/></td>
        </tr>
        <tr>
            <td><input type="file" name="image" ></td>
        </tr>
        <tr>
            <td><input type="submit" value="add"></td>
        </tr>
    </table>
</form>
<p>
<form action="/books"   method="get" >
    <input type="submit" value="Повернутися">
</form>
</p>
</fieldset>

和映射:

  @PostMapping("/books")
public String add( @RequestParam("name") String name,@RequestParam("desc") String desc,@RequestParam("book") String aut,@RequestParam("year") String year,@RequestParam("price") int price, BindingResult result, @RequestParam(name = "image", required = false) MultipartFile image){
    if (result.hasErrors()) return "addBook";
    Book book = new Book();
    book.setAut(aut);
    book.setName(name);
    book.setDesc(desc);
    book.setYear(year);
    book.setPrice(price);

         service.add(book);
        try {
            if (!image.isEmpty()) {
                validateImg(image);

            }
        } catch (IOException ex){
            result.reject(ex.getMessage());
            return "books";
        }

         return "redirect:/books";
}
@GetMapping(value = "/books", params = "new")
public String addBook(Model model){
 model.addAttribute("book",new Book());
 return "addBook";
}

此外,即使我可以修复它,如何对我的图书实体进行验证,我认为它也会被破坏

实体::

      @Table(name = "library")
@Entity
public class Book {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@Column(name = "idLibrary")
private int id;
@Column(name = "bookName")
@Size(min = 3,message = "Назва має складатися мінімум з 3 букв")
@NotEmpty
private String name;
@NotEmpty(message = "Не можу бути пустим!")
@Column(name = "Author")
private String aut;
@Column(name = "Descriptions")
private String desc;
@Column(name = "Year")
@NotEmpty(message = "Не може бути пустим!")
@Size(min =1,max=4, message = "Некорректний рік")
private String year;
@Column(name = "Price")
@NotNull(message = "Не можу бути пустим!")
@NotNegative(message = "Від'ємна ціна!")
private int price;

和getter/setter(not negative是我的自定义注释)


共1个答案

匿名用户

我通过在我的配置中添加一个MultipartResolver bean来修复id。

    @Bean
    public MultipartResolver multipartResolver(){
    StandardServletMultipartResolver resolver = new StandardServletMultipartResolver();
    return resolver;
}

我还需要将其应用于我的调度员

MultipartConfigElement multipartConfig = new 
MultipartConfigElement(
            "c:/temp", 50000000, 50000000, 0);
    dispatcher.setMultipartConfig(multipartConfig);

希望我的解决方案能对smbd有所帮助。