0

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>
Kukeltje
  • 12,223
  • 4
  • 24
  • 47
James Smith
  • 55
  • 2
  • 11

1 Answers1

1

Your first error is that you use the same property in the value attribute of the f:selectItems en h:selectManyListbox. That must not be right. And if you look at the LinkedHasMap api, https://docs.oracle.com/javase/6/docs/api/java/util/LinkedHashMap.html, the error is very clear. Change the property behind the value of the selectManyListbox to one that is of the correct type.

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
  • Please correct my code :( – James Smith Nov 07 '15 at 09:33
  • All the info is in there. If you want a fully prepared example, wait for someone with more time and a real computer. I don't have the time nor the urge nor the material (a mobile phone) to help further, sorry. – Kukeltje Nov 07 '15 at 09:36
  • and btw, the title is not in line with the problem. Since the items **are** displayed and hence the mysql tag is 'wrong' (as is the java tag) – Kukeltje Nov 07 '15 at 18:24
  • How do I calculate the price of items selected ? – James Smith Nov 07 '15 at 19:18
  • Sorry, seriously… first of all is this comment totally unrelated to your original question and this answer and secondly it is **soooooo** basic that if you are uncapable of just doing it or finding (e.g. via a search engine) how to do this in java, I'd go searching for q different job… sorry to be so blunt. Please flag this comment if you want – Kukeltje Nov 07 '15 at 19:25