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)