如何在jsp usin for Each循环中打印列表对象?



给定这个jsp页面和这个控制器代码,当我尝试直接打印对象时,它给了我输出:

[User [id=1, firstName=Ishwar, lastName=Sonar, emailId=ishwarsonar95@gmail.com`]]

但是当我使用 forEach 打印对象的单个数据时,它给了我以下错误:

出现意外错误(类型=内部服务器错误,状态=500(。

The class 'com.suraj.springbootdemo.entity.User' does not have the property 'firstName'.
javax.el.PropertyNotFoundException: The class 'com.suraj.springbootdemo.entity.User' does not have the property 'firstName'.

forEach内打印用户时,您正在尝试访问firstName属性。JSP 表达式语言不适用于属性,仅适用于 getter/setter 方法(例如getFirstName()(。如果您还没有 getter,则需要在User类中创建一个:

public String getFirstName() {
return firstName;
}

然后,您可以使用以下命令在 JSP 代码中访问它:

<td>${u.getFirstName()}</td>

最新更新