2016-03-01 25 views
0

我的搜索工作正常,我可以從數據庫中獲取結果。但是從Bean getters獲取數據讓我感到困惑。 謝謝無法從JSP中的Java bean獲取數據

我豆類:

public class Owner { 

private int Id; 
private String FName; 
private String LName; 
private String Address; 
private String City; 
private String Tel; 

public int getId() { 
    return Id; 
} 

public void setId(int id) { 
    Id = id; 
} 

public String getFName() { 
    return FName; 
} 

public void setFName(String fName) { 
    FName = fName; 
} 

public String getLName() { 
    return LName; 
} 

public void setLName(String lName) { 
    LName = lName; 
} 

public String getAddress() { 
    return Address; 
} 

public void setAddress(String address) { 
    Address = address; 
} 

public String getCity() { 
    return City; 
} 

public void setCity(String city) { 
    City = city; 
} 

public String getTel() { 
    return Tel; 
} 

public void setTel(String tel) { 
    Tel = tel; 
} 



@Override 
public String toString() { 
    return (" \n Owner ID   : " + this.getId() 
      + " \n Owner First Name : " + this.getFName() 
      + " \n Owner Last Name : " + this.getLName() 
      + " \n Owner Address  : " + this.getAddress() 
      + " \n Owner City  : " + this.getCity() 
      + " \n Owner Phone  : " + this.getTel()) 
      +"\n ----------------------------------------------" 
      +"            "; 
} 

} 

我獲取業主按姓氏方法:

public List<Owner> getOwnerbyLName(String L_Name) throws SQLException { 

    PreparedStatement st = null; 
    ResultSet rs; 
    List<Owner> OwnerL = new ArrayList<Owner>(); // making new List of type 
                // Owner Object 

    try { 

     MySQLConnection.getConnection(); // getting Connection 

     st = MySQLConnection.con 
       .prepareStatement("SELECT * FROM petclinic.owners where last_name =? "); 
     st.setString(1, L_Name); 
     // SQL Query for getting all the owners 
     // whose id is what user defined when 
     // caling this method 

     rs = st.executeQuery(); // executing query and saving the result in 
           // result set 

     if (!rs.isBeforeFirst()) { // if the rs pointer is NOT before the 
            // 1st .. 
            // i.e if the query didnt return 
            // anything 

      throw new CustomException("No Owner With This ID in DataBase"); // throwing 
                      // custom 
                      // exception 
                      // of 
                      // not 
                      // found 
     } 

     while (rs.next()) { // while result set is still pointing to next 
          // row 

      Owner OO = new Owner(); // making new object of owner bean to 
            // make use of all the fields in the 
            // class and set those values 

      OO.setId(rs.getInt(1)); // setting owner id to be the int in 
            // the 1st col.... 
      OO.setFName(rs.getString(2)); 
      OO.setLName(rs.getString(3)); 
      OO.setAddress(rs.getString(4)); 
      OO.setCity(rs.getString(5)); 
      OO.setTel(rs.getString(6)); 

      OwnerL.add(OO); // add the object in the list of owner 

     } 

     for (Owner element : OwnerL) { // looping through the owner list 
      // System.out.println(element); 
     } 

    } catch (Exception ex) { 

     System.out.println("Error " + ex); 

    } 

    finally { 

     MySQLConnection.con.close(); 
     System.out.println(" "); 
     System.out.println("Connection Closed"); 
     System.out.println(" "); 

    } 

    return OwnerL; 

} 

我的業主服務方法:

public static List<Owner> getOwnerbyLName1(String last_Name) throws SQLException{ 
     OwnerDaoImp ODI = new OwnerDaoImp(); 
     List<Owner> NewL = new ArrayList(); 
     NewL=ODI.getOwnerbyLName(last_Name); 
     return NewL; 
    } 

我的搜索按姓氏JSP:

<form method=GET> 
    Last name:<br> 
    <input type=text name=lastname><br> <br> 
    <input type=submit value=Submit name=button> 
</form> 

現在我想從我的豆得到的數據,我無法得到它,新的在這裏,希望問題是明確的。

這是我的搜索結果頁面:

<jsp:useBean id="Owners" class="com.rt.Beans.Owner"> 
</jsp:useBean> 


<p> 
    First Name: 
    <jsp:getProperty name="Owners" property="FName" /> 
+0

也發佈您的Servlet代碼。 –

回答

0

聲明JSTL標籤庫在你的頭,

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

,並嘗試以下,

<jsp:useBean id="owner" class="com.rt.Beans.Owner" scope="request"> </jsp:useBean> 

<c:forEach var="ownerObj" items="${owner.Owners}" > 
     <c:out value="${ownerObj.FName}"/> 
</c:forEach> 

在這裏我不知道關於servlet中的array_variable名稱,因爲您沒有發佈該代碼。從您的代碼假設Owners可能是數組變量。發佈servlet代碼以獲得更有用的答案。希望這可以幫助。

+0

謝謝你... 對於遲到的回覆感到抱歉。 但我得到了我的工作。 –

+0

很高興你解決了你自己。 –