无法从JSP中的java bean获取数据
我的搜索工作正常,我可以从数据库中得到结果。但是从Beans getters获取数据让我感到困惑。 多谢各位 我的豆类:无法从JSP中的java bean获取数据,java,html,jsp,Java,Html,Jsp,我的搜索工作正常,我可以从数据库中得到结果。但是从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; } pu
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 List getownerByName(字符串L_Name)引发SQLException{
PreparedStatement st=null;
结果集rs;
List OwnerL=new ArrayList();//创建类型的新列表
//所有者对象
试一试{
MySQLConnection.getConnection();//获取连接
st=MySQLConnection.con
.prepareStatement(“从petclinic.owners中选择*,其中姓氏=?”;
st.setString(1,L_名称);
//获取所有所有者的SQL查询
//谁的id是用户在何时定义的
//校准此方法
rs=st.executeQuery();//执行查询并将结果保存到
//结果集
如果(!rs.isBeforeFirst()){//如果rs指针不在
//第一。。
//即如果查询没有返回
//任何事
抛出新的CustomException(“数据库中没有此ID的所有者”);//抛出
//习俗
//例外情况
//的
//不是
//发现
}
while(rs.next()){//while结果集仍指向下一个
//划船
Owner OO=new Owner();//将Owner bean的新对象设置为
//使用中的所有字段
//类并设置这些值
OO.setId(rs.getInt(1));//将所有者id设置为
//第一列。。。。
OO.setFName(rs.getString(2));
OO.setLName(rs.getString(3));
OO.setAddress(rs.getString(4));
OO.setCity(rs.getString(5));
setel(rs.getString(6));
OwnerL.add(OO);//将对象添加到所有者列表中
}
for(Owner元素:OwnerL){//在所有者列表中循环
//系统输出打印项次(元素);
}
}捕获(例外情况除外){
系统输出打印项次(“错误”+ex);
}
最后{
MySQLConnection.con.close();
System.out.println(“”);
System.out.println(“连接关闭”);
System.out.println(“”);
}
归还所有权;
}
我的车主服务方式:
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;
}
public静态列表getOwnerbyLName1(字符串last\u Name)引发SQLException{
OwnerDaoImp ODI=新的OwnerDaoImp();
List NewL=newarraylist();
NewL=ODI.getOwnerbyLName(姓氏);
返回NewL;
}
我的姓氏搜索:
<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" />
名字:
在标题中声明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\u变量
名称,因为您没有发布该代码。根据您的代码,假设owner
可能是数组变量。发布servlet代码以获得更有用的答案。希望这有帮助 也发布你的Servlet代码。谢谢你。。。对不起,迟了答复。但我的工作正常了。很高兴你自己修好了。