簡體   English   中英

無法將“java.lang.String”類型的屬性值轉換為屬性“transactions”所需的類型“java.util.List”

[英]Failed to convert property value of type 'java.lang.String' to required type 'java.util.List' for property 'transactions'

我正在使用 thymeleaf 並在將數據從字符串轉換為列表時遇到一些錯誤。 在這里我附上了我的代碼

我的實體類:

@Entity
@Table(name="customer")
public class Customer {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Column(name = "first_name")
@NotNull(message = "First Name cannot be empty")
@Size(min = 1, message = "First Name cannot be empty")
private String firstName;

@Column(name = "last_name")
@NotNull(message = "Last Name cannot be empty")
@Size(min = 1, message = "Last Name cannot be empty")
private String lastName;

@Column(name = "email")
@NotNull(message = "Email ID cannot be empty")
@Pattern(regexp = "^[a-zA-Z0-9.+_-]+@[a-zA-Z0-9.-]+$",
        message = "Enter valid mail id")
private String email;

@Column(name = "branch")
@NotNull(message = "Branch name cannot be empty")
private String branch;

@Column(name = "balance")
private double balance;

@OneToMany(cascade = CascadeType.ALL, fetch = FetchType.LAZY)
@JoinColumn(name = "customer_id")
private List<Transaction> transactions;

public Customer(String firstName, String lastName, String email, String branch, double balance) {
    this.firstName = firstName;
    this.lastName = lastName;
    this.email = email;
    this.branch = branch;
    this.balance = balance;
}

public Customer() {
}

public int getId() {
    return id;
}

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

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getBranch() {
    return branch;
}

public void setBranch(String branch) {
    this.branch = branch;
}

public double getBalance() {
    return balance;
}

public void setBalance(double balance) {
    this.balance = balance;
}

public List<Transaction> getTransactions() {
    return transactions;
}

public void setTransactions(List<Transaction> transactions) {
    this.transactions = transactions;
}

public void addTransaction(Transaction transaction){
    if(transaction == null) {
        transactions = new ArrayList<>();
    }
    transactions.add(transaction);
}

@Override
public String toString() {
    return "Customer{" +
            "id=" + id +
            ", firstName='" + firstName + '\'' +
            ", lastName='" + lastName + '\'' +
            ", email='" + email + '\'' +
            ", branch='" + branch + '\'' +
            ", balance=" + balance +
            '}';
  }


@Entity
@Table(name = "transactions")
public class Transaction {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id")
private int id;

@Column(name = "transaction")
private double amount;

public Transaction() {
}

public Transaction(double amount) {
    this.amount = amount;
}

public int getId() {
    return id;
}

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

public double getAmount() {
    return amount;
}

public void setAmount(double amount) {
    this.amount = amount;
}

@Override
public String toString() {
    return "Transaction{" +
            "id=" + id +
            ", amount=" + amount +
            '}';
}

html格式:

    <h3>Customer Directory</h3>
    <hr>

    <p class="h4 mb-4">Save Customer</p>

    <form action="#" th:action="@{/customers/saveCustomer}"
          th:object="${customer}" method="POST">

        <!-- Add hidden form field to handle the update -->
        <input type="hidden" th:field="*{id}" />

        <input type="text" th:field="*{firstName}"
            class="form-control mb-4 col-4" placeholder="First Name">

        <input type="text" th:field="*{lastName}"
               class="form-control mb-4 col-4" placeholder="Last Name">

        <input type="text" th:field="*{email}"
               class="form-control mb-4 col-4" placeholder="Email">

        <input type="text" th:field="*{branch}"
               class="form-control mb-4 col-4" placeholder="Branch">

        <input type="text" th:field="*{balance}"
               class="form-control mb-4 col-4" placeholder="Balance">

        <input type="text" th:field="*{transactions}"
               class="form-control mb-4 col-4" placeholder="Transactions">

        <!--input type="hidden" th:field="*{transactions}" /-->

        <button type="submit" class="btn btn-info col-2">Save</button>

    </form>

    <hr>
    <a th:href="@{/customers/list}">Back to Customers List</a>

</div>

錯誤:

object='customer' 驗證失敗。 錯誤計數:1 org.springframework.validation.BindException:org.springframework.validation.BeanPropertyBindingResult:1 個錯誤字段“交易”對象“客戶”中的字段錯誤:拒絕值 [[Transaction{id=1, amount=1000.0},交易{id=3,金額=100.0}]]; 代碼 [typeMismatch.customer.transactions,typeMismatch.transactions,typeMismatch.java.util.List,typeMismatch]; 參數 [org.springframework.context.support.DefaultMessageSourceResolvable: 代碼 [customer.transactions,transactions]; 參數 []; 默認消息[交易]]; 默認消息 [無法將“java.lang.String”類型的屬性值轉換為屬性“transactions”所需的類型“java.util.List”; 嵌套異常是 org.springframework.core.convert.ConversionFailedException:無法從類型 [java.lang.String] 轉換為類型 [@javax.persistence.OneToMany @javax.persistence.JoinColumn com.kaneki.springboot.bankapplication.entity。交易] 為值 '[Transaction{id=1, amount=1000.0}, Transaction{id=3, amount=100.0}]'; 嵌套異常是 java.lang.NumberFormatException:對於輸入字符串:“[Transaction{id=1, amount=1000.0}, Transaction{id=3, amount=100.0}]”]

從您的 HTML 中,您將接收交易字段作為字符串,因此在控制器中您必須處理該字符串並從中提取 id 和金額,然后將該 id 和金額設置為交易表。 試試這個,如果你已經嘗試過或在你的控制器中寫過,請分享控制器的代碼。

暫無
暫無

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

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