JSP EL中未找到属性异常

时间:2012-07-16 06:14:50

标签: jsp java-ee servlets el

非常讨厌。不知道为什么会出现这个错误。在下面的代码中

NOTE:

if I only use <td>${user.id}</td> than its printing id properly but on ${user.username} property generating error. See below JSP page code.

If I use below code in JSP page like to test than I get all the objects and properties


<c:forEach var="user" items="${users}" varStatus="i">
    <tr>
        <td>${users[i.index]}</td>
    </tr>                       
</c:forEach>

Result:

User[id=1,username=u1,password=p1,firstname=f1,lastname=l1,dob=2012-07-01,age=40]
User[id=2,username=u2,password=p2,firstname=f2,lastname=l2,dob=2012-07-02,age=39]
User[id=3,username=u3,password=p3,firstname=f3,lastname=l3,dob=2012-07-03,age=38]

错误:

09:49:49,047 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/crud_jsp_servlet_cdi_jdbc_tagfile].[jsp]] (http-localhost-127.0.0.1-8080-1) Servlet.service() for servlet jsp threw exception: javax.el.PropertyNotFoundException: The class 'com.myapp.dao.User' does not have the property 'username'.
    at javax.el.BeanELResolver.getBeanProperty(BeanELResolver.java:661) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.el.BeanELResolver.getValue(BeanELResolver.java:290) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:175) [jboss-el-api_2.2_spec-1.0.0.Final.jar:1.0.0.Final]
    at org.apache.el.parser.AstValue.getValue(AstValue.java:169) [jbossweb-7.0.13.Final.jar:]
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:189) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jasper.runtime.PageContextImpl.proprietaryEvaluate(PageContextImpl.java:935) [jbossweb-7.0.13.Final.jar:]
    at org.apache.jsp.WEB_002dINF.admin.UserList_jsp._jspx_meth_c_005fforEach_005f0(UserList_jsp.java:137)
    at org.apache.jsp.WEB_002dINF.admin.UserList_jsp.access$1(UserList_jsp.java:114)
    at org.apache.jsp.WEB_002dINF.admin.UserList_jsp$Helper.invoke1(UserList_jsp.java:215)
    at org.apache.jsp.WEB_002dINF.admin.UserList_jsp$Helper.invoke(UserList_jsp.java:240)
    at org.apache.jsp.tag.web.layout_tag.doTag(layout_tag.java:120)
    at org.apache.jsp.WEB_002dINF.admin.UserList_jsp._jspx_meth_tags_005flayout_005f0(UserList_jsp.java:96)
    at org.apache.jsp.WEB_002dINF.admin.UserList_jsp._jspService(UserList_jsp.java:69)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70) [jbossweb-7.0.13.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.0.Final.jar:1.0.0.Final]

JSP页面:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

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

<tags:layout path="${pageContext.request.contextPath}">
    <jsp:attribute name="search">
        <tags:search />
    </jsp:attribute>
    <jsp:body>
        <div class="mainDiv">
            <table class="table table-striped table-bordered table-condensed">
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>UserName</th>
                        <th>Password</th>
                        <th>FirstName</th>
                        <th>LastName</th>
                        <th>Date of Birth</th>
                        <th>Age</th>
                        <th>Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <c:forEach var="user" items="${users}">
                        <tr>
                            <td>${user.id}</td>
                            <td>${user.username}</td>
                            <td>${user.password}</td>
                            <td>${user.firstname}</td>
                            <td>${user.lastname}</td>
                            <td>${user.dob}</td>
                            <td>${user.age}</td>
                            <td><a href="#"><i class="icon-edit"></i></a> <a href="#"><i
                                    class="icon-trash"></i></a></td>
                        </tr>
                    </c:forEach>
                </tbody>
            </table>
        </div>
    </jsp:body>
</tags:layout>

User.java

package com.myapp.dao;

import java.sql.Date;


public class User {

    private Long id;
    private String username;
    private String password;
    private String firstname;
    private String lastname;
    private Date dob;
    private int age;

    public User(){
    };

    public User(Long id, String username, String password, String firstname, String lastname, Date dob, int age){

        this.id = id;
        this.username = username;
        this.password = password;
        this.firstname = firstname;
        this.lastname = lastname;
        this.dob = dob;
        this.age = age;
    }

    public Long getId(){
        return id;
    }

    public void setId(Long id){
        this.id = id;
    }

    public String getUserName(){
        return username;
    }

    public void setUserName(String username){
        this.username = username;
    }

    public String getPassword(){
        return password;
    }

    public void setPassword(String password){
        this.password = password;
    }

    public String getFirstName(){
        return firstname;
    }

    public void setFirstName(String firstname){
        this.firstname = firstname;
    }

    public String getLastName(){
        return lastname;
    }

    public void setLastName(String lastname){
        this.lastname = lastname;
    }

    public Date getDob(){
        return dob;
    }

    public void setDob(Date dob){
        this.dob = dob;
    }

    public int getAge(){
        return age;
    }

    public void setAge(int age){
        this.age = age;
    }

    public String getFullName(){
        return this.firstname + " " + this.lastname;
    }

}

Selvlet:

List<User> users = null;
        userDAO = new UserDAOJDBC();

        users = userDAO.listUsers();
        request.setAttribute("users", users);

        String path = "/WEB-INF/admin/UserList.jsp";
        getServletContext().getRequestDispatcher(path).forward(request, response);

1 个答案:

答案 0 :(得分:5)

案例对Java Bean属性很重要。在EL中,您尝试访问属性username,但在bean中,您有getUserName/setUserName。请改用getUsername/setUsername(小写'n')。