I';我正试图用JavaSpring将图像添加到表单中,这在我开始尝试添加图像表单之前是可行的。你能简单地告诉我怎么做吗?

I';我正试图用JavaSpring将图像添加到表单中,这在我开始尝试添加图像表单之前是可行的。你能简单地告诉我怎么做吗?,java,spring,spring-boot,model-view-controller,core,Java,Spring,Spring Boot,Model View Controller,Core,创建MVC连接mysql的模型。类名是PostBook.java。在我开始尝试将图像上传到数据库之前,它就起作用了 @Entity public class PostBook{ @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String titleBook, authorBook; private int year, price;

创建MVC连接mysql的模型。类名是PostBook.java。在我开始尝试将图像上传到数据库之前,它就起作用了

    @Entity
public class PostBook{

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    private String titleBook, authorBook;
    private int year, price;

    @Lob
    private byte[] image;

    public byte[] getImage() {
        return image;
    }

    public void setImage(byte[] image) {
        this.image = image;
    }

    public int getPrice() {
        return price;
    }

    public void setPrice(int price) {
        this.price = price;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getTitleBook() {
        return titleBook;
    }

    public void setTitleBook(String titleBook) {
        this.titleBook = titleBook;
    }

    public String getAuthorBook() {
        return authorBook;
    }

    public void setAuthorBook(String authorBook) {
        this.authorBook = authorBook;
    }

    public int getYear() { return year; }

    public void setYear(int year) {
        this.year = year;
    }

    public PostBook(String titleBook, String authorBook, int year, byte[] image, int price) {
        this.titleBook = titleBook;
        this.authorBook = authorBook;
        this.year = year;
        this.price = price;
        this.image=image;
    }

    public PostBook() {
    }
}
模型视图控制器的控制器。在我开始尝试在那里添加图像之前,它也起了作用。这篇文章的一些文字)

@控制器
公共类图书管理员{
@自动连线
私人PostBookRepository PostBookRepository;
@GetMapping(“/books”)
公共字符串图书(模型){
List postBooks=postBookRepository.findAll();
模型。添加属性(“标题”、“书籍”);
model.addAttribute(“postBooks”,postBooks);
归还“书籍”;
}
@GetMapping(“/add_books”)
公共字符串bookMain(模型){
List postBooks=postBookRepository.findAll();
model.addAttribute(“postBooks”,postBooks);
模型。添加属性(“标题”、“新书”);
返回“添加书籍”;
}
@邮戳(“/添加书籍”)
公共字符串addBook(@RequestParam String titleBook、@RequestParam String authorBook、@RequestParam int year、@RequestParam int price、@RequestParam byte[]image,Model Model){
PostBook PostBook=新的PostBook(标题簿、作者簿、年份、图像、价格);
postBookRepository.save(postBook);
返回“重定向:/books”;
}
@GetMapping(“/books/{id}/edit_book”)
公共字符串bookEdit(@PathVariable(value=“id”)长id,模型){
如果(!postBookRepository.existsById(id)){
返回“重定向:/books”;
}
可选编辑=postBookRepository.findById(id);
model.addAttribute(“edit”,edit.get());
model.addAttribute(“标题”、“编辑书”);
返回“编辑书籍”;
}
@后期映射(“/books/{id}/edit_book”)
公共字符串更新本(@PathVariable(value=“id”)长id、@RequestParam字符串标题本、@RequestParam字符串authorBook、@RequestParam int year、@RequestParam int price、@RequestParam byte[]image、Model Model){
PostBook PostBook=postBookRepository.findById(id).orelsetrow();
postBook.setTitleBook(titleBook);
postBook.setAuthorBook(authorBook);
postBook.setYear(年);
postBook.setPrice(价格);
postBook.setImage(image);
postBookRepository.save(postBook);
返回“重定向:/books”;
}
@后期映射(“/books/{id}/remove”)
公共字符串deleteBook(@PathVariable(value=“id”)长id,模型){
PostBook PostBook=postBookRepository.findById(id).orelsetrow();
postBookRepository.delete(postBook);
返回“重定向:/books”;
}
}
还有添加书籍的html部分。文件名为add_books.html。还有一些话要问

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
  <meta charset="UTF-8">
  <title th:text="${title}"/>
</head>
<body>
<header th:insert="blocks/header::header"></header>

<div class="container mt-5 mb-5">
  <h1 th:text="'Add ' + ${title} + '!'" ></h1>
  <form action="add_books" method="post" border="3">
    <input type="text" name="titleBook" required placeholder="Enter book's title" class="form-control">
    <input type="text" name="authorBook" required placeholder="Enter book's author" class="form-control">
    <input type="number" min="1000" max="2021" name="year" required placeholder="Enter book's year" class="form-control">
    <input type="number" min="0" name="price" placeholder="Enter book's price" class="form-control">
    <input type="file" placeholder="Enter book's image">           <!--to upload image-->
    <button type="submit" class="btn btn-success">Save book</button>
  </form>
</div>

</body>
</html>

存折
下面是添加newbooks.html后的情况。一些额外的词来张贴这个问题

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
  <title th:text="${title}"/>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<header th:insert="blocks/header::header"></header>


<form action="/add_books">
  <button class="btn btn-success">Add new book</button>
</form>


<table class="table" border="3">

  <tr>
    <th scope="col">id</th>
    <th scope="col">Title</th>
    <th scope="col">Author</th>
    <th scope="col">Year</th>
    <th scope="col">Price</th>
    <th scope="col">picture</th>
    <th scope="col">Edit</th>
    <th scope="col">Delete</th>
  </tr>

  <tr th:each="el : ${postBooks}">
    <td scopoe="row" th:text="${el.id}"/>
    <td th:text="${el.titleBook}"/>
    <td th:text="${el.authorBook}"/>
    <td th:text="${el.year}"/>
    <td th:text="${el.price}"/>
    <td th:src="${el.image}"/>
    <td >
      <a th:href="'/books/' + ${el.id} + '/edit_book'">
        <button class="btn btn-warning" type="submit">Edit</button>   <!-- Button to edit-->
      </a>
    </td>
    <td>
      <form th:action="'/books/' + ${el.id} + '/remove'" method="post">
        <button class="btn btn-warning" type="submit">Delete</button>    <!-- Button to delete-->
      </form>
    </td>

  </tr>
</table>

</body>
</html>

添加新书
身份证件
标题
作者
年
价格
照片
编辑
删除
删除

ow键。您遇到的实际问题是什么?出现了意外错误(类型=错误请求,状态=400)。方法参数类型byte[]所需的请求参数“image”不存在,因此,您要求请求参数image,但未将其添加到请求中。你真的应该开始研究你的代码,你的get和帖子的映射是相同的(“添加书籍”),这很奇怪。您是否调试了代码,以查看执行了什么,以及您认为存在的所有值是否都存在?好的,我会尝试
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-+0n0xVW2eSR5OomGNYDnhzAbDsOXxcvSN1TPprVMTNDbiYZCxYbOOl7+AMvyTG2x" crossorigin="anonymous">
  <title th:text="${title}"/>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<header th:insert="blocks/header::header"></header>


<form action="/add_books">
  <button class="btn btn-success">Add new book</button>
</form>


<table class="table" border="3">

  <tr>
    <th scope="col">id</th>
    <th scope="col">Title</th>
    <th scope="col">Author</th>
    <th scope="col">Year</th>
    <th scope="col">Price</th>
    <th scope="col">picture</th>
    <th scope="col">Edit</th>
    <th scope="col">Delete</th>
  </tr>

  <tr th:each="el : ${postBooks}">
    <td scopoe="row" th:text="${el.id}"/>
    <td th:text="${el.titleBook}"/>
    <td th:text="${el.authorBook}"/>
    <td th:text="${el.year}"/>
    <td th:text="${el.price}"/>
    <td th:src="${el.image}"/>
    <td >
      <a th:href="'/books/' + ${el.id} + '/edit_book'">
        <button class="btn btn-warning" type="submit">Edit</button>   <!-- Button to edit-->
      </a>
    </td>
    <td>
      <form th:action="'/books/' + ${el.id} + '/remove'" method="post">
        <button class="btn btn-warning" type="submit">Delete</button>    <!-- Button to delete-->
      </form>
    </td>

  </tr>
</table>

</body>
</html>