我在springboot2中有一个带有jsp的简单java应用程序。我使用h2数据库来测试数据。它工作正常,但是jsp不呈现来自控制器的列表数据。jsp中的列表是空的,而在控制器中它有2个值。其他属性(如string)工作正常。我不明白问题出在哪里。
控制器:
@Controller
public class HomeController {
@Autowired
UserService userService;
@GetMapping("/users")
public String getUsers(ModelMap map) {
this.userService.getAll().forEach(user -> System.out.println("user: " + user));
map.addAttribute("users", this.userService.getAll());
return "/users";
}
}
用户模型:
@NoArgsConstructor
@Getter
@Setter
@EqualsAndHashCode
@ToString
@Entity
public class User implements Serializable {
@Id
@GeneratedValue
private Long id;
private String firstName;
private String lastName;
private String country;
public User(@JsonProperty Long id, @JsonProperty String firstName, @JsonProperty String lastName, @JsonProperty String country) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.country = country;
}
}
jsp页面:
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<jsp:include page="header.jsp">
<jsp:param name="title" value="LIBRARY - Users"/>
</jsp:include>
<!--NAVBAR-->
<%@ include file="navbar.jsp"%>
<!--CONTENT-->
<div class="container-fluid h-100">
<table class="table table-hover">
<thead>
<tr>
<th>Index</th>
<th>Id</th>
<th>First Name</th>
<th>Last Name</th>
<th>Country</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="user" varStatus="iteration">
<tr>
<td>${iteration.index}</td>
<td>${user.id}</td>
<td>${user.firstName}</td>
<td>${user.lastName}</td>
<td>${user.country}</td>
</tr>
</c:forEach>
</tbody>
</table>
<c:if test="${empty users}">
<div class="d-flex justify-content-center">
<p>There are no records in the database</p>
</div>
</c:if>
</div>
<jsp:include page="footer.jsp" />
从控制器调试:
user: User(id=111111, firstName=Wick, lastName=England, country=John)
user: User(id=111112, firstName=Madman, lastName=USA, country=Andy)
cls从h2返回list,它在调试中,看起来很好。
@Transactional
public List<User> getAll() {
return (List<User>) this.userRepository.findAll();
}
截图:
有什么问题吗?
1条答案
按热度按时间2w3rbyxf1#
在jsp的顶部添加以下行:
同时,更换
具有