I am working on a JSF application. I have used new LinkedHashMap to retrieve two column values from a table of database to populate a listbox. First column contains the Item and second column contains the Price. The items are displaying in my listbox, but when I select one or two of the items and clicked the button calculate, its gives me error. The error is
javax.faces.FacesException: Target model Type is no a Collection or Array
See my code below Java:
String url = "jdbc:mysql://localhost:3306/ListBox";
String user = "root";
String pw = "root";
String favlst;
double total;
double price;
String item;
Map<String,Object> lst; {
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(url, user, pw);
String sql = "SELECT * FROM list";
Statement stt = con.createStatement();
ResultSet rs = stt.executeQuery(sql);
lst = new LinkedHashMap<String, Object>();
while(rs.next()){
price = rs.getDouble("Price");
item = rs.getString("Item");
lst.put(item, price);
}
}catch(Exception e){
}
}
public double getTotal() {
return total;
}
public void setTotal(double total) {
this.total = total;
}
public Map<String, Object> getSelectlst() {
return lst;
}
public String Calculate(){
total = 0;
price = 0;
total += (Double)lst.get(item);
return "success";
}
}
JSF
<h:form>
<h3> Generated by Map </h3>
<h:selectManyListbox value = "#{menu2.selectlst}">
<f:selectItems value = "#{menu2.selectlst}"/>
</h:selectManyListbox>
<br> <br>
<h:commandButton value = "Calculate" action = "#{menu2.Calculate}"/>
<br> <br>
Total: Rs <h:outputLabel value = "#{menu2.total}"></h:outputLabel>
</h:form>