为什么我的视图html文件和thymeleaf在spting应用程序中不显示对象列表?

aurhwmvo  于 2021-06-29  发布在  Java
关注(0)|答案(1)|浏览(290)

当我将其作为modelattribute添加到视图时,在显示人员列表时遇到问题
以下是与此问题相关的简短代码片段:

@Controller
@RequestMapping("/people")
 public class PeopleController {

  private final PersonDAO personDAO;

 @Autowired
 public PeopleController(PersonDAO personDAO) {
     this.personDAO = personDAO;
 }

 @GetMapping()
 public String index(Model model){
     model.addAttribute("people", personDAO.index());
     return "people/index";
 }
}

这是我的html文件:

<!DOCTYPE html>
<html lang="en" xmlns:th="http://thymeleaf.org/" xmlns:xsi="http://www.w3.org/2001/XMLSchema- 
instance"
  xsi:schemaLocation="http://thymeleaf.org/ ">
<head>
 <meta charset="UTF-8">
 <title>All people</title>
</head>
<body>

 <div th:each="person : ${people}">
  <a th:href="@{/people/{id}(id =${person.getId()})}" th:text ="${person.getName() + ', ' + 
  person.getSurname()
  + ', ' + person.getAge() + ', ' + person.getUsername() + ', ' + person.getPassword()}">VALUE</a>
 </div>
 <br/>
 <hr/>
 <a href="/people/new">Create new person</a>
</body>

我可以在控制台中显示从persondao.index()获取的完整人员列表,然后返回我的视图return“people/index”,一切正常。所以我假设列表正确地选择所有对象并将其传递给模型。
这就是persondao.index()后面的内容;方法:

List<Person> people = session.createQuery("from People", Person.class ).list();

但是网页是空的。可能问题隐藏在我的视图文件中。我很感激你能帮我找出问题所在。

gab6jxml

gab6jxml1#

我创造了 Person 模型如示例所示:

@Entity
@Table(name = "person")
public class Person {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private long id;
    private String name;
    private String surname;
    private String age;
    private String username;
    private String password;

    public Person() {
    }

    // getter/setter ..
}

我创造了一个 PersonRepository 为了得到人民。您可以查看“使用spring数据存储库”页面了解更多信息。

@Repository
public interface PersonRepository extends JpaRepository<Person, Long> {}

我创造了 PeopleController .

@Controller
@RequestMapping("/people")
public class PeopleController {

    private final PersonRepository personRepository;

    public PeopleController(PersonRepository personRepository) {
        this.personRepository = personRepository;
    }

    @GetMapping()
    public String index(Model model) {
        model.addAttribute("people", personRepository.findAll());
        return "people/index";
    }
}

我创造了 index.html 内部 /resources/templates/people/ 文件夹。

<!DOCTYPE html>
<html lang="en"
      xmlns:th="http://thymeleaf.org/"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://thymeleaf.org/ ">
<head>
    <meta charset="UTF-8">
    <title>All People</title>
</head>
<body>
    <div th:each="person : ${people}">
        <a th:href="@{/people/{id}(id =${person.getId()})}"
           th:text ="${person.getName() + ', ' + person.getSurname() + ', ' +
                       person.getAge() + ', ' + person.getUsername() + ', ' +
                       person.getPassword()}">VALUE</a>
    </div>
    <br/>
    <hr/>
    <a href="/people/new">Create new person</a>
</body>

我把几张唱片加到 person table 举个例子。

我想我已经达到了你想看到的结果。

相关问题