我通过使用jstl在jsp页面上显示数据时遇到问题。

这是我的页面,并显示信息:

EMPTY !!!
Name    Email   Action


get-user.jsp

<html>
    <head>
        <title>All users</title>
    </head>
    <body>
        <c:if test="${empty webModels}">
            <h3>EMPTY !!!</h3>
        </c:if>
            <table>
                <thead>
                <tr>
                    <th>Name</th>
                    <th>Email</th>
                    <th>Action</th>
                </tr>
                </thead>
                <tbody>
                <c:forEach items="${webModels}" var="web">
                    <tr>
                        <td>${web.userName}</td>
                        <td>${web.userEmail}</td>
                        <td>${web.Action}</td>
                    </tr>
                </c:forEach>
                </tbody>
            </table>
    </body>
</html>


控制器打印信息大小4

@RequestMapping(value = "/get-user", method = RequestMethod.GET)
public List<WebModel> getWebModels(){
    List<WebModel> webModels = serviceWeb.getAllUser();
    System.out.println(webModels.size()); //not empty
    return webModels;
}


WebModel我从一些表中获取数据库中的数据,而我在WebModel中设置了主数据

public class WebModel {
    private String userName;
    private String userEmail;
    private String Action;
    //getter setter constructor
}

最佳答案

documentation about return types of controller methods说:


使用在方法级别通过@ModelAttribute指定的属性名称(或基于返回类型类名称的默认属性名称),可以将任何其他返回类型视为要公开给视图的单个模型属性。该模型隐含了命令对象和带@ModelAttribute注释的参考数据访问器方法的结果。


因此,如果我没看错,则应在JSP中使用list而不是webModels,或者应使用以下方法注释该方法

@ModelAttribute("webModels")

08-03 18:38
查看更多