Reputation: 73
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;
}
Upvotes: 0
Views: 973
Reputation: 1109432
You're going in the wrong direction as to achieving the requirement. You have the symptom names already there in the allSym
list as referenced by <f:selectItems>
. Your mistake is that you're setting the selected symptoms as List<Symptom>
while it should actually be a List<String>
or whatever type the #{c.symptomId}
actually is (it seems to be String
as you're attempting to get it by ResultSet#getString()
, which is not a sane type, but that aside), otherwise you would get a ClassCastException
when iterating over it.
Fix it accordingly:
private List<String> newSymptomList; // +getter+setter.
Finally, you could just go through the allSym
list which you already have there in the same bean to get the Symptom
objects.
List<Symptom> selectedSymptoms = new ArrayList<Symptom>();
for (Symptom symptom : allSym) {
if (newSymptomList.contains(symptom.getSymptomId())) {
selectedSymptoms.add(symptom);
}
}
// ...
Another way is to keep the newSymptomList
a real List<Symptom>
and fix the itemValue
of your <f:selectItems>
to be #{c}
instead of #{c.symptomId}
. You only need to implement a javax.faces.converter.Converter
so that JSF will be able to automatically convert between Symptom
and its unique String
representation. For an example, see also our <h:selectOneMenu>
tag wiki page.
Upvotes: 1