JSTL Spring Boot - Bean 属性“电子邮件”不可读或具有无效的 getter 方法

问题描述

我对使用 Spring Boot 和 JSTL 还很陌生。我目前正在尝试在我的视图中实现一些东西,我可以选择多个复选框并将数据发送到我的控制器进行操作。我想我已经做对了一切,但似乎无法在这里找到问题的根源。有人可以帮忙吗?收到以下错误

021-02-09 17:49:45.227 错误 16216 --- [nio-8080-exec-6] o.s.web.servlet.tags.form.CheckBoxTag:无效的属性“电子邮件” bean 类 [java.util.ArrayList]:Bean 属性“email”不是 可读或具有无效的 getter 方法: getter 是否匹配 setter 的参数类型?

org.springframework.beans.NotReadablePropertyException:无效 bean 类 [java.util.ArrayList] 的属性“email”:Bean 属性 'email' 不可读或具有无效的 getter 方法: getter 的返回类型是否与 setter 的参数类型匹配?在 org.springframework.beans.AbstractnestablePropertyAccessor.getPropertyValue(AbstractnestablePropertyAccessor.java:625) ~[spring-beans-5.3.3.jar:5.3.3] 在 org.springframework.beans.AbstractnestablePropertyAccessor.getPropertyValue(AbstractnestablePropertyAccessor.java:615) ~[spring-beans-5.3.3.jar:5.3.3]

我的控制器:

package com.bizlinks.mainapp.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.modelattribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;

import com.bizlinks.mainapp.models.Lead;
import com.bizlinks.mainapp.repository.UserRepository;



@Controller
public class AdminController {
    
    @Autowired // This means to get the bean called userRepository
    //Which is auto-generated by Spring,we will use it to handle the data
    private UserRepository userRepository;  
    
    @RequestMapping(value = "admin",method = RequestMethod.GET)
    public String List(Model model) {
        
        
        List<Lead> leads = userRepository.findAll();
        model.addAttribute("leads",leads);
                
        
        return "admin";
        
    }
    
    @RequestMapping(value = "admin",method = RequestMethod.POST)
    public String deleteLead(@modelattribute("leads")List<Lead> leads){
        
        for(int i =0; i< leads.size();i++) {
            
            System.out.println(leads.get(i).getEmail());
        }
        
        return "admin";
    }
}

我的模型:

package com.bizlinks.mainapp.models;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.Table;


@Entity
@Table(name = "leads")
public class Lead {

    @Id
    @Column(name = "email")
    private String email;

    @Column(name = "firstName")
    private String firstName;

    @Column(name = "surName")
    private String surName;
    
    @Column(name ="phone")
    private String phone;

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

    public String getEmail() {
        return email;
    }

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

    public String getFirstName() {
        return firstName;
    }

    public void setSurName(String surName) {
        this.surName = surName;
    }

    public String getSurName() {
        return surName;
    }
    
    public void setPhone(String phone) {
        this.phone = phone;
    }
    public String getPhone() {
        return phone;
    }
}

我的视图主体:

<body>

        
    <nav class="nav nav-fill">
        
      <a class="nav-link" href="/">Home</a>

      
    </nav>
    
    <div class="container-fluid con">
            <table>
                    <tr>
                        <th>First Name</th>
                        <th>Surname</th>
                        <th>Email</th>
                        <th>Phone</th>
                    </tr>
                    
                  <form:form action="admin" modelattribute="leads">
                     <c:forEach items="${leads}" var="item">                    
                        <tr>
                                 
                            <td><c:out value="${item.email}"/></td>                     
                            <td><c:out value="${item.firstName}"/></td>
                            <td><c:out value="${item.surName}"/></td>                       
                            <td><c:out value="${item.phone}"/></td>
                            <form:checkBox path="email" value="${item.email}"/> 
                            
                        </tr>                     
                   </c:forEach>
                   
                   <input type="submit" value="Submit" /> 
                   
                  </form:form>
                   
                
            </table>
    </div>


        <script src="https://code.jquery.com/jquery-3.5.1.slim.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/popper.js@1.16.1/dist/umd/popper.min.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js"></script>
</body>

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)