为什么<c:forEach>在JSP Spring中不使用Ajax请求?

mof*_*tje 2 ajax spring jsp controller

我在主JSP页面中使用Ajax请求获取一些数据.

main.jsp的片段

function gu(){
    $.get('/admin/getAllUsers', {}, function(data) {
        console.log(data); // see below
        $("#allUsersData").html(data);
    });
}
Run Code Online (Sandbox Code Playgroud)

在我的Spring控制器中,我将所有用户添加到不同的JSP页面.

MainController.java的片段

@RequestMapping(value = "/admin/getAllUsers", method = RequestMethod.GET)
public String getAllUsers(Model model){
    List<User> users = userRepository.findAll();
    System.out.println(users.size()); // output: 3
    model.addAttribute("allUsers", users);

    return "data/all-users";
}
Run Code Online (Sandbox Code Playgroud)

现在在all-users.jsp我有一个<c:forEach>应该加载html表中的所有用户:

<table class="table">
    <thead>
        <tr>
            <th>Firstname</th>
            <th>Lastname</th>
            <th>Email</th>
            <th>Date</th>
        </tr>
    </thead>
    <tbody>
        <c:if test="${not empty allUsers}">
            <c:forEach items="${allUsers}" var="usr">
                <tr>
                    <td>${usr.firstName}</td>
                    <td>${usr.lastName}</td>
                    <td>${usr.username}</td>
                    <td>${usr.creationDate}</td>
                </tr>
            </c:forEach>
        </c:if>
    </tbody>
</table>
Run Code Online (Sandbox Code Playgroud)

但是,当我将来自请求的html添加到我的主JSP页面时,会显示一个空表.当我记录Ajax请求的结果时,我发现用户数据被插入到all-users.jsp中:

<c:if test="true">
    <c:forEach items="[User{id=1, username='username1', firstName='John', lastName='Doe', roles=[Role{id=1, name='ROLE_USER'}], creationDate=2018-02-19T08:58:13.333}, User{id=2, username='username2', firstName='John2', lastName='Doe2', roles=[Role{id=3, name='ROLE_USER'}], creationDate=2018-02-19T08:58:13.471}]" var="usr">
        <tr>
            <td></td>
            <td></td>
            <td></td>
            <td></td>
        </tr>
    </c:forEach>
</c:if>
Run Code Online (Sandbox Code Playgroud)

为什么数据被加载到数据JSP页面中,但在将其附加到主JSP页面时却没有显示?

Rav*_*lor 6

你能检查一下,也许你没有在你的JSP文件中包含核心标记库.您将通过在文件顶部插入以下行来完成此操作.

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
Run Code Online (Sandbox Code Playgroud)