使用数据库中的项填充selectManyListbox

时间:2012-03-07 19:54:52

标签: jsf

我目前正在尝试查看表格中的症状并在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 个答案:

答案 0 :(得分: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>