Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/jsf/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jsf 使用picklist生成ArrayList并在数据库中查找匹配值_Jsf - Fatal编程技术网

Jsf 使用picklist生成ArrayList并在数据库中查找匹配值

Jsf 使用picklist生成ArrayList并在数据库中查找匹配值,jsf,Jsf,我有一个名为newSymptomList的数组列表,其中包含一个症状id列表,例如拾取列表目标生成的[1,3,4]。我想检查每个症状,并从数据库中获取相关症状名称,尽管我被困在while循环中 挑选名单: <rich:pickList id="plID" listWidth="10em" value="#{sym.newSymptomList}" sourceCaption="Symptom

我有一个名为newSymptomList的数组列表,其中包含一个症状id列表,例如拾取列表目标生成的[1,3,4]。我想检查每个症状,并从数据库中获取相关症状名称,尽管我被困在while循环中

挑选名单:

<rich:pickList id="plID" listWidth="10em" 
                        value="#{sym.newSymptomList}" 
                        sourceCaption="Symptoms" 
                        targetCaption="Selected Symptoms">
                    <!--List of all Symptoms-->
                    <f:selectItems value="#{sym.allSym}" var="c"
                                   itemLabel="#{c.symptomName}" itemValue="#{c.symptomId}"/>
</rich:pickList>
SymptomBean中的相关代码:

private List<Symptom> newSymptomList = new ArrayList<Symptom>();

public List getNewSymptomList()
{
  return newSymptomList;
}

public void setNewSymptomList(List<Symptom> newSymptomList )
{
  this.newSymptomList = newSymptomList; 
}

//Here is the code which returns a list of matching symptom names:

public List getSymNames() {
    List selectedSymptoms= new ArrayList(); 
    int i = 0;
    try
    {
        Class.forName("com.mysql.jdbc.Driver");
        con = ...
        ps = con.createStatement();
        rs = ps.executeQuery("select * from symptoms");
        while (rs.getString(1)== newSymptomList) {
            //getString(1) is symptomID and getString(2) is symptomName
            selectedSymptoms.add(i,new Symptom(rs.getString(1), rs.getString(2)));
            i++;
        }
    }//end of try

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

    return selectedSymptoms;
}

在达到要求方面,你走错了方向。您的症状名称已存在于allSym列表中,由引用。您的错误是,您将所选症状设置为列表,而实际上它应该是列表或任何类型,{c.sympleId}实际上似乎是字符串,因为您试图通过ResultSetString获取它,ResultSetString不是sane类型,但除此之外,否则在对它进行迭代时会得到ClassCastException

相应地进行修正:

private List<String> newSymptomList; // +getter+setter.

另一种方法是将newSymptomList保持为一个真实的列表,并将您的项目值固定为{c},而不是{c.symptomId}。您只需要实现一个javax.faces.converter.converter,这样JSF就能够在症状和它唯一的字符串表示之间自动转换。例如,另请参见。

由于收到错误而被卡住?看不到如何创建查询?不知道该循环什么?你在忙什么?谢谢巴卢斯,帮了大忙。我有点困惑,因为我一直在allSym上得到一个错误,每个语句的错误在哪里?
List<Symptom> selectedSymptoms = new ArrayList<Symptom>();

for (Symptom symptom : allSym) {
    if (newSymptomList.contains(symptom.getSymptomId())) {
        selectedSymptoms.add(symptom);
    }
}

// ...