java - JSP 在 Spring Boot 2 中不渲染 java 列表

标签 java spring-boot jsp

我在 Spring Boot 2 中有一个带有 JSP 的简单 java 应用程序。 我使用h2数据库来测试数据。 它工作正常,但 JSP 不会呈现来自 Controller 的列表数据。 JSP 中的列表是空的,而在 Controller 中它有 2 个值。其他属性(例如 String)工作正常。 我不明白问题出在哪里。

Controller:

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

User model:

@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:

<%@ 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" />

Debug from the controller:

user: User(id=111111, firstName=Wick, lastName=England, country=John)
user: User(id=111112, firstName=Madman, lastName=USA, country=Andy)

UserService.cls returns List from h2 and it is in the debug, it looks fine.

@Transactional
public List<User> getAll() {
    return (List<User>) this.userRepository.findAll();
}

Screenshot:

enter image description here 这里可能出现什么问题?

最佳答案

在 JSP 顶部添加以下行:

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

另外,替换

return "/users";

return "users";

关于java - JSP 在 Spring Boot 2 中不渲染 java 列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59894386/

相关文章:

java - 二叉树广度优先搜索算法

jsp - 在jsp中获取上下文路径的问题?

css - 隐藏的 div 在 IE 中的位置有大的空白

java - 导航 2D 数组时,检查相邻元素是否存在相对于入口点的有效路径?

java - HTMLUnit 不等待 Javascript

java - 使用 Maven 的 Spring Boot 示例不会在代理后面编译

java - 使用 MapStruct 映射枚举字段

java - 如何在 Spring Data Mongodb 上使用 SpEL 表达式?

java - Servlet:无法从 jsp 链接 css:无法解析目录资源:${pageContext.request.contextPath} 链接到 root 而不是 src/main/webapp

不允许声明 Java 变量