简体   繁体   中英

JSP does not render java list in Spring Boot 2

I have a simple java app in Spring Boot 2 with JSP. I use h2 database for testing data. It works fine but JSP does not render the list data from the controller. The list in JSP is empty, while in the controller it has 2 values. Other properties like String are working fine. I cannot understand where the problem is.

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:

在此处输入图片说明 What could be the issue here?

Add the following line at the top in your JSP:

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

Also, replace

return "/users";

with

return "users";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM