0

Im making simple Spring MVC app, also I'm reading Spring in Action 3rd edition. I came to the chapter with file uploading. For this I need to add enctype="multipart/form-data" to < sf:form > tag. When i did it my sf:form stoped working so i found how to fix it question on this web site and replaced < sf:form > by < form >, now its totally broken and i dont know how to fix it, when i make post request i get eror

Required String parameter 'name' is not present

My form (introduction omited ) :

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

And mapping :

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

Also, even if i can fix it, how to make validation to my book entity i think it will be broken too

Entity ::

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

And getters/setters (NotNegative is my custom annotation)

gg ff
  • 489
  • 1
  • 10
  • 20
  • 1
    Restore the correct `sf:form` and post what error you get. You should use the `sf:form` else things like `sf:input` won't work. – M. Deinum Jan 28 '19 at 14:56
  • If i want to make sf:input i need to write name of param in my Entity, right ? How to create param with MultipartFile type in Entity if Hibernate won't allow me to do so ? – gg ff Jan 28 '19 at 15:02
  • If i remove enctype="multipart/form-data" and replace form by < sf:form > and all the inputs by < sf:input > and remove input that uploads a file it's still working – gg ff Jan 28 '19 at 15:07
  • Add `sf:input` for the file upload and use a multipart form as you had before. Then post the error/exception you get. As that is the one you should be fixing. – M. Deinum Jan 28 '19 at 19:22
  • Thx, but i Have already fix it, i just needed to create Multipart Resolver bean, and it start working like a charm, i will answer on my question a bit later, good luck ! – gg ff Jan 28 '19 at 20:36
  • That was probably the solution we would have come up with if you posted the initial error you got. – M. Deinum Jan 28 '19 at 20:40
  • Im agree, anyway im very thankful to you for being ready to help, good luck – gg ff Jan 28 '19 at 21:01

1 Answers1

0

I fixed id by adding an MultipartResolver bean to my Config.

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

Also i needed to apply it to my dispatcher

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

hope my solution will help to smbd.

gg ff
  • 489
  • 1
  • 10
  • 20