2012-03-07 20 views
1

我目前正在嘗試通過表中的症狀並將它們顯示在selectManyListbox中,儘管當我編譯和運行時列表框並未出現。填充selectManyListbox與數據庫中的項目

的index.html說sym.symptomList和sym.Symptom是未知屬性

的index.html

<h:selectManyListbox id ="list1" value="#{sym.symptomList}"> 
    <f:selectItems value="#{sym.Symptom}" var="c" 
     itemLabel="#{c.symptomName}" itemValue="#{c.symptomId}"/> 
</h:selectManyListbox> 

豆:

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.SessionScoped; 
import java.util.*; 
import java.sql.*; 

@ManagedBean(name="sym") 
@SessionScoped 
public class SymptomBean{ 

Connection con; 
Statement ps; 
ResultSet rs; 
private List symptomList = new ArrayList(); 

public List getAllSym() { 
int i = 0; 
try 
{ 
    Class.forName("com.mysql.jdbc.Driver"); 
    con = DriverManager.getConnection(...) //ommitted details 
    ps = con.createStatement(); 
    rs = ps.executeQuery("select * from symptoms"); 
    while (rs.next()) { 
     symptomList.add(i, new Symptom(rs.getString(1), rs.getString(2))); 
     i++; 
     } 
} 

catch (Exception e) 
{ 
    System.out.println("Error Data : " + e.getMessage()); 
} 

return symptomList; 
} 

public class Symptom{ 
public String symptomId; 
public String symptomName; 

    public Symptom(String symptomId, String symptomName){ 
    this.symptomId = symptomId; 
    this.symptomName = symptomName; 
    } 

public String getSymptomId(){ 
    return symptomId; 
    } 

public String getSymptomName(){ 
    return symptomName; 
    } 
}  

}

回答

1

它說sym.symptomList是一個未知的道具因爲你沒有一個公共的getter和setter,並且sym.Symptom根本不存在。

爲symptomList創建公共getter和setter,並且不要在getAllSym()方法中設置syptomList的值。事情是這樣的:

private List symptomList; 

public List getSymptomList() {return symptomList;} 
public void setSymptomList(List symptomList) {this.symptomList=symptomList;} 

public List getAllSym() { 
    List allSymptoms = new ArrayList(); 
    //DB lookup logic... 
    while (rs.next()) { 
     allSymptoms.add(new Symptom(rs.getString(1), rs.getString(2)));   
    } 

    //Close connections, error handling, etc... 

    return allSymptoms; 
} 

然後你.xhtml是:

<h:selectManyListbox id ="list1" value="#{sym.symptomList}"> 
    <f:selectItems value="#{sym.allSym}" var="c" 
    itemLabel="#{c.symptomName}" itemValue="#{c.symptomId}"/> 
</h:selectManyListbox> 
+0

它現在,謝謝! – Infinity 2012-03-08 15:50:42