在JSP中从EL调用POJO方法的正确方法

时间:2016-03-21 10:08:51

标签: java jsp el

我有以下代码:

public class ChatUser {
public ChatUser(String UserId, boolean isSelected) {
    this.UserId = UserId;
    this.isSelected = isSelected;     
}
public String getUserId() {
    return UserId;
}
public void setUserId(String UserId) {
    this.UserId = UserId;
}
public boolean isIsSelected() {
    return isSelected;
}
public void setIsSelected(boolean isSelected) {
    this.isSelected = isSelected;
}    
private String UserId;
private boolean isSelected;    

}

我正在从EL

访问该方法
<rows>
    <c:forEach var="user"  items='${requestScope.userList}'        varStatus="indexnum" >   
        <row id="${user.getUserId()}">
            <cell>${user.isIsSelected()}</cell>
            <cell>${user.getUserId()}</cell>
        </row> 
    </c:forEach>
</rows>

但它出现以下错误:

javax.el.MethodNotFoundException: Method not found: class s.getUserId()

有时它有效但不一致。

JSP Error

1 个答案:

答案 0 :(得分:-1)

必须在Java Bean的字段名中使用Camel案例。变量名应为userId并已选中。在您的情况下,请使用以下内容:

<rows>
    <c:forEach var="user"  items='${requestScope.userList}' varStatus="indexnum" >   
        <row id="${user.UserId}">
            <cell>${user.isSelected}</cell>
            <cell>${user.UserId}</cell>
        </row> 
    </c:forEach>
</rows>