I have an array list named “newSymptomList” which contains a list of Symptom id’s (e.g. [1,3,4]) generated by a pick list target. I want to go through each symptom and get the relevant symptom name from the database although I’m stuck on the while loop.
PickList:
<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>
Relevant code in 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;
}
You’re going in the wrong direction as to achieving the requirement. You have the symptom names already there in the
allSymlist as referenced by<f:selectItems>. Your mistake is that you’re setting the selected symptoms asList<Symptom>while it should actually be aList<String>or whatever type the#{c.symptomId}actually is (it seems to beStringas you’re attempting to get it byResultSet#getString(), which is not a sane type, but that aside), otherwise you would get aClassCastExceptionwhen iterating over it.Fix it accordingly:
Finally, you could just go through the
allSymlist which you already have there in the same bean to get theSymptomobjects.Another way is to keep the
newSymptomLista realList<Symptom>and fix theitemValueof your<f:selectItems>to be#{c}instead of#{c.symptomId}. You only need to implement ajavax.faces.converter.Converterso that JSF will be able to automatically convert betweenSymptomand its uniqueStringrepresentation. For an example, see also our<h:selectOneMenu>tag wiki page.