数据库返回空值问题,框架Sping Boot 和MySQL,thymeleaf

vybvopom  于 2023-08-02  发布在  Mysql
关注(0)|答案(1)|浏览(94)

我正在做一个简单的书店Web应用程序,后端使用spring-boot,主要问题,代码似乎运行良好,没有错误,但数据库无法接收来自论坛用户的输入数据,数据库只返回空值像(?,?,?),代码如下>>
我的预期结果我希望给定的值被存储在mysql数据库

<div class="container my-5 p-5" style="border: 1px solid black;">
    <h4 class="text-centre">New Book Register</h4>
    <form class="col-md-4 offset-md-4" th:action="@{/save}"  method="post">
        <div class="mb-3">
            <label for="author" class="form-label">Author</label>
            <input type="text" class="form-control" id="author">
        </div>
        <div class="mb-3">
            <label for="name" class="form-label">name</label>
            <input type="text" class="form-control" id="name">
        </div>
        <div class="mb-3 form-check">
            <label for="price" class="form-label">price</label>
            <input type="text" class="form-control" id="price">
        </div>
      <div style="text-align: centre;"><button type="submit" class="btn btn-primary">Submit</button></div>
    </form>

back end---------------------
@Entity

@Table(name = "Book")
public class BookEntity {
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)

    private int id;
    private String name;
    private String author;
    private String price;
    public BookEntity(int id, String name, String author, String price) {
        this.id = id;
        this.name = name;
        this.author = author;
        this.price = price;
    }
    
    public int getId() {
        return id;
    }
    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getPrice() {
        return price;
    }

    public void set Price(String price) {
        this.price = price;
    }
    
    public Book Entity() {

    }
------------------------------
@Controller

public class BookController {

    @Autowired
    private BookService service;

    @RequestMapping("/")
public   String home(){

        return "home";
    }

    @GetMapping("/book_register")
    public   String bookRegister(){

        return "bookRegister";
    }

    @Get Mapping("/Available_Book")
    public String AvailableBook()
    {

        return "AvailableBook";
    }

     @PostMapping("/save")
    public  String addBook(@ModelAttribute BookEntity b){
        service.save(b);
        return "redirect:/Available_Book";
     }

@Service
public class Book Service {
    @Autowired
    private Book Repository repository;

    public void save(Book Entity b){
        repository.save(b);

    }

字符串

yeotifhr

yeotifhr1#

似乎HTML中的表单在输入字段中缺少name属性。name属性对于将表单数据发送到后端服务器至关重要。没有它,表单数据将无法正确Map到Sping Boot 应用程序中的BookEntity类。
要解决这个问题,请确保将name属性添加到每个输入字段中,如

<input type="text" class="form-control" id="author" name="author">

字符串

相关问题