无法从JSP中的java bean获取数据

时间:2016-03-01 04:32:23

标签: java html jsp

我的搜索工作正常,我可以从DB获得结果。但是从Beans getters获取数据让我感到困惑。 谢谢

我的豆类:

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>

现在我想从我的bean中获取数据而我无法得到它,这里有新的,希望问题很清楚。

这是我的搜索结果页:

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


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

1 个答案:

答案 0 :(得分:0)

在标题中声明JSTL taglib,

<%@ 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代码以获得更有帮助的答案。希望这可以帮助。