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

时间:2020-02-27 22:46:54

标签: java spring spring-boot thymeleaf lombok

我想用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";
    }
}

使用龙目岛的实体

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>

有人可以发现问题吗,谢谢:)

1 个答案:

答案 0 :(得分:1)

如果要查看数据,应在PeopleInformation实体中声明吸气剂

赞:

    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;
}