Java 无法使用Spring和EleAF传输视图上的对象
我曾试图使用Spring和Thymeleaf创建超级简单的应用程序,但在视图中传输对象时遇到了一些问题 类Person.java:Java 无法使用Spring和EleAF传输视图上的对象,java,spring,spring-boot,thymeleaf,Java,Spring,Spring Boot,Thymeleaf,我曾试图使用Spring和Thymeleaf创建超级简单的应用程序,但在视图中传输对象时遇到了一些问题 类Person.java: public class Person { private String firstName; private String lastName; private int age; public Person(String firstName, String lastName, int age) { super(); this.firstNam
public class Person {
private String firstName;
private String lastName;
private int age;
public Person(String firstName, String lastName, int age) {
super();
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
我的控制器类PeoplePageCotroller.java如下所示:
@Controller
@RequestMapping({"/people", "/people/"})
public class PeoplePageController {
@RequestMapping(method = RequestMethod.GET)
public String initPeoplePage(Model model) {
ArrayList<Person> people = createRandomPeople(5);
model.addAttribute("people", people);
return "peoplePage";
}
private ArrayList<Person> createRandomPeople(int numberOfPeople) {
ArrayList<Person> people = new ArrayList<Person>();
for (int i = 0; i < numberOfPeople; i++) {
Person person = new Person("FirstName" + i, "LastName" + i, i + 20);
people.add(person);
}
return people;
}
}
<table
class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>First name</th>
<th>Last name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<div th:each="person : ${people}">
<tr>
<td th:text="${person.firstName}"></td>
<td th:text="${person.lastName}"></td>
<td th:text="${person.age}"></td>
</tr>
</div>
</tbody>
</table>
问题可能与您如何使用迭代来构建HTML表有关。您不需要添加div来迭代每一行,而是使用tr标记。在您的示例中:
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>First name</th>
<th>Last name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr th:each="person : ${people}">
<td th:text="${person.firstName}"></td>
<td th:text="${person.lastName}"></td>
<td th:text="${person.age}"></td>
</tr>
</tbody>
</table>
名字
姓
年龄
Thymeleaf文档中关于迭代的更多信息:问题可能与您如何使用迭代来构建HTML表有关。您不需要添加div来迭代每一行,而是使用tr标记。在您的示例中:
<table class="col-md-12 table-bordered table-striped table-condensed cf">
<thead class="cf">
<tr>
<th>First name</th>
<th>Last name</th>
<th>Age</th>
</tr>
</thead>
<tbody>
<tr th:each="person : ${people}">
<td th:text="${person.firstName}"></td>
<td th:text="${person.lastName}"></td>
<td th:text="${person.age}"></td>
</tr>
</tbody>
</table>
名字
姓
年龄
Thymeleaf文档中关于迭代的更多信息:我自己已经尝试过这段代码,它可以正常工作,没有任何错误。只有将null对象添加到人员列表中时才会发生这种情况 例:
ArrayList<Person> people = createRandomPeople(5);
people.add(null);
arraylistpeople=createRandomPeople(5);
people.add(null);
我自己尝试过这段代码,它运行正常,没有任何错误。只有将null对象添加到人员列表时才会发生这种情况
例:
ArrayList<Person> people = createRandomPeople(5);
people.add(null);
arraylistpeople=createRandomPeople(5);
people.add(null);
getter和setter?在Person.java类中,它们是getter和setter,我只是没有复制它们。更正了。getter和setter?在Person.java类中,它们是getter和setter,我只是没有复制它们。更正了。