繁体   English   中英

SpringBoot+Thymeleaf:在类型对象上找不到属性或字段“名称”

[英]SpringBoot+Thymeleaf: Property or field 'name' cannot be found on object of type

我想用 Thymeleaf 显示 HTML 文件中的对象,但它说“在'com.example.demo.Entities.PeopleInformation'类型的对象上找不到属性或字段'名称' - 可能不是公开的或无效的?”

那是我的页面控制器

package com.example.demo.Controller;

import java.util.ArrayList;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.example.demo.Entities.PeopleInformation;

@Controller
public class HelloWorld {

    @GetMapping(value= "/list_contacts")
    public String showContacts(Model m){
        ArrayList<PeopleInformation> kontakte = new ArrayList<PeopleInformation>();
        PeopleInformation a = new PeopleInformation("Lutz","Walter","0152 222556","Aktuell");
        PeopleInformation b = new PeopleInformation("Bosch","Holger","0152 567345","Aktuell");
        PeopleInformation c = new PeopleInformation("Schindler","Nicole","0152 220022","Aktuell");
        kontakte.add(a);
        kontakte.add(b);
        kontakte.add(c);
        m.addAttribute("kontakte",kontakte);
        return "list_contacts";
    }
}

那是使用 Lombok 的实体

package com.example.demo.Entities;

import org.springframework.data.annotation.Id;

import com.microsoft.spring.data.gremlin.annotation.Vertex;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

@NoArgsConstructor @Setter @Getter 
public class PeopleInformation {
    @Id
    private Long id;
    private String name;
    private String vorname;
    private String telefon;
    private String status;

    public PeopleInformation(String name,String vorname,String telefon,String status) {
        this.name = name;
        this.vorname = vorname;
        this.telefon = telefon;
        this.status = status;
    }
}

这是我的带有 Thymeleaf 的 HTML 文件

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="ISO-8859-1">
<title>Kontakt-Liste</title>
</head>
<body>
     <div>
         <table border="1">
            <tr>
               <th>Name</th>
               <th>Vorname</th>
               <th>Telefon</th>
               <th>Status</th>
            </tr>
            <tr th:each ="kontakte : ${kontakte}">
               <td th:utext="${kontakte.name}">...</td>
               <td th:utext="${kontakte.vorname}">...</td>
               <td th:utext="${kontakte.telefon}">...</td>
               <td th:utext="${kontakte.status}">...</td>
            </tr>
         </table>
      </div>
</body>
</html>

有人能发现问题吗,谢谢:)

如果您想查看数据,您应该在 PeopleInformation 实体中声明 getter

像这样:

    public Long getId() {
    return id;
}

public String getName() {
    return name;
}

public String getVorname() {
    return vorname;
}

public String getTelefon() {
    return telefon;
}

public String getStatus() {
    return status;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM