2013-03-18 68 views
-1

我有問題在jsp頁面的幫助下顯示jsp頁面中的數據。有代碼,也有我得到的錯誤; 我的JSP頁面如何在jsp中使用jstl arraylist

 <body> 
    <table border="1"> 
     <thead> 
       <tr> 
        <th>UserId </th> 
        <th>First Name </th> 
        <th>Last Name </th> 
        <th>DOB </th> 
        <th>Email Id </th> 
        <th colspan="2" >Action</th> 
       </tr> 
     </thead> 
     <tbody> 
      <c:forEach items="${users}" var="user" > 
      <tr> 
        <td><c:out value="${user.userid}" /></td> 
        <td><c:out value="${user.firstName}" /></td> 
        <td><c:out value="${user.lastName}" /></td> 
        <td><fmt:formatDate pattern="yyyy-MMM-dd" value="${user.dob}" /></td> 
        <td><c:out value="${user.email}" /></td> 
        <td><a href="UserController?action=edit&userId=<c:out value="${user.userid}"/>">Update</a></td> 
        <td><a href="UserController?action=delete&userId=<c:out value="${user.userid}"/>">Delete</a></td> 
       </tr> 

       </c:forEach> 

     </tbody> 
    </table> 
    <p><a href="UserController?action=insert">Add User</a> 

這段代碼,我們取ALLUSERLIST爲: 公衆的ArrayList getAllUsers() {

ArrayList<User> USER = new ArrayList<User>(); 
    try 
    { 
     iduQuery = "select * from user"; 
     Statement st = conn.createStatement(); 
     ResultSet rs =st.executeQuery(iduQuery); 
     while(rs.next()) 
     { 
      User user =new User(); 

      user.setUserid(rs.getString("userid")); 
      System.out.println("USER ID : " + user); 
      user.setFirstname(rs.getString("firstname")); 
      user.setLastname(rs.getString("lastname")); 
      user.setDob(new java.sql.Date(rs.getDate("dob").getTime())); 
      user.setEmail(rs.getString("email")); 
      USER.add(user); 
     } 

    }catch(Exception e) 
    { 
      e.printStackTrace(); 
      System.out.println("Error : GetALLUSER " + e); 
    } 
    return USER; 

}

當我們調用JSP頁面:

  forward ="/listUser.jsp"; 
      ArrayList<User> myList = new ArrayList<User>(); 
     myList = userdao.getAllUsers(); 
     System.out.println("THis List of data arte : " + myList); 
     request.setAttribute("users", myList); 

和錯誤是表明: HTTP狀態500 -

org.apache.jasper.JasperException: org.apache.jasper.JasperException: An exception occurred processing JSP page /listUser.jsp at line 33 
30:   <tbody> 
31:   <c:forEach items="${users}" var="user"> 
32:    <tr> 
33:      <td><c:out value="${user.userid}" /></td> 
34:      <td><c:out value="${user.firstName}" /></td> 
35:      <td><c:out value="${user.lastName}" /></td> 
36:      <td><fmt:formatDate pattern="yyyy-MMM-dd" value="${user.dob}" /></td> 

User.class

package com.d.model; 
import java.util.Date;; 

public class User { 
    private String userid; 
    private String firstName; 
    private String lastName; 
    private Date dob; 
    private String email; 

    public String getUserid() { 

     return userid; 
    } 
    public void setUserid(String userid) { 

     this.userid = userid; 

    } 
    /** 
    * @return the firstname 
    */ 
    public String getFirstname() { 
     return firstName; 
    } 
    /** 
    * @param firstname the firstname to set 
    */ 
    public void setFirstname(String firstName) { 
     this.firstName = firstName; 

    } 
    /** 
    * @return the dob 
    */ 
    public Date getDob() { 
     return dob; 
    } 
    /** 
    * @param dob the dob to set 
    */ 
    public void setDob(Date dob) { 
     this.dob = dob; 
    } 
    /** 
    * @return the email 
    */ 
    public String getEmail() { 
     return email; 
    } 
    /** 
    * @param email the email to set 
    */ 
    public void setEmail(String email) { 
     this.email = email; 
    } 
    /** 
    * @return the lastname 
    */ 
    public String getLastname() { 
     return lastName; 
    } 
    /** 
    * @param lastname the lastname to set 
    */ 
    public void setLastname(String lastName) { 

     this.lastName = lastName; 

    } 

    @Override 
    public String toString() { 
     return "User [userid= " + userid + ", firstName= '" + firstName 
       + "', lastName=" + lastName + ", dob=" + dob + ", email=" 
       + email + "]"; 
    }  
} 
+0

你沒有顯示你的相關列堆棧跟蹤「usrid」未找到 – PSR 2013-03-18 11:29:32

+0

請刪除此代碼,並顯示現在的代碼 – PSR 2013-03-18 11:30:35

+0

可以顯示異常堆棧跟蹤 – PSR 2013-03-18 11:34:20

回答

2

拼寫錯誤setUsrid .change它setUserid

要設置usrid但你正在訪問userid

user.setUsrid(rs.getString("userid")); //usrid 

${user.userid} // userid 
+0

@Deval Dholariya看到我的答案。我認爲它會爲你工作ü – PSR 2013-03-18 09:16:36

+0

感謝PSR,我已經使用usrid的userid實例,所以請給我幫助,它仍然給錯誤:Error:GetALLUSER java.sql.SQLException:Column'usrid'未找到。如果我已經使用userid,那麼在提問中提到錯誤。 – 2013-03-18 11:13:38

+0

@Deval Dholariya可以再次顯示我的代碼,並且目前的錯誤 – PSR 2013-03-18 11:14:12

1

拼寫錯誤setUsrid .change它setUserid

要設置usrid但您要訪問的用戶ID

user.setUsrid(rs.getString("userid")); //usrid 

${user.userid} // userid 
+0

謝謝你給我答覆。 – 2014-03-14 06:01:51

相關問題