簡體   English   中英

使用 Spring MVC 將數據從 Thymeleaf 表單保存到數據庫時出現問題

[英]Problem with saving data from Thymeleaf form to database with Spring MVC

我剛剛開始學習 Spring MVC。 我嘗試將一些數據從 Thymeleaf 表單保存到存儲庫,它擴展了 CrudRepository。 不幸的是,數據不顯示。

當我 go 到結果頁面時,我看到使用過的 ID,但沒有輸入要形成的數據。 錯誤在哪里?

在此處輸入圖像描述

這是一個Controller

package com.jtm.twiservice.controller;
import com.jtm.twiservice.Main;
import com.jtm.twiservice.model.Customer;
import com.jtm.twiservice.repository.CustomerRepository;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class CustomerClientController {
private static final Logger myLog = LoggerFactory.getLogger(Main.class);

@Autowired
private CustomerRepository customerRepository;

@GetMapping("/customer")
public String customerForm(Model model) {
    model.addAttribute("customer", new Customer());
    return "customer";
}

@PostMapping("/customer")
public String createCustomer(Customer customer, Model model) {
    customerRepository.save(customer);
    return "customer";
}

@GetMapping("/customers")
public String getCustomers(Model model) {
    model.addAttribute("customers", customerRepository.findAll());
    return "customers;
}
}

` Model :

package com.jtm.twiservice.model;
import org.springframework.beans.factory.annotation.Autowired;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;

@Entity
public class Customer {

@Id
@GeneratedValue(strategy=GenerationType.AUTO)
private Long id;
private String firstName;
private String lastName;
private String email;
private String schoolForm;

public Customer() {}

public Customer(String firstName, String lastName, String email, String schoolForm) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.schoolForm = schoolForm;
}

@Override
public String toString() {
    return String.format(
            "Customer[id=%d, firstName='%s', lastName='%s', email='%s', schoolForm='%s']",
            id, firstName, lastName, email, schoolForm);
}

public Long getId() {
    return id;
}

public String getFirstName() {
    return firstName;
}

public String getLastName() {
    return lastName;
}

public String getEmail() {
    return email;
}

public String getSchoolForm() {
    return schoolForm;
}
}

存儲庫

package com.jtm.twiservice.repository;

import com.jtm.twiservice.model.Customer;
import org.springframework.data.repository.CrudRepository;

public interface CustomerRepository extends CrudRepository<Customer, Long> { }

表格摘錄:

        <form action="#" th:action="@{/customer}" th:object="${customer}" method="post">
            <p class="text"><label>first name:</label>  <input type="text" th:field="*{firstName}" /></p>
            <p class="text"><label>last name:</label> <input type="text" th:field="*{lastName}" /></p
            <p class="text"><label>school form:</label> <input type="text" th:field="*{schoolForm}" /></p>
            <div class="text">email: <input type="text" th:field="*{email}" /></div>
            <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /> </p>

        </form>

並從結果模板中提取:

<td th:text="${customer.id}">customer ID</td>
<td th:text="${customer.firstName}">first name</td>
<td th:text="${customer.lastName}">last name</td>

嘗試使用

List<Customer> customers = new customerRepository.findAll();
Model.addAttribute("c" , customers);

更多信息請看我的問題在H2數據庫spring boot thymleaf中存儲圖片

另一種方式,使用此代碼:

  model.addAttribute("customers", customerRepository.findAll());

對於結果模板:

<th:block th:each="customer : ${customers}">
  <td th:text="${customer.id}">customer ID</td>
  <td th:text="${customer.firstName}">first name</td>
  <td th:text="${customer.lastName}">last name</td>
</th:block>

嘗試這個 :

 @PostMapping("/customer") public String createCustomer(Customer customer,BindingResult result) { if (result.hasErrors()) { return "your error page in here"; } customerRepository.save(customer); return "customer"; }

雖然這篇文章已經很老了,但我只是在調試自己的代碼時偶然發現了它; 我的回答可能對以后的人有幫助。

我相信我有一個類似的問題,其中 Thymeleaf 表單實際上沒有為實體的字段分配值。 對我來說,問題是該實體的字段沒有設置器 - 看起來你在這里也有這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM